这就是我所拥有的:
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类:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class Shot{
private int xVel_sh;
private int xCoord;
private int yCoord;
private Image shot;
private Graphics g;
public Shot(int vel, int xLoc, int yLoc) {
vel = xVel_sh;
xCoord = xLoc;
yCoord = yLoc;
shot = Toolkit.getDefaultToolkit().createImage("shot4.png");
}
public void setShotVel(int shotVel) {
xVel_sh = shotVel;
}
public void paint() {
g.drawImage(shot, xCoord, yCoord, null);
}
}
我设置它所以当我击中空格时,它应该创建一个镜头对象,然后调用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
答案 0 :(得分:3)
g
永远不会在Shot
中初始化,因此当您致电g.drawImage
时,它会抛出NullPointerException
。