java 简单的绘图程序鼠标侦听器

java simple paint program mouse listeners

我正在尝试在 Java 中编写一个简单的绘画程序,但我似乎无法理解不同类型的鼠标侦听器及其功能。

我已将我的鼠标按下侦听器创建为一条线的新实例,在拖动鼠标时将点添加到该线,然后在鼠标被拖动时将该线添加到我的线数组列表中释放并重置 currentLine 变量的值,以便可以记录新的一行点。

这不是空指针异常的误解问题,我知道什么是空指针异常;由于我对鼠标听众的误解,我不明白为什么我会得到一个。如您所见,我的想法是按下鼠标时创建一条线的新实例,以便在拖动鼠标时可以添加任何点。

如果有人对我哪里出错有任何建议,我将不胜感激。非常感谢

我的程序包含以下内容类:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Canvas extends JPanel {

private ArrayList<Line> lines = new ArrayList<Line>();
private Line currentLine;

public Canvas() {

    this.init();
    this.frameInit();

}

private void init() {

    this.setPreferredSize(new Dimension(600,600));

    this.addMouseMotionListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {

            currentLine = new Line();

        }

        public void mouseDragged(MouseEvent e) {

            currentLine.points.add(e.getPoint());
            repaint();

        }

        public void mouseReleased(MouseEvent e) {

            if(!currentLine.points.isEmpty()) {
                lines.add(currentLine);
            }

            currentLine=null;

        }

    });

}

private void frameInit() {

    JFrame window = new JFrame("GPaint");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setContentPane(this);
    window.pack();
    window.setVisible(true);

}

public void paintComponent(Graphics g) {

    Graphics2D twoD = (Graphics2D) g;
    twoD.setColor(Color.RED);
    twoD.setStroke(new BasicStroke(6));

    if(!lines.isEmpty()) {

        for(Line l: lines) {

            if(l.points.size()>1) {

                for(int i=1; i<l.points.size()-1; i++) {

                    twoD.drawLine(l.points.get(i-1).x, l.points.get(i-1).y, l.points.get(i).x, l.points.get(i).y);

                }

            }

            if(l.points.size()==1){

                //0 index as there is only one point, so fill an oval
                twoD.fillOval(l.points.get(0).x-3, l.points.get(0).y-3, 6, 6);

            }

        }

    }

}

}

import java.awt.Point;
import java.util.ArrayList;

public class Line {

ArrayList<Point> points;

public Line() {

    points = new ArrayList<Point>();

}

}

这里是堆栈的结果:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Canvas.mouseDragged(Canvas.java:39)
at java.awt.Component.processMouseMotionEvent(Component.java:6581)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3342)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.awt.EventQueue.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

问题是您需要注册一个 MouseListener 来接收 mousePressedmouseReleased 事件。

添加以下内容将解决您的问题。

this.addMouseListener(new MouseAdapter() {

    @Override
    public void mousePressed(MouseEvent e) {
        currentLine = new Line();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (!currentLine.points.isEmpty()) {
            lines.add(currentLine);
        }
        currentLine = null;
    }
});