繁体   English   中英

Java GUI在鼠标事件上绘制图像

[英]Java GUI drawing an image on mouse event

我正在编写一个程序,将鼠标悬停在网格上会将图像放入悬停的单元格中。 目前,我正在使用它,以便仅填充一种颜色,但是我不知道如何制作它,以便绘制图像。 这是我的程序:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Test
{
Image img=Toolkit.getDefaultToolkit().getImage("img.gif");
public static void main(String[]args)
{
    new Test();
}
public Test()
{
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new PixelGrid());
    frame.setSize(new Dimension(364,357));
    frame.setVisible(true);
}
public class PixelGrid extends JPanel
{
    private List<Shape>grid,square;
    public PixelGrid()
    {
        grid=new ArrayList<>();
        square=new ArrayList<>();
        addMouseMotionListener(new MouseAdapter()
        {
            public void mouseMoved(MouseEvent e)
            {
                for(Shape shape:grid)
                {
                    if(shape.contains(e.getPoint()))
                    square.add(shape);
                }
                repaint();
            }
        }
        );
        for(int row=0;row<5;row++)
        {
            for(int col=0;col<5;col++)
            grid.add(new Rectangle(col*25+112,row*25+50,25,25));
        }
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawLine(112,50,237,50);
        g.drawLine(112,75,237,75);
        g.drawLine(112,100,237,100);
        g.drawLine(112,125,237,125);
        g.drawLine(112,150,237,150);
        g.drawLine(112,175,237,175);
        g.drawLine(112,50,112,175);
        g.drawLine(137,50,137,175);
        g.drawLine(162,50,162,175);
        g.drawLine(187,50,187,175);
        g.drawLine(212,50,212,175);
        g.drawLine(237,50,237,175);
        Graphics2D g2=(Graphics2D)g;
        for(Shape cell:square)
        g2.fill(cell);
    }
}
}

我想使图像“ img”成为填充的“像素”。但是,我对如何执行此操作感到困惑,因为我认为我不能使用for-each循环和Graphics2D。 如果有人可以帮助,非常感谢!

定义ImageHolder类,该类具有2个字段shape和image。

class ImageHolder {
  Shape shape;
  Image img;

  public void paint(Graphics2D g2) {
    if (img!=null) {
      g2.drawImage(img);
    }
    else {
      g2.fill(shape);
    }
  }
}

您的网格应为列表。 初始化时,所有ImageHolders都有正方形和空图像。 单击时将图像分配给单击的持有人。

在paintComponent()中,您只需调用持有者的paint()方法

看一看。 您要做的就是重新g2.fill(cell); g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);

在这里阅读drawimage api

for (Shape cell : square) {
     //g2.fill(cell);
     g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
     System.out.println(cell.getBounds().x);
}

但是在这种情况下,请使用Dimention list2D list代替Shape

完整的代码

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

public class Test {

    Image img = Toolkit.getDefaultToolkit().getImage("img.gif");

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PixelGrid());
        frame.setSize(new Dimension(364, 357));
        frame.setVisible(true);
    }

    public class PixelGrid extends JPanel {
     int x=0;
     int y=0;
        private List<Shape> grid, square;

        public PixelGrid() {
            grid = new ArrayList<>();
            square = new ArrayList<>();
            addMouseMotionListener(new MouseAdapter() {
                public void mouseMoved(MouseEvent e) {

                    for (Shape shape : grid) {
                        if (shape.contains(e.getPoint())) {
                            square.add(shape);
                        }
                    }
                    repaint();
                }
            }
            );
            for (int row = 0; row < 5; row++) {
                for (int col = 0; col < 5; col++) {
                    grid.add(new Rectangle(col * 25 + 112, row * 25 + 50, 25, 25));

                }
            }
        }

        public void paintComponent(Graphics g) {

            System.out.println(x);
            g.drawLine(112, 50, 237, 50);
            g.drawLine(112, 75, 237, 75);
            g.drawLine(112, 100, 237, 100);
            g.drawLine(112, 125, 237, 125);
            g.drawLine(112, 150, 237, 150);
            g.drawLine(112, 175, 237, 175);
            g.drawLine(112, 50, 112, 175);
            g.drawLine(137, 50, 137, 175);
            g.drawLine(162, 50, 162, 175);
            g.drawLine(187, 50, 187, 175);
            g.drawLine(212, 50, 212, 175);
            g.drawLine(237, 50, 237, 175);
            Graphics2D g2 = (Graphics2D) g;
            //g2.drawImage(img,x,y,null);
            for (Shape cell : square) {
                //g2.fill(cell);
                g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
                System.out.println(cell.getBounds().x);
            }
        }
    }
}

输出>>

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM