Add image on mouse click? Java applet

后端 未结 1 390
清酒与你
清酒与你 2021-01-17 05:19

How would I go and add an image on the mouse coordinates when the mouse clicks? I have looked at this :Adding Images on Mouse Click to JPanel

But I don\'t understand

相关标签:
1条回答
  • 2021-01-17 05:32

    Start by taking a look at Painting in AWT and Swing and Performing Custom Painting to understand how painting works in AWT/Swing.

    Then, take a look at 2D Graphics for more details about how you can use the Graphics class to paint things with.

    This is a really basic example which loads a single image and every time you click on the panel, moves it to that point.

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class DrawImage {
    
        public static void main(String[] args) {
            new DrawImage();
        }
    
        public DrawImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage image;
            private Point drawPoint;
    
            public TestPane() {
                try {
                    image = ImageIO.read(getClass().getResource("/SmallPony.png"));
                    addMouseListener(new MouseAdapter() {
    
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            drawPoint = new Point(e.getPoint());
                            repaint();
                        }
    
                    });
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (drawPoint != null) {
                    g2d.drawImage(image, drawPoint.x, drawPoint.y, this);
                }
                g2d.dispose();
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题