Java Swing通过鼠标单击和拖动绘制线条 [英] Java Swing draw lines with mouse click and drag

查看:1130
本文介绍了Java Swing通过鼠标单击和拖动绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想带回一个之前提出过的问题:鼠标移动时的java绘制线

I want to bring back a question that was asked before: java draw line as the mouse is moved

我想在我的应用程序中添加一个功能,允许用户绘制一条直线通过在起始位置单击鼠标并在结束位置释放它。该线应在鼠标移动时移动,直到最终释放;类似于使用Microsoft Paint应用程序绘制线条的方式。

"I would like to add a feature to my application which allows the user to draw a straight line by clicking the mouse at the start location and releasing it at the end location. The line should move as the mouse moves until it is finally released; similar to the way that a line can be drawn using the Microsoft Paint application.

如何实现这一点,以便在移动线条时重新绘制线条而不重新绘制可能已在该矩形区域中绘制的其他内容?

How can implement this so that the line is repainted as it moves without repainting other things that may already be drawn in that rectangular area?"

问题是:如何在旧线条的情况下绘制多条线?

这是适用于我的代码,但是上一行会在您绘制一个新行时被删除:

This is the code that works for me, but the previous line gets erased as soon as you draw a new one:

public static void main(String args[]) throws Exception {
    JFrame f = new JFrame("Draw a Red Line");
    f.setSize(300, 300);
    f.setLocation(300, 300);
    f.setResizable(false);
    JPanel p = new JPanel() {
        Point pointStart = null;
        Point pointEnd   = null;
        {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    pointStart = e.getPoint();
                }

                public void mouseReleased(MouseEvent e) {
                    pointStart = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    pointEnd = e.getPoint();
                }

                public void mouseDragged(MouseEvent e) {
                    pointEnd = e.getPoint();
                    repaint();
                }
            });
        }
        public void paint(Graphics g) {
            super.paint(g);
            if (pointStart != null) {
                g.setColor(Color.RED);
                g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
            }
        }
    };
    f.add(p);
    f.setVisible(true); 
}


推荐答案


这是适用于我的代码,但是一旦你绘制一个新代码就删除了前一行:

This is the code that works for me, but the previous line gets erased as soon as you draw a new one:

两种常见方法:


  1. 保持要绘制的对象的ArrayList。然后每次组件需要重新绘制时,paintComponent()方法重新绘制所有对象

  1. Keep an ArrayList of objects to paint. Then the paintComponent() method repaints all the objects each time the component needs to repaint itself

绘制到BufferImage上,然后绘制BufferedImage。

Paint onto a BufferImage and then just paint the BufferedImage.

查看自定义绘画方法,以获取这两种方法的工作示例。

Check out Custom Painting Approaches for a working example of both of these approaches.

这篇关于Java Swing通过鼠标单击和拖动绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆