Pirate
Pirate

Reputation: 77

JAVA drawing graphic

I want to draw a rectangle on an image when the mouse button is pressed and released. And this part works just fine. Now I want to be able to see the rectangle while I drag the mouse, what I get is lots of rectangles being drawn please help.

class ActionTemp implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        myPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent event) {
                letsdraw = tempimage.createGraphics();
                Point panelPoint = event.getPoint();
                sX = panelPoint.x;
                sY = panelPoint.y;
            }
            @Override
            public void mouseReleased(MouseEvent event) {
                letsdraw.draw(new Rectangle2D.Float(Math.min(sX, curX),
                        Math.min(sY, curY), Math.abs(sX - curX),
                        Math.abs(sY - curY)));
                letsdraw.dispose();
                myPanel.repaint();
            }
        });

        myPanel.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                Point panelPoint = event.getPoint();
                curX = panelPoint.x;
                curY = panelPoint.y;
                letsdraw.draw(new Rectangle2D.Float(Math.min(sX, curX),
                        Math.min(sY, curY), Math.abs(sX - curX),
                        Math.abs(sY - curY)));
                myPanel.repaint();
            }
        });
    }
}

Upvotes: 0

Views: 60

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Start by having a look at Painting in AWT and Swing and Performing Custom Painting.

The basic problem is, you painting directly to the image, which means, unless you have a separate copy, you're just compounding each successive rectangle on top of the last.

Instead, you want to paint each of them separately, something like...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
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.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SelectionExample {

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

    public SelectionExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Rectangle selection = new Rectangle();
        private Point clickPoint;

        private BufferedImage tempimage;

        public TestPane() {
            try {
                tempimage = ImageIO.read(new File("/Users/shane/Dropbox/MegaTokyo/thumnails/2.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            MouseAdapter ma = new MouseAdapter() {

                @Override
                public void mouseDragged(MouseEvent e) {
                    int minX = Math.min(e.getX(), clickPoint.x);
                    int minY = Math.min(e.getY(), clickPoint.y);
                    int maxX = Math.max(e.getX(), clickPoint.x);
                    int maxY = Math.max(e.getY(), clickPoint.y);

                    selection.x = minX;
                    selection.y = minY;
                    selection.width = maxX - minX;
                    selection.height = maxY - minY;
                    repaint();
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    clickPoint = new Point(e.getPoint());
                }

            };

            addMouseListener(ma);
            addMouseMotionListener(ma);
        }

        @Override
        public Dimension getPreferredSize() {
            return tempimage == null ? new Dimension(200, 200) : new Dimension(tempimage.getWidth(), tempimage.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - tempimage.getWidth()) / 2;
            int y = (getHeight() - tempimage.getHeight()) / 2;
            g2d.drawImage(tempimage, x, y, this);
            if (selection.width > 0 && selection.height > 0) {
                g2d.setColor(new Color(0, 0, 255, 64));
                g2d.fill(selection);
                g2d.setColor(Color.BLUE);
                g2d.draw(selection);
            }
            g2d.dispose();
        }
    }

}

Upvotes: 2

Related Questions