How to draw on top of an image in Java?

后端 未结 3 1348
既然无缘
既然无缘 2021-01-16 16:39

I used JFrame to import and display an image, and used mousemotionlistener to detect the mouse clicks, and I want to be able to draw on top of the image. I want to be able t

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 17:06

    I would highly recommend that you start by having a read through Performing Custom Painting and the 2D Graphics Trail, they will provide you with a starting point.

    There are a number of ways you might achieve this, this example simply keeps track of the click points and draws dots over the top of the image

    import java.awt.Color;
    import java.awt.Dimension;
    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.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private List points;
            private BufferedImage image;
    
            public TestPane() {
                points = new ArrayList<>(25);
                try {
                    image = ImageIO.read(new File("/Users/shanewhitehead/Desktop/Screen Shot 2017-03-09 at 1.55.18 pm.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        points.add(e.getPoint());
                        repaint();
                    }                
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return image == null ? new Dimension(200, 200) : new Dimension(image.getWidth(), image.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (image != null) {
                    g2d.drawImage(image, 0, 0, this);
                }
                g2d.setColor(Color.RED);
                for (Point p : points) {
                    g2d.fillOval(p.x - 4, p.y - 4, 8, 8);
                }
                g2d.dispose();
            }
    
        }
    
    }
    

    This example draws the dots directly to the image itself...

    import java.awt.Color;
    import java.awt.Dimension;
    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.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage image;
    
            public TestPane() {
                try {
                    image = ImageIO.read(new File("/Users/shanewhitehead/Desktop/Screen Shot 2017-03-09 at 1.55.18 pm.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (image != null) {
                            Point p = e.getPoint();
                            Graphics2D g2d = image.createGraphics();
                            g2d.setColor(Color.RED);
                            g2d.fillOval(p.x - 4, p.y - 4, 8, 8);
                            g2d.dispose();
                            repaint();
                        }
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return image == null ? new Dimension(200, 200) : new Dimension(image.getWidth(), image.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(image, 0, 0, this);
                g2d.dispose();
            }
    
        }
    
    }
    

    In both cases, they simply make use of the Graphics2D API

提交回复
热议问题