Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - how to add mouseClicked to script

I have floodfill algorithm and I want add to this mouseClicked, but I dont know how becouse I have many errors.

Here is my code. I want get the x,y possition from mouseClicked and give it to "floodFill(image,x,y, yellow);"

Can anyone help me? thanks

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

  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can add mouseClicked event in this way:

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

If you want use some fields from outside of the anonymous class you have to define it as final. For example:

final BufferedImage image = ...

And

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

Please, read about final field before writing next parts of your project. Example sources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...