Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JPanel/JFrame的2D射手。用它在屏幕上绘制图像(项目符号)';自身速度_Java_Swing_Graphics_Nullpointerexception_Jpanel - Fatal编程技术网

Java 使用JPanel/JFrame的2D射手。用它在屏幕上绘制图像(项目符号)';自身速度

Java 使用JPanel/JFrame的2D射手。用它在屏幕上绘制图像(项目符号)';自身速度,java,swing,graphics,nullpointerexception,jpanel,Java,Swing,Graphics,Nullpointerexception,Jpanel,以下是我所拥有的: import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java

以下是我所拥有的:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener, KeyListener, ImageObserver {
        public Image ship;
        public Image enemy;
        private int xLoc;
        private int yLoc = 180;
        private int xLoc2 = 700;
        private int yLoc2 = 180;
        private int xVel;
        private int yVel;
        private int xVel_en;
        private int xVel_sh = 6;
        private ImageObserver observer = null;
        private ArrayList<Base> shield = new ArrayList<Base>();
        private Image shieldPiece;
        private Shot shot;

        public GamePanel()  {
            ship = Toolkit.getDefaultToolkit().createImage("ship3.png");
            enemy = Toolkit.getDefaultToolkit().createImage("alien-ship-1.png");
            shieldPiece = Toolkit.getDefaultToolkit().createImage("block2.png");

            for(int x = 500; x < 680; x = x + 18)
                for(int y = 60; y < 370; y = y + 18)    {
                    Base temp = new Base(x,y);
                    shield.add(temp);
                }
        }

        public void paint(Graphics g){
            super.paint(g);
            g.drawImage(ship, xLoc, yLoc, null);
            g.drawImage(enemy, xLoc2, yLoc2, null);

            for(Base b: shield)
                g.drawImage(shieldPiece, b.getShieldX(), b.getShieldY(), null);
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

            move();
            setBackground(Color.BLACK);
            repaint();

        }

        private void move() {

            int endOfWindow = getWidth();
            int endOfWindow1 = getHeight();
            if(xLoc>=(endOfWindow - ship.getWidth(null))|| xLoc<0)
                xVel = -xVel;
            xLoc +=xVel;    
            if(xLoc>=(endOfWindow - ship.getWidth(null)))
                xVel = -xVel;
            xLoc +=xVel;
            if(yLoc>=(endOfWindow1 - ship.getHeight(null)) || yLoc<0)
                yVel = -yVel;
            yLoc +=yVel;
            if(yLoc>=(endOfWindow1 - ship.getHeight(null)))
                yVel = -yVel;
            yLoc +=yVel;

        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub

            if(arg0.getKeyCode() == KeyEvent.VK_D)
                xVel = 3;
            if(arg0.getKeyCode() == KeyEvent.VK_A)
                xVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_W)
                yVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_S)
                yVel = 3;

            if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
                xVel = 3;
            if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
                xVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_UP)
                yVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
                yVel = 3;

            if(arg0.getKeyCode() == KeyEvent.VK_SPACE)  {
                Shot shoot = new Shot(xVel_sh, xLoc, yLoc);
                shoot.paint();

            }


        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

            if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_A)
                xVel = 0;
            if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_S)
                yVel = 0;

            if(arg0.getKeyCode() == KeyEvent.VK_RIGHT || arg0.getKeyCode() == KeyEvent.VK_LEFT)
                xVel = 0;
            if(arg0.getKeyCode() == KeyEvent.VK_UP || arg0.getKeyCode() == KeyEvent.VK_DOWN)
                yVel = 0;

        }

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
}
我已经设置好了它,所以当我点击空格时,它应该创建一个快照对象,然后在shot类中调用paint()方法。快照对象将在xLoc和yLoc处创建(来自GamePanel类,即“飞船”或玩家的位置)

然而,我得到了很多错误,当我点击空格时,从NullPointerException开始。游戏没有崩溃,它只是不断出现一个空指针异常。它后面还有一个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Shot.paint(Shot.java:25)
    at GamePanel.keyPressed(GamePanel.java:104)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

不太清楚为什么没有画出来……有人能帮忙吗?此外,我还需要最终实现对象碰撞(此处未显示屏蔽类)……因此,如果有人对这方面的教程有任何指导意见吗P

g
从未在
Shot
中初始化,因此当您调用
g.drawImage
时,它会抛出
NullPointerException

g
从未在
Shot
中初始化,因此,当您调用
g.drawImage
时,它会抛出一个
NullPointerException

,您可以像调试任何NPE一样调试NPE——找出抛出它的行,然后找出该行上的哪个变量为null,然后回溯到您的程序中,找出它为null的原因。祝你好运。你不应该维护对任何
图形
上下文(尤其是你没有创建的上下文)的引用。您还应该在使用完所有
Graphcis
上下文后立即处理它们,因为它们可能会消耗资源。您可以像任何NPE一样调试NPE--找出哪一行抛出它,然后找出该行上的哪个变量为null,然后回溯到您的程序中,找出它为null的原因。祝你好运。你不应该维护对任何
图形
上下文(尤其是你没有创建的上下文)的引用。您还应该在使用完所有
Graphcis
上下文后立即处理它们,因为它们可能会消耗资源。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Shot.paint(Shot.java:25)
    at GamePanel.keyPressed(GamePanel.java:104)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)