我有2个班级Drawing.java
和Front.java
。我希望使用java.awt.graphics
类中的drawing
库创建形状,只有在front
类中调用该类的函数时才会创建形状。这是洞察力的代码。
Drawing.java
类:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Drawing extends JPanel {
Graphics g;
int x, y;
String name;
public Drawing(int x, int y, String name) {
this.name = name;
this.x = x;
this.y = y;
}
char[] n = name.toCharArray();
public void drawProcess(){
g.drawOval(x, y, 100, 100);
g.drawChars(n, 0, 40, x+5, y);
}
Front.java
类:
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static java.lang.Math.sqrt;
import javax.swing.JOptionPane;
public class Front extends javax.swing.JFrame {
.
.
.
canvas.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent evt){
System.out.print(" " + evt.getX() + " " + evt.getY() + " " + name);
Drawing dr = new Drawing(evt.getX(), evt.getY(), name);
dr.drawProcess();
}
});
运行Front
类时,出现以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dfd_analyzer.Drawing.<init>(Drawing.java:14)
at dfd_analyzer.Front$5.mouseClicked(Front.java:294)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
现在,根据我的理解,我想我在引用图形对象或该方向上的某些东西时遇到问题,或者我对正在发生的事情缺乏清楚的了解,将不胜感激。提前致谢。
更新:我能够创建drawing
类的实例,但是调用dr.drawProcess()
会导致nullpointer异常。我知道这是因为Graphics抽象类未正确引用。我是Swing的新手,真的可以在这里使用一些帮助。