我正在尝试用Java编写一个简单的绘图程序,我似乎无法理解不同类型的鼠标监听器及其运行方式。
我已经让我的鼠标按下了监听器,这样它就会创建一个新的实例,在拖动鼠标的同时将点添加到该行中,然后在释放鼠标时将这一行添加到我的线条的arraylist中重置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)
答案 0 :(得分:3)
问题是您需要注册一个MouseListener
来接收mousePressed
和mouseReleased
个事件。
添加以下内容可以解决您的问题。
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;
}
});