尝试使用if语句绘制图形时出现错误

时间:2020-03-05 21:39:08

标签: java swing if-statement graphics nullpointerexception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SnakeV2.ObjectGeneration.paintApple(ObjectGeneration.java:56)
    at SnakeV2.SnakeTestNewGen.paintComponent(SnakeTestNewGen.java:91)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
public void paintApple(Graphics g) {
        xStone = xApple;
        yStone = yApple;

        if (snake.getLength() >= 0 && snake.getScore() <= 100) {
            g.fillRoundRect(xApple, yApple, size, size, 10, 10);
        } else if (snake.getLength() >= 10 && snake.getScore() >= 110) {
            if (num == 0) {
                g.fillRoundRect(xApple, yApple, size, size, 10, 10);
                g.fillRoundRect(xStone + 10, yStone, size, size, 10, 10);
            } else if (num == 1) {
                g.fillRoundRect(xApple, yApple, size, size, 10, 10);
                g.fillRoundRect(xStone - 10, yStone, size, size, 10, 10);
            } else if (num == 2) {
                g.fillRoundRect(xApple, yApple, size, size, 10, 10);
                g.fillRoundRect(xStone, yStone + 10, size, size, 10, 10);
            } else if (num == 3) {
                g.fillRoundRect(xApple, yApple, size, size, 10, 10);
                g.fillRoundRect(xStone, yStone - 10, size, size, 10, 10);
            }
        }
    }
if (newGen.getNum() == 0 || newGen.getNum() == 1 || newGen.getNum() == 2) {
            g.setColor(Color.RED);
            newGen.paintApple(g);
}

好吧,基本上,我在蛇游戏中添加了一些东西,我只是想测试一下我可以增加多少,然后再感到无聊并继续进行项目。

我创建了一个新的对象生成类,该类使我可以创建一个新的计时器,以便我可以以与主游戏类不同的延迟进行更新,以便可以在一定时间后使食物变得绝望。我还想添加石头(如果头部撞到游戏结束),将在达到一定得分+尾巴长度后生成。我生成一个介于(0-3)之间的随机数,并根据我得到的随机数使岩石出现在每一侧。在尝试使其正常工作时,我收到了此大量错误消息,并且我对它的含义或如何解决没有任何线索,因此,如果有人可以给我任何有关解决此问题的提示或想法,请执行。 / p>

0 个答案:

没有答案