在Java中的JFrame上拖动矩形

发布于 2024-07-27 20:21:32 字数 281 浏览 8 评论 0原文

我想根据 mousedrag 事件绘制矩形。 如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增大或减小。 我有以下代码。

在下面的代码中,我使用 SelectionArea 类,它扩展了我正在其上执行绘图操作的画布。 我在此类中使用图像变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即绘制小程序的内容),

但如果我绘制第一个矩形,则代码工作正常。 如果我开始绘制第二个矩形,则先前绘制的矩形就会消失。 我希望先前绘制的矩形出现在屏幕上,

请告诉我如何解决这个问题。

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

in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)

but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen

plz tell me how to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2024-08-03 20:21:32

您需要做的是将之前绘制的矩形保存在某种数据结构中,以便稍后可以再次绘制。

这段代码(抱歉长度太长,将执行与您所描述的类似的操作。
要使用它,只需将 JPanel 放入 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);
    }
}

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

This code (sorry about the length, will do something similar to what you are describing.
To use it, just slap the JPanel inside of a 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);
    }
}
靑春怀旧 2024-08-03 20:21:32

自定义绘画方法展示了执行此操作的两种技术。

Custom Painting Approaches shows two techniques for doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文