gpt4 book ai didi

java - 如何让 KeyListener 为 Java 小程序绘制图像

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:39 25 4
gpt4 key购买 nike

我正在尝试为类(class)制作一个java小程序游戏,允许用户上下输入来移动“玩家”图像并使用空格键射击“僵尸”。我有一个“子弹”图像,并尝试使用 KeyListener,以便当用户按下空格键时,“子弹”图像会显示在玩家当前位置的屏幕上,并在屏幕上传播。但是,当按下空格时,我会收到运行时错误,并且不会显示项目符号图像。如果有人能让我知道如何解决这个问题,我将不胜感激!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ZombieAttackMain extends Applet implements Runnable, KeyListener{

private Image background, normalZombie, fastZombie, tankyZombie,player,bullet, bloodSplat, cow;

private Graphics bufferGraphics;
private Graphics g;
private Image offScreen;

private final int POSITION_1_Y = 100;
private final int POSITION_2_Y = 255;
private final int POSITION_3_Y = 400;
private final int POSITION_4_Y = 550;

private final int PLAYER_X_POSITION = 250;
private int playerYPosition = POSITION_3_Y;

private int zombieStartingXPosition = 1000; //change to 1100 later
private final int NORMAL_ZOMBIE_DX = -2; //not final
private final int FAST_ZOMBIE_DX = -3; //not final
private final int TANKY_ZOMBIE_DX = -1; //not final
private int zombieYPosition; //depends on the zombieYPositionRandom

private int bulletStartingXPosition = 405;
private int bulletYPosition; //depends on player position
private final int BULLET_DX = 5; //not final

private int bloodSplatX, bloodSplatY; //depend on dead zombie position

//Called when applet starts
public void init(){
setSize(1100, 700);
normalZombie = getImage(getCodeBase(), "NormalZombie.png");
tankyZombie = getImage(getCodeBase(), "TankyZombie.png");
fastZombie = getImage(getCodeBase(), "FastZombie.png");
player = getImage(getCodeBase(), "Player.png");
bullet = getImage(getCodeBase(), "Bullet.png");
bloodSplat = getImage(getCodeBase(), "BloodSplat.png");
background = getImage(getCodeBase(), "Background.jpg");
cow = getImage(getCodeBase(), "Cow.png");
addKeyListener(this);
}
//Called after init, sets up thread and starts it
public void start() {
Thread thread = new Thread(this);
thread.start();
}

public void run() {
while(true){
repaint();
try {
Thread.sleep(17); //about 60 fps
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


public void update(Graphics g){
if(offScreen == null){
offScreen = createImage(this.getWidth(), this.getHeight());
bufferGraphics = offScreen.getGraphics();
}
bufferGraphics.setColor(getBackground());
bufferGraphics.fillRect(0,0,this.getWidth(),this.getHeight());

bufferGraphics.setColor(getForeground());
paint(bufferGraphics);

g.drawImage(offScreen,0,0,this);
}

public void paint(Graphics g){
g.drawImage(background,0,0,null);
g.drawImage(cow,-15,115,null);
g.drawImage(cow,-15,255,null);
g.drawImage(cow,-15,400,null);
g.drawImage(cow,-15,550,null);
g.drawImage(normalZombie,zombieStartingXPosition,100,null);
g.drawImage(tankyZombie,zombieStartingXPosition,400,null);
g.drawImage(fastZombie,zombieStartingXPosition,255,null);
g.drawImage(player,PLAYER_X_POSITION,playerYPosition, null);
//g.drawImage(bullet,bulletStartingXPosition,336,this);
g.drawImage(bloodSplat,bloodSplatX,bloodSplatY,null);
}

public void moveUp(Image player){
if(playerYPosition == POSITION_2_Y){
playerYPosition = POSITION_1_Y;
}
if(playerYPosition == POSITION_3_Y){
playerYPosition = POSITION_2_Y;
}
if(playerYPosition == POSITION_4_Y){
playerYPosition = POSITION_3_Y;
}
}

public void moveDown(Image player){
if(playerYPosition == POSITION_3_Y){
playerYPosition = POSITION_4_Y;
}
if(playerYPosition == POSITION_2_Y){
playerYPosition = POSITION_3_Y;
}
if(playerYPosition == POSITION_1_Y){
playerYPosition = POSITION_2_Y;
}

}

public void spawnBullet(){
g.drawImage(bullet,bulletStartingXPosition, playerYPosition,null);
}

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
moveUp(player);
break;
case KeyEvent.VK_DOWN:
moveDown(player);
break;
case KeyEvent.VK_SPACE:
spawnBullet();
break;
}
}

public void keyReleased(KeyEvent e) {

}


public void keyTyped(KeyEvent e) {

}

public void stop() {

}

public void destroy() {

}
}

错误消息:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ZombieAttackMain.spawnBullet(ZombieAttackMain.java:118)
at ZombieAttackMain.keyPressed(ZombieAttackMain.java:130)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.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.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)

最佳答案

您的问题是您从未初始化您的私有(private)图形g。乍一看这可能并不明显,因为您的 paint()update() 方法使用 g 就很好,但在这些方法中,g 是一个完全不同的本地 Graphics 对象,它掩盖了您的全局 g 。我的建议是删除 private Graphics g; 因为确实没有必要让另一个 Graphics 对象 float ,并将 spawnBullet() 更改为:

public void spawnBullet(){
bufferGraphics.drawImage(bullet, bulletStartingXPosition, playerYPosition, null);
}

但是,由于每次更新都会清除整个屏幕,因此不会显示项目符号...最好仅在 paint() 方法中绘制,但这完全是不同的主题。

编辑:展示如何让项目符号实际显示的示例

private int bulletStartingXPosition = 405;
private int bulletYPosition; //depends on player position
private final int BULLET_DX = 5; //not final

// NEW STUFF
private int bulletXPosition;
private boolean showingBullet = false;

public void paint(Graphics g){
g.drawImage(background,0,0,null);
g.drawImage(cow,-15,115,null);
g.drawImage(cow,-15,255,null);
g.drawImage(cow,-15,400,null);
g.drawImage(cow,-15,550,null);
g.drawImage(normalZombie,zombieStartingXPosition,100,null);
g.drawImage(tankyZombie,zombieStartingXPosition,400,null);
g.drawImage(fastZombie,zombieStartingXPosition,255,null);
g.drawImage(player,PLAYER_X_POSITION,playerYPosition, null);
//g.drawImage(bullet,bulletStartingXPosition,336,this);
g.drawImage(bloodSplat,bloodSplatX,bloodSplatY,null);

// NEW STUFF
if (showingBullet) {
g.drawImage(bullet,bulletXPosition, playerYPosition, null);
bulletXPosition += BULLET_DX;
if (bulletXPosition > 1100) { // if it went off the screen
showingBullet = false;
}
}
}

public void spawnBullet() {

// NEW STUFF
if (showingBullet) {
return; // don't want to reset the bullet if it's already on the screen
}
showingBullet = true;
bulletXPosition = bulletStartingXPosition;
}

关于java - 如何让 KeyListener 为 Java 小程序绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com