Home Ask Login Register

Developers Planet

Your answer is one click away!

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() {
            public void mousePressed(MouseEvent event) {
                letsdraw = tempimage.createGraphics();
                Point panelPoint = event.getPoint();
                sX = panelPoint.x;
                sY = panelPoint.y;
            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)));

        myPanel.addMouseMotionListener(new MouseAdapter() {
            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)));


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() {
            public void run() {
                try {
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new BorderLayout());

Post Status

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


Leave an answer

Quote of the day: live life