java编写一个颜料桶工具:可以用当前颜色填充封闭区域。
时间: 2023-07-15 14:15:55 浏览: 72
这个问题需要使用图形界面库来实现,我选择使用JavaFX。
首先,我们定义一个颜料桶工具类 `PaintBucketTool`,其中包含以下属性:
- `canvas`:绘图区域
- `color`:当前颜色
- `image`:用于绘制的图像
- `graphicsContext`:图形上下文
```java
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
public class PaintBucketTool {
private Canvas canvas;
private Color color;
private Image image;
private GraphicsContext graphicsContext;
public PaintBucketTool(Canvas canvas, Color color) {
this.canvas = canvas;
this.color = color;
this.image = new Image("file:background.png");
this.graphicsContext = canvas.getGraphicsContext2D();
}
}
```
接下来,我们需要实现一个 `fill` 方法,用于填充封闭区域。我们使用一个递归算法来实现。
```java
public void fill(int x, int y) {
// 获取当前像素的颜色
Color pixelColor = image.getPixelReader().getColor(x, y);
// 如果当前像素的颜色与目标颜色相同,则直接返回
if (pixelColor.equals(color)) {
return;
}
// 如果当前像素的颜色与起点像素的颜色不同,则直接返回
if (!pixelColor.equals(image.getPixelReader().getColor(0, 0))) {
return;
}
// 将当前像素的颜色设置为目标颜色
graphicsContext.getPixelWriter().setColor(x, y, color);
// 递归填充相邻的像素
if (x > 0) {
fill(x - 1, y);
}
if (x < canvas.getWidth() - 1) {
fill(x + 1, y);
}
if (y > 0) {
fill(x, y - 1);
}
if (y < canvas.getHeight() - 1) {
fill(x, y + 1);
}
}
```
最后,我们需要将颜料桶工具绑定到画布上,并添加事件监听器。
```java
public void bind() {
canvas.setOnMouseClicked(event -> {
// 获取点击的像素坐标
int x = (int) event.getX();
int y = (int) event.getY();
// 使用当前颜色填充封闭区域
fill(x, y);
});
}
```
完整代码如下:
```java
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
public class PaintBucketTool {
private Canvas canvas;
private Color color;
private Image image;
private GraphicsContext graphicsContext;
public PaintBucketTool(Canvas canvas, Color color) {
this.canvas = canvas;
this.color = color;
this.image = new Image("file:background.png");
this.graphicsContext = canvas.getGraphicsContext2D();
}
public void fill(int x, int y) {
// 获取当前像素的颜色
Color pixelColor = image.getPixelReader().getColor(x, y);
// 如果当前像素的颜色与目标颜色相同,则直接返回
if (pixelColor.equals(color)) {
return;
}
// 如果当前像素的颜色与起点像素的颜色不同,则直接返回
if (!pixelColor.equals(image.getPixelReader().getColor(0, 0))) {
return;
}
// 将当前像素的颜色设置为目标颜色
graphicsContext.getPixelWriter().setColor(x, y, color);
// 递归填充相邻的像素
if (x > 0) {
fill(x - 1, y);
}
if (x < canvas.getWidth() - 1) {
fill(x + 1, y);
}
if (y > 0) {
fill(x, y - 1);
}
if (y < canvas.getHeight() - 1) {
fill(x, y + 1);
}
}
public void bind() {
canvas.setOnMouseClicked(event -> {
// 获取点击的像素坐标
int x = (int) event.getX();
int y = (int) event.getY();
// 使用当前颜色填充封闭区域
fill(x, y);
});
}
}
```
使用方法:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
// 创建画布
Canvas canvas = new Canvas(800, 600);
// 创建颜料桶工具
PaintBucketTool paintBucketTool = new PaintBucketTool(canvas, Color.RED);
// 绑定工具到画布上
paintBucketTool.bind();
// 创建场景
StackPane root = new StackPane(canvas);
Scene scene = new Scene(root, 800, 600);
// 显示窗口
primaryStage.setTitle("Paint Bucket Tool");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```