Java游戏中没有与repaint()一起使用的图像

时间:2018-04-21 14:54:56

标签: java

我想在我的游戏中显示图像。我的游戏背景有效,但我无法让playerIconbulletIconheartIcon图像生效。

我的imageLoader类:(正常工作)

package BullHellSpel;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader {

public static BufferedImage loadImage(String path){
    try {
        return ImageIO.read(ImageLoader.class.getResource(path));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    return null;
}
}

我的游戏类的重要部分:(正确加载我的bg.png图像)

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();
}

My Bullet Class :(工作不正常)

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);

}
}

我的GameObject类:

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);
}
}

我尝试使用render(),render(Graphics g),repaint()和repaint(Graphics g)绘图,但似乎没有任何效果。我99%确定我的图像路径是正确的,并且在我使用渲染(图形g)和重绘(图形g)之前我的图像正常工作之前使用的占位符正方形。当我启动游戏时,我没有收到任何错误。为什么我的图像没有显示?

0 个答案:

没有答案