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

时间:2015-02-04 19:50:59

标签: java swing graphics paint

很抱歉我对Java Graphics的不了解。所以,我有这种绘画方法:

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”是我的Shape类的一个实例。调用的方法是相当自我解释的(Setters和getters),但最后包括在内以供参考。

为什么会这样?我也遇到了一些终端错误,如果有任何用处我会在这里发布。我的意思是,我不希望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$3.run(RepaintManager.java:808)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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.access$1100(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.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)

1 个答案:

答案 0 :(得分:1)

您在绘画方法第48行中获得了NullPointerException

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