在 paint 方法中调用对象方法 Java

Calling object methods inside the paint method Java

抱歉我对 Java 图形的理解不佳。所以,我有这个绘画方法:

public void paint(Graphics window)
{
    window.setColor(Color.WHITE);
    window.fillRect(0,0,getWidth(), getHeight());
    window.setColor(Color.BLUE);
    window.drawRect(20,20,getWidth()-40,getHeight()-40);
    window.setFont(new Font("TAHOMA",Font.BOLD,18));
    window.drawString("CREATE YOUR OWN SHAPE!",40,40);
}

一切都很好,矩形和文本都显示出来了。

那我改成:

public void paint(Graphics window)
{
    window.setColor(Color.WHITE);
    window.fillRect(0,0,getWidth(), getHeight());
    window.setColor(Color.BLUE);
    window.drawRect(20,20,getWidth()-40,getHeight()-40);
    window.setFont(new Font("TAHOMA",Font.BOLD,18));
    window.drawString("CREATE YOUR OWN SHAPE!",40,40);

    sh.setX(sh.getX()+sh.getXSpeed());
    sh.setY(sh.getY()+sh.getYSpeed());
    sh.draw(window);

    if(!(sh.getX()>=10 && sh.getX()<=730))
        sh.setXSpeed(-sh.getXSpeed());

    if(!(sh.getY()>=10 && sh.getY()<=530))
        sh.setYSpeed(-sh.getYSpeed());
}

然后突然什么都没有出现。 (甚至没有之前出现的文本)"sh" 是我的形状 class 的一个实例。调用的方法是相当自我解释的(Setters 和 getters),但包含在最后以供参考。

为什么会这样?我还遇到了一系列终端错误,如果它有任何用处,我会在这里 post 它们。我的意思是,我不希望 setter 和 getter 导致事情消失或任何重大错误(请注意,当我也删除 "sh.draw(window)" 时,事情仍然会消失)。是不是在paint方法内部调用对象方法有问题?我能做什么呢?

方法供参考,不需要看懂代码:

   public int getX()
       {
        return xPos;
       }

       public int getY()
         {
            return yPos;
           }

           public int getXSpeed()
           {
            return xSpeed;
           }

           public int getYSpeed()
           {
            return ySpeed;
           }

           public void setX(int newX)
           {
            xPos=newX;
           }

           public void setY(int newY)
           {
            yPos=newY;
           }

           public void setXSpeed(int newX)
           {
            xSpeed=newX;
           }

           public void setYSpeed(int newY)
           {
            ySpeed=newY;
           }

        public void draw(Graphics window)
       {
          window.setColor(color);
          window.fillRect(xPos, yPos, width, height);
          if(color!=Color.BLUE)
            window.setColor(Color.BLUE);
          else
            window.setColor(Color.RED);
          int xDev=(width/10);
          int yDev=(height/10);
          int xDev2=width/8;
          int yDev2=height/8;

          window.fillOval(xPos+xDev, yPos+yDev, width-yDev*2, height-yDev*2);
          window.setColor(Color.BLACK);
          window.fillOval(xPos+xDev*2, yPos+yDev*2, width-xDev*4, height-yDev*4);
          xPos+=width/2;
          yPos+=height*2/3;
          int[] treeXPoints = {xPos,xPos-xDev*2,xPos-xDev,xPos+xDev,xPos+xDev*2};
          int[] treeYPoints = {yPos,yPos-yDev*2,yPos-yDev*3,yPos-yDev*3,yPos-yDev*2};
          Polygon present1 = new Polygon(treeXPoints,treeYPoints,treeXPoints.length);
          window.setColor(Color.YELLOW);
          window.fillPolygon(present1);
          xPos-=width/2;
          yPos-=height*2/3;
       }

终端错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at MovingShapePanel.paint(MovingShapePanel.java:48)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
    at javax.swing.RepaintManager.run(RepaintManager.java:808)
    at javax.swing.RepaintManager.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access00(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access0(EventQueue.java:103)
    at java.awt.EventQueue.run(EventQueue.java:694)
    at java.awt.EventQueue.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    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)

您的绘画方法第 48 行中出现 NullPointerException

我猜你的字段 sh 在你调用它的方法之前没有设置