-1

我正在编写一个小程序,当您单击屏幕时会出现一个米老鼠脸,目前我正在编写代码以确保当您单击脸部时可以拖动整个图像。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();
  }
}
4

2 回答 2

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在使用它们的代码之前)。

于 2013-10-07T18:05:32.957 回答
0

mousePressed 发生在 mouseClicked 之前,因此您应该在 inonMousePressed和 not中初始化 face 变量onMouseClicked

mousePressed :在组件上按下鼠标按钮时调用

mouseClicked :在组件上单击(按下和释放)鼠标按钮时调用。

于 2013-10-07T18:04:21.310 回答