- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我收到这些错误:
java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400)
at com.game.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:12)
at com.game.main.Game.<init>(Game.java:48)
at com.game.main.Game.main(Game.java:173)
BufferedImageLoader.java
package com.game.main;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
BufferedImage image;
public BufferedImage loadImage(String path) {
try {
image = ImageIO.read(getClass().getResource(path));
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
}
游戏.java
package com.game.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = -473349850293143017L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public static boolean paused = false;
public int diff = 0;
// 0 = normal
// 1 = hard
private Random r;
private Handler handler;
private HUD hud;
private Spawn spawner;
private Menu menu;
public enum STATE {
Menu,
Select,
Help,
Game,
End
};
public static STATE gameState = STATE.Menu;
public static BufferedImage sprite_sheet;
public Game() {
BufferedImageLoader loader = new BufferedImageLoader();
try {
sprite_sheet = loader.loadImage("/JAVA-Game-Objects.png");
System.out.print("loaded");
} catch (Exception e) {
e.printStackTrace();
}
handler = new Handler();
hud = new HUD();
menu = new Menu(this, handler, hud);
this.addKeyListener(new KeyInput(handler, this));
this.addMouseListener(menu);
new Window(WIDTH, HEIGHT, "Game", this);
spawner = new Spawn(handler, hud, this);
r = new Random();
if(gameState == STATE.Game) {
handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player, handler));
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
} else {
for (int i = 0; i < 20; i++) {
handler.addObject(new MenuParticle(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.MenuParticle, handler));
}
}
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double ns = 10000000;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if(running)
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick() {
if (gameState == STATE.Game) {
if (!paused) {
handler.tick();
hud.tick();
spawner.tick();
if (HUD.HEALTH <= 0) {
HUD.HEALTH = 100;
gameState = STATE.End;
handler.clearEnemies();
for (int i = 0; i < 20; i++) {
handler.addObject(new MenuParticle(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.MenuParticle, handler));
}
}
}
} else if(gameState == STATE.Menu || gameState == STATE.End || gameState == STATE.Select) {
menu.tick();
handler.tick();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
if (paused) {
g.setColor(Color.white);
g.drawString("PAUSED", 100, 100);
}
if (gameState == STATE.Game) {
hud.render(g);
} else if(gameState == STATE.Menu || gameState == STATE.Help || gameState == STATE.End || gameState == STATE.Select) {
menu.render(g);
}
g.dispose();
bs.show();
}
public static float clamp(float y, float min, float max) {
if (y >= max) {
return y = max;
} else if (y <= min) {
return y = min;
} else {
return y;
}
}
public static void main(String args[]) {
new Game();
}
}
第二个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.game.main.SpriteSheet.grabImage(SpriteSheet.java:15)
at com.game.main.Player.<init>(Player.java:21)
at com.game.main.Menu.mousePressed(Menu.java:51)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6633)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
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.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
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:742)
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)
SpritheSheet.java
package com.game.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage sprite;
public SpriteSheet(BufferedImage ss) {
this.sprite = ss;
}
public BufferedImage grabImage(int col, int row, int width, int height) {
BufferedImage img = sprite.getSubimage((row * 32) - 32, (col * 32) - 32, width, height);
return img;
}
}
Player.java
package com.game.main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class Player extends GameObject {
Handler handler;
private BufferedImage player_image;
public Player(float x, float y, ID id, Handler handler) {
super(x, y, id);
this.handler = handler;
SpriteSheet ss = new SpriteSheet(Game.sprite_sheet);
player_image = ss.grabImage(1, 1, 32, 32);
}
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, 32, 32);
}
public void tick() {
x += velX;
y += velY;
x = Game.clamp(x, 0, Game.WIDTH - 50);
y = Game.clamp(y, 0, Game.HEIGHT - 70);
handler.addObject(new Trail(x, y, ID.Trail, Color.white, 32, 32, 0.05f, handler));
collision();
}
private void collision() {
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);
if (tempObject.getId() == ID.BasicEnemy || tempObject.getId() == ID.FastEnemy || tempObject.getId() == ID.SmartEnemy) {
if (getBounds().intersects(tempObject.getBounds())) {
HUD.HEALTH -= 2;
}
}
}
}
public void render(Graphics g) {
g.drawImage(player_image, (int) x, (int) y, null);
}
}
菜单.java
package com.game.main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import com.game.main.Game.STATE;
public class Menu extends MouseAdapter{
private Game game;
private Handler handler;
private HUD hud;
Random r = new Random();
public Menu(Game game, Handler handler, HUD hud) {
this.game = game;
this.hud = hud;
this.handler = handler;
}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if(game.gameState == STATE.Menu) {
// play
if (mouseOver(mx, my, 210, 150, 200, 64)) {
game.gameState = STATE.Select;
return;
}
// help
if (mouseOver(mx, my, 210, 250, 200, 64)) {
game.gameState = STATE.Help;
}
// quit
if (mouseOver(mx, my, 210, 350, 200, 64)) {
System.exit(1);
}
}
if(game.gameState == STATE.Select) {
// normal
if (mouseOver(mx, my, 210, 150, 200, 64)) {
game.gameState = STATE.Game;
handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
handler.clearEnemies();
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
game.diff = 0;
}
// hard
if (mouseOver(mx, my, 210, 250, 200, 64)) {
game.gameState = STATE.Game;
handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
handler.clearEnemies();
handler.addObject(new HardEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
game.diff = 1;
}
// back
if (mouseOver(mx, my, 210, 350, 200, 64)) {
game.gameState = STATE.Menu;
return;
}
}
// back
if (game.gameState == STATE.Help) {
if (mouseOver(mx, my, 210, 350, 200, 64)) {
game.gameState = STATE.Menu;
return;
}
}
// Try Again?
if (game.gameState == STATE.End) {
if (mouseOver(mx, my, 210, 350, 200, 64)) {
game.gameState = STATE.Menu;
hud.setLevel(1);
hud.setScore(0);
}
}
}
public void mouseReleased(MouseEvent e) {
}
private boolean mouseOver(int mx, int my, int x, int y, int width, int height) {
if (mx > x && mx < x + width) {
if (my > y && my < y + height) {
return true;
} else return false;
} else return false;
}
public void tick() {
}
public void render(Graphics g) {
if (game.gameState == STATE.Menu) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
g.setFont(fnt);
g.setColor(Color.WHITE);
g.drawString("Menu", 240, 50);
g.setFont(fnt2);
g.drawRect(210, 150, 200, 64);
g.drawString("Play", 280, 190);
g.drawRect(210, 250, 200, 64);
g.drawString("Help", 280, 290);
g.drawRect(210, 350, 200, 64);
g.drawString("Quit", 280, 390);
} else if (game.gameState == STATE.Help) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.WHITE);
g.drawString("Help", 240, 50);
g.setFont(fnt3);
g.drawString("Use WASD to move player and dodge enemies", 50, 200);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Back", 280, 390);
} else if (game.gameState == STATE.End) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.WHITE);
g.drawString("Game Over", 180, 50);
g.setFont(fnt3);
g.drawString("You lost with a score of: " + hud.getScore() , 175, 200);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Try Again", 245, 390);
} else if (game.gameState == STATE.Select) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
g.setFont(fnt);
g.setColor(Color.WHITE);
g.drawString("SELECT DIFFICULTY", 140, 70);
g.setFont(fnt2);
g.drawRect(210, 150, 200, 64);
g.drawString("Normal", 280, 190);
g.drawRect(210, 250, 200, 64);
g.drawString("Hard", 280, 290);
g.drawRect(210, 350, 200, 64);
g.drawString("Back", 280, 390);
}
}
}
第三个错误:
Exception in thread "Thread-0" java.lang.NullPointerException
at com.game.main.SpriteSheet.grabImage(SpriteSheet.java:15)
at com.game.main.BasicEnemy.<init>(BasicEnemy.java:25)
at com.game.main.Spawn.tick(Spawn.java:29)
at com.game.main.Game.tick(Game.java:118)
at com.game.main.Game.run(Game.java:98)
at java.base/java.lang.Thread.run(Thread.java:830)
BasicEnemy.java
package com.game.main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class BasicEnemy extends GameObject{
private Handler handler;
private BufferedImage enemy_image;
public BasicEnemy(float x, float y, ID id, Handler handler) {
super(x, y, id);
this.handler = handler;
velX = 5;
velY = 5;
SpriteSheet ss = new SpriteSheet(Game.sprite_sheet);
enemy_image = ss.grabImage(2, 1, 16, 16);
}
public Rectangle getBounds() {
return new Rectangle((int)x, (int)y, 16, 16);
}
public void tick() {
x += velX;
y += velY;
if (y <= 0 || y >= Game.HEIGHT - 54) velY *= -1;
if (x <= 0 || x >= Game.WIDTH - 20) velX *= -1;
//handler.addObject(new Trail(x, y, ID.Trail, Color.red, 16, 16, 0.02f, handler));
}
public void render(Graphics g) {
g.drawImage(enemy_image, (int) x, (int) y, null);
}
}
Spawn.java
package com.game.main;
import java.util.Random;
public class Spawn {
private Handler handler;
private HUD hud;
private Game game;
private Random r = new Random();
private int scoreKeep = 0;
public Spawn(Handler handler, HUD hud, Game game) {
this.handler = handler;
this.hud = hud;
this.game = game;
}
public void tick() {
scoreKeep++;
if (scoreKeep >= 250) {
scoreKeep = 0;
hud.setLevel(hud.getLevel() + 1);
if (game.diff == 0) {
if (hud.getLevel() == 2) {
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
} else if (hud.getLevel() == 3) {
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
} else if (hud.getLevel() == 4) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 5) {
handler.addObject(new SmartEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.SmartEnemy, handler));
} else if (hud.getLevel() == 6) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 7) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 10) {
handler.clearEnemies();
handler.addObject(new EnemyBoss((Game.WIDTH / 2) - 48, -120, ID.EnemyBoss, handler));
}
} else if (game.diff == 1) {
if (hud.getLevel() == 2) {
handler.addObject(new HardEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
} else if (hud.getLevel() == 3) {
handler.addObject(new HardEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.BasicEnemy, handler));
} else if (hud.getLevel() == 4) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 5) {
handler.addObject(new SmartEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.SmartEnemy, handler));
} else if (hud.getLevel() == 6) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 7) {
handler.addObject(new FastEnemy(r.nextInt(Game.WIDTH), r.nextInt(Game.HEIGHT), ID.FastEnemy, handler));
} else if (hud.getLevel() == 10) {
handler.clearEnemies();
handler.addObject(new EnemyBoss((Game.WIDTH / 2) - 48, -120, ID.EnemyBoss, handler));
}
}
}
}
}
我不想在这里包含太多代码,所以如果您需要查看完整代码,这里有一个 GitHub 存储库:https://github.com/Super-Michael-05/Game-JAVA-Current-Project
我正在使用 Eclipse IDE。
如果您需要更多信息,请询问。
任何帮助都会有帮助。
最佳答案
对于第一个错误,放置文件路径而不是“路径”对我有用。因此,您可以尝试执行 image = ImageIO.read(getClass().getResources("/JAVA-Game-Objects.png"));
。您还可以在此之前添加空检查,看看是否有帮助。
对于第二个和第三个错误,请尝试在“/JAVA-Game-Objects.png”前面添加“res”,以便得到 sprite_sheet = loader.loadImage("res/JAVA-Game-Objects.png") png”);
。我也在关注同一个系列,这就是为我修复这些错误的方法。
关于java - 我在 Java 中遇到三个错误,但我不知道如何修复它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237913/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!