如何添加的mouseClicked脚本 [英] how to add mouseClicked to script

查看:174
本文介绍了如何添加的mouseClicked脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此时,floodFill算法,我想添加到此的mouseClicked,但我不知道如何becouse我有很多错误。

下面是我的code。我想要得到的x,y possition从和的mouseClicked给它此时,floodFill(图像,X,Y,黄色);

谁能帮助我?谢谢

 进口java.awt.Point中;
进口java.awt.image.BufferedImage中;
进口的java.io.File;
进口javax.imageio.ImageIO中;
进口javax.swing.ImageIcon中;
进口javax.swing.JLabel中;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;

类AnimationFiller
{
 //绘制一个黑色正方形的1个像素的笔宽
  静态无效drawSquare(BufferedImage的图像)
  {
    java.awt.Graphics2D中的GR =(java.awt.Graphics2D中)image.getGraphics();

    gr.setColor(新java.awt.Color中(0,0,0)); // 黑色
    gr.setStroke(新java.awt.BasicStroke中(1)); //设置钢笔的宽度为1个像素
    gr.drawRect(0,0,296,264); //(X,Y,W,H);
  }
  //实现了洪水填充算法
  公共静态无效此时,floodFill(BufferedImage的形象,诠释的x,INT Y,诠释填充颜色)
  {
    的java.util.ArrayList<点> examList =新的java.util.ArrayList<>();

    INT initialColor = image.getRGB(X,Y);
    examList.add(新点(X,Y));

    而(examList.size()大于0)
    {
      点p = examList.remove(0); //获取并删除第一个点的列表
      如果(image.getRGB(p.x,p.y)== initialColor)
      {
        X = p.x; Y = p.y;
        image.setRGB(X,Y,填充颜色); //填补当前像素

        examList.add(新的点(x-1,y))为; //检查西邻
        examList.add(新点(x + 1,y)); //检查东邻
        examList.add(新点(X,Y-1)); //检查北邻
        examList.add(新点(X,Y + 1)); //检查南邻
      }
    }

  }

  公共静态INT packRgb(INT读,诠释克,INT B)
  {
    回报(R * 256 + G)* 256 + B;
  }

  静态的JLabel _imageLabel;
  公共静态无效的主要(字串[] args)抛出异常
  {
    //从文件中读取bmp图像
    最终的BufferedImage图像= ImageIO.read(新文件(PICTURE.BMP));

    drawSquare(图像);
    最后的JLabel imageLabel =新的JLabel();
    _imageLabel = imageLabel; //使其全球
    imageLabel.setIcon(新的ImageIcon(图片));


    javax.swing.JFrame中的窗口=新javax.swing.JFrame中的();
    window.setResizable(假);
    window.setTitle(Kolorowanka);
    window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    window.add(imageLabel);

    window.pack();
    window.setVisible(真正的);

    //填充黄色图像
    最终诠释黄色= packRgb(255,255,0);
    imageLabel.addMouseListener(新的MouseListener(){
    @覆盖
    公共无效的mouseReleased(的MouseEvent E){
    }

    @覆盖
    公共无效鼠标pressed(的MouseEvent E){
    }

    @覆盖
    公共无效的mouseExited(的MouseEvent E){
    }

    @覆盖
    公共无效的mouseEntered(的MouseEvent E){
    }

    @覆盖
    公共无效的mouseClicked(的MouseEvent E){
        此时,floodFill(图像,e.getX(),e.​​getY(),黄色);
        imageLabel.setIcon(新的ImageIcon(图片));
    }
});

  }
}
 

解决方案

这是一个实现一个扫描线颜色填充算法,我写了几个月前的。

边界是一个堆栈, STARTX startY 是起点cordinates。 像素数组包含的所有像素的图像中的RGB值。使用 PixelGrabber都会源图像上获得的数组。

使用所有其他变量的暗示。

  boundaries.push(新点(STARTX,startY));
//现在我们有起始位置为在上述和下部线着色
//着色在正确的方向上只进行

而(!boundaries.isEmpty())//循环多次,直到没有起始位置被发现
{
    //现在开始着色
    点PT = boundaries.pop();
    //这是起点,朝着正确的方向前进着色
    INT X = pt.x;
    INT Y = pt.y;

    //第一次去的最左到不同颜色的像素被发现
    而(X GT; = 0)
    {
        如果(像素[Y *宽+ X] = pickPoint previousColor!)破;
        x--;
    }
    X ++;

    //现在一直延伸
    而(X< = WIDTH)
    {
        如果(像素[Y *宽+ X] == pickPoint previousColor)
            像素[Y *宽+ X] = colorToFill;
        其他
        {
            upperLineCountingStarted = FALSE;
            lowerLineCountingStarted = FALSE;
            打破;
        }

        如果(Y> 0安培;&安培; upperLineCountingStarted ==假放;&放大器;像素[(Y-1)*宽+ X] == pickPoint previousColor)
        {
            boundaries.push(新点(X,Y-1));
            upperLineCountingStarted = TRUE;
        }

        否则,如果(Y> 0安培;&安培; upperLineCountingStarted ==真放;&安培;!像素[(Y-1)*宽+ X] = pickPoint previousColor)
            upperLineCountingStarted = FALSE;

        如果(Y<身高和放大器;&安培; lowerLineCountingStarted ==假放;&放大器;像素[(Y + 1)*宽+ X] == pickPoint previousColor)
        {
            boundaries.push(新点(X,Y + 1));
            lowerLineCountingStarted = TRUE;
        }
        否则,如果(Y<身高和放大器;&安培; lowerLineCountingStarted ==真放;&安培;!像素[(Y + 1)*宽+ X] = pickPoint previousColor)
            lowerLineCountingStarted = FALSE;

        X ++;
    }
}
 

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

  }
}

解决方案

This is a implementation of a scanline flood fill algorithm which I wrote a couple of months ago.

boundaries is a stack, startX , startY are the cordinates of the starting point. pixels array contain the rgb value of all the pixels in the in the image. The array was obtained using PixelGrabber on the source image.

The use of all other variables are implied.

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

这篇关于如何添加的mouseClicked脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆