0

我有floodfill算法,我想添加到这个mouseClicked,但我不知道如何,因为我有很多错误。

这是我的代码。我想从 mouseClicked 获取 x,y 位置并将其提供给“floodFill(image,x,y, yellow);”

谁能帮我?谢谢

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

class AnimationFiller
{
 // draw a black square with a pen width of 1 pixels
  static void drawSquare(BufferedImage image)
  {
    java.awt.Graphics2D gr=(java.awt.Graphics2D) image.getGraphics(); 

    gr.setColor(new java.awt.Color(0,0,0));  // black
    gr.setStroke(new java.awt.BasicStroke(1));  // set pen width to 1 pixel
    gr.drawRect(0, 0, 296, 264);  // (x,y,w,h);
  }
  // implements the flood fill algorithm
  public static void floodFill(BufferedImage image, int x,int y, int fillColor)
  {
    java.util.ArrayList<Point> examList=new java.util.ArrayList<>();

    int initialColor=image.getRGB(x,y);
    examList.add(new Point(x,y));

    while (examList.size()>0)
    {
      Point p = examList.remove(0);  // get and remove the first point in the list
      if (image.getRGB(p.x,p.y)==initialColor) 
      {
        x = p.x;  y = p.y;
        image.setRGB(x, y, fillColor);  // fill current pixel

        examList.add(new Point(x-1,y));        // check west neighbor
        examList.add(new Point(x+1,y));        // check east neighbor
        examList.add(new Point(x,y-1));        // check north neighbor
        examList.add(new Point(x,y+1));        // check south neighbor
      }
    }

  }

  public static int packRgb(int r,int g,int b)
  {
    return (r*256+g)*256+b;
  }

  static JLabel _imageLabel;
  public static void main(String[] args) throws Exception
  {
    // read bmp image from file
    final BufferedImage image = ImageIO.read(new File("picture.bmp"));

    drawSquare(image);
    final JLabel imageLabel = new JLabel();
    _imageLabel = imageLabel;  // make it global
    imageLabel.setIcon(new ImageIcon(image));


    javax.swing.JFrame window = new javax.swing.JFrame();
    window.setResizable(false);
    window.setTitle("Kolorowanka");
    window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    window.add(imageLabel);

    window.pack();
    window.setVisible(true);

    // fill the image with yellow color
    final int yellow = packRgb(255,255,0);
    imageLabel.addMouseListener(new MouseListener() {
    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        floodFill(image, e.getX(), e.getY(), yellow);
        imageLabel.setIcon(new ImageIcon(image));
    }
});

  }
}
4

2 回答 2

0

您可以通过这种方式添加mouseClicked事件:

imageLabel.addMouseListener(new MouseListener() {
    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        floodFill(image, e.getX(), e.getY(), yellow);
    }
});

如果要使用匿名类之外的某些字段,则必须将其定义为final。例如:

final BufferedImage image = ...

final int yellow = packRgb(255, 255, 0);

请在编写项目的下一部分之前阅读有关最终字段的信息。示例来源:

于 2013-02-03T13:30:52.467 回答
0

这是我几个月前编写的扫描线洪水填充算法的实现。

boundaries是一个栈,startXstartY是起点的坐标。 pixels数组包含图像中所有像素的 rgb 值。该阵列是PixelGrabber在源图像上使用的。

暗示使用所有其他变量。

boundaries.push(new Point(startX,startY));
//Now we have the starting positions for coloring in the above and lower lines
//coloring is done in right directions only

while(!boundaries.isEmpty()) //Loop as many times till no starting positions are found
{
    //Now begin coloring
    Point pt=boundaries.pop();
    //This is the starting point for coloring towards right direction
    int x=pt.x;
    int y=pt.y;

    //First go as far left as possible until a different colored pixel is found
    while(x>=0)
    {
        if(pixels[y*WIDTH+x]!=pickPointPreviousColor) break;
        x--;
    }
    x++;

    //Now extend right
    while (x<=WIDTH)
    {
        if(pixels[y*WIDTH+x]==pickPointPreviousColor)
            pixels[y*WIDTH+x]=colorToFill;
        else
        {
            upperLineCountingStarted = false;
            lowerLineCountingStarted=false;
            break;
        }

        if(y>0 && upperLineCountingStarted==false && pixels[(y-1)*WIDTH+x]==pickPointPreviousColor)
        {
            boundaries.push(new Point(x,y-1));
            upperLineCountingStarted=true;
        }

        else if(y>0 && upperLineCountingStarted==true && pixels[(y-1)*WIDTH+x]!=pickPointPreviousColor)
            upperLineCountingStarted=false;

        if(y<HEIGHT && lowerLineCountingStarted==false && pixels[(y+1)*WIDTH+x]==pickPointPreviousColor)
        {
            boundaries.push(new Point(x,y+1));
            lowerLineCountingStarted=true;
        }
        else if(y<HEIGHT && lowerLineCountingStarted==true && pixels[(y+1)*WIDTH+x]!=pickPointPreviousColor)
            lowerLineCountingStarted=false;

        x++;
    }
}
于 2013-02-03T14:19:24.387 回答