这是我所拥有的:
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对象,然后在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)
不太确定为什么没有绘制它...任何人都可以帮忙吗?另外,我最终需要实现对象冲突(此处未显示shield类)...因此,如果有人对本教程有任何指导? :P
最佳答案
g
从未在Shot
中初始化,因此,当您调用g.drawImage
时,它将引发NullPointerException
。
关于java - 使用JPanel/JFrame的2D Shooter。屏幕上的drawImage(项目符号)具有自己的速度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16379164/