.drawLine() issues and buffered image

前端 未结 3 830
时光取名叫无心
时光取名叫无心 2020-12-11 10:11

I have a paint programme and i have all the buttons and sliders done however i am having a problem with the actual painting itself. When I drag the cursor across the scre

3条回答
  •  庸人自扰
    2020-12-11 10:46

    • Just to justify my comment, I am adding this answer, though a slight change from the comment is here, which being the use of mousePressed(...) instead of mouseClicked(...).
    • One more addition being, since you wanted the Graphics2D object of the BufferedImage so instead of using getGraphics() always use createGraphics() which returns the Graphics2D object, hence you don't really have to worry about the Cast thingy in this.

      Please do have a look at the example below :

    ======================

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
    import java.net.URL;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    
    public class PaintingExample {
    
        private BufferedImage bImage;
        private ImageIcon image;
        private JLabel imageLabel;
        private int xClicked = 0;
        private int yClicked = 0;
        private int xDragged = 0;
        private int yDragged = 0;
    
        private MouseAdapter mouseListener = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                xClicked = me.getX();
                yClicked = me.getY();
            }
    
            @Override
            public void mouseDragged(MouseEvent me) {
                xDragged = me.getX();
                yDragged = me.getY();
    
                Graphics2D g2 = bImage.createGraphics();
                g2.setColor(Color.WHITE);
                BasicStroke stroke=new BasicStroke(30);
                g2.setStroke(stroke);
                g2.drawLine(xClicked, yClicked, xDragged, yDragged);
                g2.dispose();
                imageLabel.setIcon(new ImageIcon(bImage));
            }
        };
    
        public PaintingExample() {
            try {
                bImage = ImageIO.read(new URL(
                        "http://i.imgur.com/fHiBMwI.jpg"));
                image = new ImageIcon(bImage);          
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        private void displayGUI() {
            JFrame frame = new JFrame("Painting on Image");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            imageLabel = new JLabel(image);
            imageLabel.addMouseListener(mouseListener);
            imageLabel.addMouseMotionListener(mouseListener);
    
            contentPane.add(imageLabel);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new PaintingExample().displayGUI();
                }
            });
        }
    }
    

提交回复
热议问题