Pirate February 2016

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

Answers


MadProgrammer February 2016

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

Post Status

Asked in February 2016
Viewed 3,803 times
Voted 5
Answered 1 times

Search




Leave an answer