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

时间:2014-10-22 15:33:34

标签: java swing events user-interface graphics

我正在制作一个悬停在网格上的程序会将图像放入悬停在其中的单元格中。目前我有它的工作,只有一个颜色填写,但我不知道如何制作它所以绘制图像。这是我的计划:

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

我想制作图像&#34; img&#34;成为像素&#34;但是,我对如何做到这一点很困惑,因为我不认为我可以使用 - 每个循环和Graphics2D。如果有人可以提供帮助,非常感谢!

2 个答案:

答案 0 :(得分:2)

定义ImageHolder类,它有2个字段形状和图像。

class ImageHolder {
  Shape shape;
  Image img;

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

你的网格应该是List。在init上,所有ImageHolders都有正方形和空图像。单击图像将分配给单击的持有者。

在paintComponent()中你只需要调用holder的paint()方法

答案 1 :(得分:2)

看一下这个。 您需要做的就是使用g2.fill(cell);

重新充分g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);

read drawimage api here

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

输出&GT;&GT;

enter image description here