gpt4 book ai didi

java - 使用线条创建填充桶工具

转载 作者:行者123 更新时间:2023-12-01 14:47:51 24 4
gpt4 key购买 nike

我想使用我拥有的线条类为我的程序制作一个填充桶工具。我的填充桶会记录填充一个区域所需绘制的所有线条,例如:

/image/FJx3j.png - 在此图像中,我点击的点是红色圆圈,它会为每条线找到 2 个点(起点和终点)。我显然没有画出所有的线条,但这就是我所拥有的基本概念。

我遇到的问题是图像最终看起来像这样,但我不明白为什么。 /image/1o1Sb.jpg

这是我查找到目前为止所有行的代码:

公共(public)类 FillBucket{

private BufferedImage image;
private Fill currentFill;
private Line currentLine;

private Queue<Point> pointsToVisit;

@Override
public void mousePressed(Point point) {
super.mousePressed(point);

if(pointIsOnCanvas(point)) {
image = new BufferedImage(getCanvas().getPreferredSize().width, getCanvas().getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
getCanvas().paint(g2);

currentFill = new Fill(getColor());
pointsToVisit = new LinkedList<>();
createPoints(point, image.getRGB(point.x, point.y));

getCanvas().addDrawable(currentFill);
getCanvas().repaint();
}
}

private void createPoints(Point clickedPoint, int clickedColor) {
pointsToVisit.add(clickedPoint);

while(!pointsToVisit.isEmpty()) {
Point testPoint = pointsToVisit.poll();

if(testPoint.x > 0 && testPoint.x < image.getWidth() && testPoint.y > 0 && testPoint.y < image.getHeight()
&& image.getRGB(testPoint.x, testPoint.y) == clickedColor) {
while(testPoint.x > 0 && image.getRGB(testPoint.x, testPoint.y) == clickedColor) {
testPoint.x--;
}
currentLine = new Line(getColor(), 5);
currentLine.addPoint(new Point(testPoint));
while(testPoint.x < image.getWidth() && image.getRGB(testPoint.x, testPoint.y) == clickedColor) {
pointsToVisit.add(new Point(testPoint.x, testPoint.y+1));
pointsToVisit.add(new Point(testPoint.x, testPoint.y-1));

image.setRGB(testPoint.x, testPoint.y, getColor().getRGB());
testPoint.x++;
}
currentLine.addPoint(new Point(testPoint));
currentFill.addLine(currentLine);
}
}
}

}

这是我的线路类的基础知识:

public class Line {
private List<Point> points;
private int width;

/**
* Create a Line
* @param color - The color of the line
* @param width - The width of the line
*/
public Line(Color color, int width){
points = new LinkedList<>();
setColor(color);
setWidth(width);
}

/**
* Add a Point to the Line
* @param p - The Point to add to the Line
*/
public void addPoint(Point p) {
points.add(p);
}

@Override
public void draw(Graphics2D g2) {
super.draw(g2);
g2.setStroke(new BasicStroke(getWidth(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
for(int i=1; i<points.size(); i++)
g2.drawLine(points.get(i-1).x, points.get(i-1).y, points.get(i).x, points.get(i).y);
}

}

这是我的 Fill 类:

public class Fill{

List<Line> lines;

/**
* Create a Fill
* @param color - The Color of the Fill
*/
public Fill(Color color){
lines = new LinkedList<>();
setColor(color);
}

/**
* Add a Line to the Fill
* @param l - The Line to add
*/
public void addLine(Line l) {
lines.add(l);
}

@Override
public void draw(Graphics2D g2) {
super.draw(g2);
for(Line l: lines) {
l.draw(g2);
}
}

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

您需要将 createPoints(...) 函数更改为如下所示:

private void createPoints(Point clickedPoint, int clickedColor) {
pointsToVisit.add(clickedPoint);

while(!pointsToVisit.isEmpty()) {
Point testPoint = pointsToVisit.poll();

if(testPoint.x >= 0 && testPoint.x < image.getWidth() && testPoint.y >= 0 && testPoint.y < image.getHeight()
&& image.getRGB(testPoint.x, testPoint.y) == clickedColor) {
while(testPoint.x > 0 && image.getRGB(testPoint.x-1, testPoint.y) == clickedColor) {
testPoint.x--;
}
currentLine = new Line(getColor(), 5);
currentLine.addPoint(new Point(testPoint));

pointsToVisit.add(new Point(testPoint.x, testPoint.y+1));
pointsToVisit.add(new Point(testPoint.x, testPoint.y-1));

image.setRGB(testPoint.x, testPoint.y, getColor().getRGB());

while(testPoint.x < image.getWidth()-1 && image.getRGB(testPoint.x+1, testPoint.y) == clickedColor) {
pointsToVisit.add(new Point(testPoint.x, testPoint.y+1));
pointsToVisit.add(new Point(testPoint.x, testPoint.y-1));

image.setRGB(testPoint.x, testPoint.y, getColor().getRGB());
testPoint.x++;
}
currentLine.addPoint(new Point(testPoint));
currentFill.addLine(currentLine);
}
}
}

(我在 if 条件中撒了几个 = 符号,以便您也可以单击图像的左上角,还有几个 +1-1 到您的 while 条件中以修复接下来描述的错误)

基本上,在第一个 while 循环之后发生的情况是,您要么位于图像的左边缘,在这种情况下一切正常,要么您的 testPoint 指向第一个不是的像素等于 clickedColor ,在这种情况下,您的第二个 while 循环将立即终止。

顺便说一句:如果您单击已设置为填充颜色的点(即,如果 clickedColor == getColor()),您的代码可能会锁定。

关于java - 使用线条创建填充桶工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com