我正在尝试用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$1.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.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.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$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.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)