编写Java代码实现画图工具,实现画笔粗细、画笔颜色选择。插入图片,实现裁剪功能
时间: 2024-02-12 22:09:07 浏览: 111
以下是一个简单的 JavaFX 画图工具示例,实现了画笔粗细、画笔颜色选择、插入图片和裁剪功能。
```java
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class DrawingTool extends Application {
private Canvas canvas;
private GraphicsContext gc;
private Color color = Color.BLACK;
private double lineWidth = 1.0;
private Point2D startPoint;
private Rectangle clipRect;
private ImageView imageView;
@Override
public void start(Stage primaryStage) throws Exception {
canvas = new Canvas(800, 600);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.setOnMousePressed(event -> {
startPoint = new Point2D(event.getX(), event.getY());
gc.setStroke(color);
gc.setLineWidth(lineWidth);
gc.beginPath();
gc.moveTo(startPoint.getX(), startPoint.getY());
});
canvas.setOnMouseDragged(event -> {
gc.lineTo(event.getX(), event.getY());
gc.stroke();
});
canvas.setOnMouseReleased(event -> {
gc.closePath();
});
ColorPicker colorPicker = new ColorPicker();
colorPicker.setOnAction(event -> color = colorPicker.getValue());
Slider slider = new Slider(1, 10, 1);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.valueProperty().addListener((observable, oldValue, newValue) -> lineWidth = newValue.doubleValue());
Button insertImageButton = new Button("Insert Image");
insertImageButton.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image File");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
try {
Image image = new Image(selectedFile.toURI().toString());
imageView = new ImageView(image);
imageView.setPreserveRatio(true);
imageView.setSmooth(true);
imageView.setCache(true);
imageView.setOnMousePressed(insertImageEventHandler);
canvas.getChildren().add(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Button clipImageButton = new Button("Clip Image");
clipImageButton.setOnAction(event -> {
clipRect = new Rectangle();
clipRect.setFill(Color.TRANSPARENT);
clipRect.setStroke(Color.BLACK);
clipRect.setStrokeWidth(1.0);
canvas.setCursor(Cursor.CROSSHAIR);
canvas.getChildren().add(clipRect);
canvas.setOnMousePressed(clipImageEventHandler);
canvas.setOnMouseDragged(clipImageEventHandler);
canvas.setOnMouseReleased(clipImageEventHandler);
});
BorderPane root = new BorderPane();
root.setCenter(canvas);
root.setTop(colorPicker);
root.setBottom(slider);
root.setLeft(insertImageButton);
root.setRight(clipImageButton);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private void saveImage() {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Image File");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("PNG", "*.png"),
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("GIF", "*.gif"));
File file = fileChooser.showSaveDialog(null);
if (file != null) {
Image snapshot = canvas.snapshot(null, null);
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void clipImage() {
if (imageView != null && clipRect != null) {
Image image = imageView.getImage();
double scaleX = image.getWidth() / imageView.getFitWidth();
double scaleY = image.getHeight() / imageView.getFitHeight();
double x = clipRect.getX() * scaleX;
double y = clipRect.getY() * scaleY;
double w = clipRect.getWidth() * scaleX;
double h = clipRect.getHeight() * scaleY;
Image clippedImage = new Image(image.getPixelReader(), (int) x, (int) y, (int) w, (int) h);
imageView.setImage(clippedImage);
canvas.getChildren().remove(clipRect);
clipRect = null;
}
}
private final EventHandler<MouseEvent> insertImageEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.isSecondaryButtonDown()) {
canvas.getChildren().remove(imageView);
imageView = null;
}
}
};
private final EventHandler<MouseEvent> clipImageEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
clipRect.setX(event.getX());
clipRect.setY(event.getY());
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
clipRect.setWidth(event.getX() - clipRect.getX());
clipRect.setHeight(event.getY() - clipRect.getY());
} else if (event.getEventType() == MouseEvent.MOUSE_RELEASED) {
clipImage();
canvas.setCursor(Cursor.DEFAULT);
canvas.setOnMousePressed(null);
canvas.setOnMouseDragged(null);
canvas.setOnMouseReleased(null);
}
}
};
public static void main(String[] args) {
launch(args);
}
}
```
这个实现可以让你在界面上进行绘制、选择画笔颜色和粗细、插入图片和裁剪图片。请注意,这只是一个简单的示例,你可以根据你的需求进行修改和扩展。
阅读全文