我正在编写一个小程序,当你点击屏幕时会出现一个米奇鼠标脸,当我正在编写代码以确保当你点击脸部时你可以拖动整个图像。米奇由三个FilledOval对象组成:face,leftEar和rightEar。每当我运行程序时它会给我带来错误,但applet实际上是应该的。这些是错误:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Mickey.onMousePress(Mickey.java:73)
at objectdraw.WindowControllerListener.mousePressed(WindowController.java:200)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
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:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
错误从第73行开始,我有onMousePress方法。 这是我的代码:
import objectdraw.*;
import java.awt.*;
public class Mickey extends WindowController
{
private static final int INSTR1_X = 50;
private static final int INSTR1_Y = 50;
private static final int INSTR2_X = INSTR1_X;
private static final int INSTR2_Y = INSTR1_Y + 20;
private static final int FACE_RADIUS = 50;
private static final int EAR_RADIUS = 30;
private static final int EAR_OFFSET = 50; // Center of each ear is this offset
// up and over (x and y) from center
// of face.
private static double X_POS = 0;
private static double Y_POS = 0;
private Text instrOne, instrTwo;
private FilledOval face, leftEar, rightEar;
private Location mousePoint, difference;
private static boolean isClicked = false;
private static boolean isOver = false;
public void begin()
{
instrOne = new Text( "Click to display a Mickey silhouette " +
"centered at the mouse click",
INSTR1_X, INSTR1_Y, canvas );
instrTwo = new Text( "Mouse press in any part of the image and " +
"drag to move image around.",
INSTR2_X, INSTR2_Y, canvas );
}
public void onMouseClick( Location point )
{
if ( !isClicked )
{
X_POS = ( point.getX() - (FACE_RADIUS / 2) );
Y_POS = ( point.getY() - (FACE_RADIUS / 2) );
mousePoint = new Location ( X_POS, Y_POS );
instrOne.hide();
instrTwo.hide();
face = new FilledOval( mousePoint, FACE_RADIUS, FACE_RADIUS, canvas);
leftEar = new FilledOval( (X_POS - (EAR_RADIUS / 2)),
(Y_POS - (EAR_RADIUS / 2)),
EAR_RADIUS, EAR_RADIUS, canvas );
rightEar = new FilledOval((X_POS + (EAR_OFFSET - (EAR_RADIUS / 2))) ,
(Y_POS - (EAR_RADIUS / 2)),
EAR_RADIUS, EAR_RADIUS, canvas );
isClicked = true;
}
}
public void onMousePress ( Location point )
{
if (face.contains ( point ) ||
leftEar.contains ( point ) ||
rightEar.contains ( point ) )
{
isOver = true;
}
}
public void onMouseRelease ( Location point )
{
isOver = false;
}
public void onMouseDrag ( Location point )
{
if ( isOver )
{
X_POS = ( point.getX() - (FACE_RADIUS / 2) );
Y_POS = ( point.getY() - (FACE_RADIUS / 2) );
face.moveTo( X_POS, Y_POS );
leftEar.moveTo( (X_POS - (EAR_RADIUS / 2)),
(Y_POS - (EAR_RADIUS / 2)) );
rightEar.moveTo( (X_POS + (EAR_OFFSET - (EAR_RADIUS / 2))),
(Y_POS - (EAR_RADIUS / 2)) );
}
}
public void main ( String[] args )
{
Mickey current = new Mickey();
current.begin();
}
}
答案 0 :(得分:1)
onMousePress
。它尝试使用尚未初始化的face
。 face
初始化onMouseClick
,单击鼠标时会调用face
。这意味着“按下并释放鼠标按钮”。您无法在初始化之前使用NullPointerException
(或任何字段),因此它会与face = new FilledOval( mousePoint, FACE_RADIUS, FACE_RADIUS, canvas);
leftEar = new FilledOval( (X_POS - (EAR_RADIUS / 2)),
(Y_POS - (EAR_RADIUS / 2)),
EAR_RADIUS, EAR_RADIUS, canvas );
rightEar = new FilledOval((X_POS + (EAR_OFFSET - (EAR_RADIUS / 2))) ,
(Y_POS - (EAR_RADIUS / 2)),
EAR_RADIUS, EAR_RADIUS, canvas );
崩溃。尝试移动此代码
onMousePress
到需要省略号时调用的方法(在使用它们的代码之前{{1}})。
答案 1 :(得分:0)
mousePressed在mouseClicked之前发生,因此您应该在onMousePressed
而不是onMouseClicked
初始化面部变量。
mousePressed :在组件上按鼠标按键时调用
mouseClicked :在组件上单击鼠标按钮(按下并释放)时调用。