2013-03-06 48 views
0

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

http://i.imgur.com/Cywh8xU.png - 在此图像中,我点击的点是红色圆圈,比每行会找到2点起点和终点)。我显然没有画出所有的线条,但这是我拥有的基本概念。

我遇到的问题是图像最终看起来像这样,我不明白为什么。 http://i.imgur.com/IXHgEf2.jpg

这里是我发现所有的线,所以远代码:

公共类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); 
} 

}

这里是我的填充类:

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); 
    } 
} 

任何帮助,将不胜感激,谢谢。

+0

http://i.imgur.com/iA7XStz.jpg我无法发布另一个链接,但这是另一个当我点击已存在的行时发生的事例。 – 2013-03-06 05:29:32

回答

1

你需要改变你的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); 
     } 
    } 
} 

(我撒了几个= -signs到您的if条件允许你点击到图像的左上角还,和一对夫妇的+1 S和-1 s转换您while条件而定)

基本上下面描述的bug,会发生什么是你第一次后,while循环,你要么在图像的左边缘,其中如果一切正常,或者testPoint指向第一个不等于clickedColor的像素,则第二个while循环立即终止。

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

+0

谢谢,这个工作很好。 – 2013-03-06 16:37:43