图像在Java游戏中无法使用repaint()
我正在尝试在游戏中显示图像。我的游戏背景正常,但我无法让我的图像在Java游戏中无法使用repaint(),java,Java,我正在尝试在游戏中显示图像。我的游戏背景正常,但我无法让我的playerIcon、bulletIcon和heartIcon图像正常工作 我的imageLoader类:(工作正常) 我的游戏类的重要部分:(正确加载我的bg.png图像) 我的子弹类:(工作不正常) 我的处理程序类: package BullHellSpel; import java.awt.Graphics; import java.util.LinkedList; public class Handler { Linked
playerIcon
、bulletIcon
和heartIcon
图像正常工作
我的imageLoader类:(工作正常)
我的游戏类的重要部分:(正确加载我的bg.png图像)
我的子弹类:(工作不正常)
我的处理程序类:
package BullHellSpel;
import java.awt.Graphics;
import java.util.LinkedList;
public class Handler {
LinkedList<GameObject> object = new LinkedList<GameObject>();
public void tick(){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.repaint(g);
}
}
public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);
}
}
package BullHellSpel;
导入java.awt.Graphics;
导入java.util.LinkedList;
公共类处理程序{
LinkedList对象=新建LinkedList();
公共空白勾号(){
对于(int i=0;i
我曾经尝试过使用render()、render(Graphics g)、repaint()和repaint(Graphics g)进行绘图,但似乎没有任何效果。我99%确信我的图像的路径是正确的,并且在渲染(图形g)和重新绘制(图形g)之前我使用的占位符方块都可以很好地工作。当我启动游戏时,我没有收到任何错误。为什么没有显示我的图像?GameObject中的代码可能对解决问题至关重要,这里不包括它。对象的渲染顺序是否在背景之后?否则,将在它们上面绘制背景。尝试移除背景,看看会发生什么。
public void run()
{
init();
}
private void init(){
bg = ImageLoader.loadImage("/bg.png");
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//You can draw here
g.drawImage(bg, 0, 0, null);
handler.render(g);
g.dispose();
bs.show();
}
package BullHellSpel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Bullet extends GameObject {
private BufferedImage bulletIcon;
private Handler handler;
public Bullet(int x, int y, ID id, Handler handler) {
super(x, y, id);
this.handler = handler;
Random rand = new Random();
velX = rand.nextInt(5) + 3;
velY = rand.nextInt(3) + 2;
}
public Rectangle getBounds(){
return new Rectangle(x, y, 10, 10);
}
public void run()
{
init();
}
private void init(){
bulletIcon = ImageLoader.loadImage("/bullet.png");
}
public void tick() {
x += velX;
y += velY;
if(x <= 0 || x >= Game.WIDTH - 16) velX *= -1;
}
public void repaint() {
Graphics g = null;
g.drawImage(bulletIcon, x, y, null);
//g.setColor(Color.yellow);
//g.fillRect(x, y, 10, 10);
}
public void repaint(Graphics g) {
g.drawImage(bulletIcon, x, y, null);
//g.setColor(Color.yellow);
//g.fillRect(x, y, 10, 10);
}
}
package BullHellSpel;
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class GameObject {
protected int x, y;
protected ID id;
protected int velX, velY;
public GameObject(int x, int y, ID id){
this.x = x;
this.y = y;
this.id = id;
}
public abstract void tick();
public abstract void repaint(Graphics g);
public abstract Rectangle getBounds();
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void getY(int y){
this.y = y;
}
public void getX(int x){
this.x = x;
}
public void setId(ID id){
this.id = id;
}
public ID getId(){
return id;
}
public void setVelX(int velX){
this.velX = velX;
}
public void setVelY(int velY){
this.velY = velY;
}
public int getVelX(int velX){
return velX;
}
public int getVelY(int velY){
return velY;
}
}
package BullHellSpel;
import java.awt.Graphics;
import java.util.LinkedList;
public class Handler {
LinkedList<GameObject> object = new LinkedList<GameObject>();
public void tick(){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.repaint(g);
}
}
public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);
}
}