如何将mouseClicked添加到脚本中

时间:2013-02-03 13:14:24

标签: java algorithm mouselistener flood-fill onmouseclick

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

这是我的代码。我想从mouseClicked获取x,y possition并将其赋予“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));
    }
});

  }
}

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分: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++;
    }
}