Drag a rectangle on a JFrame in Java

I want to draw a rectangle based on the mousedrag event. if the user drags the mouse, then the rectangle on the applet should increase or decrease based on the current coordinates of the mouse. I have the following code.

in the following code, I use the SelectionArea class, which extends the canvas on which I perform the drawing operation. I use the image variable in this class for double buffering to reduce flicker and maintain the previous state of the applet (i.e. draw the contents of the applet)

but the code works fine if I draw the first rectangle. if I start drawing a second rectangle, the previously disappearing rectangle disappears. I want the previously drawn rectangle to be on the screen

plz tell me how to solve this.

+1
source share
2 answers

What you need to do is save the previously drawn rectangle in some kind of data structure, so you can do it again later.

This code (sorry for the length, do something similar to what you describe). To use it, just click JPanelinside JFrame.

public class DrawPane extends JPanel {

    private List<DrawnShape> drawings;
    private DrawnShape curShape;

    public DrawPane() {
        drawings = new ArrayList<DrawnShape>();
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(300, 300));
        addMouseListener(clickListener);
        addMouseMotionListener(moveListener);
    }

    @Override
    protected void paintComponent(Graphics g2) {
        super.paintComponent(g2);
        Graphics2D g = (Graphics2D) g2;
        for (DrawnShape s : drawings) {
            s.draw(g);
        }
        g.setColor(Color.BLACK);
        g.setStroke(new BasicStroke(2));

        if (curShape == null)
            return;
        curShape.draw(g);
    }

    private MouseListener clickListener = new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            curShape = new DrawnShape(e.getPoint(), e.getPoint());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            drawings.add(new DrawnShape(curShape.getClickP(), e.getPoint()));
            curShape = null;
        }
    };

    private MouseMotionListener moveListener = new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
            curShape = new DrawnShape(curShape.getClickP(), e.getPoint());
            repaint();
        }
        @Override
        public void mouseMoved(MouseEvent e) {
        }
    };
}

class DrawnShape {

    private Point p1, p2;

    public DrawnShape(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getClickP() {
        return p1;
    }

    public void draw(Graphics2D g) {
        g.drawLine(p1.x, p1.y, p2.x, p1.y);
        g.drawLine(p1.x, p1.y, p1.x, p2.y);
        g.drawLine(p2.x, p2.y, p2.x, p1.y);
        g.drawLine(p2.x, p2.y, p1.x, p2.y);
    }
}
+3
source

Custom drawing approaches show two methods for this.

0
source

All Articles