我正在编写一个小程序,当您单击屏幕时会出现一个米老鼠脸,目前我正在编写代码以确保当您单击脸部时可以拖动整个图像。Mickey 由三个 FilledOval 对象组成:face、leftEar 和 rightEar。每当我运行程序时,它都会给我错误,但是小程序实际上可以正常工作。这些是错误:
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();
}
}