我试图用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)
解决方法:
问题是您需要注册一个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;
}
});