如何使用bufferedimage绘制精灵

时间:2016-04-26 15:38:44

标签: java bufferedimage

我制作了一张spritesheet,并在那里有一个精灵。但我想显示那个精灵。我得到了主类(称为Game),一个Sprite类,一个SpriteSheet类,一个Entity类和一个Enemy类。在主要课程中,我得到了主要的游戏循环(它将成为一个游戏),一个运行方法,一些其他的东西,一个init方法和一个渲染方法。我想用BufferedImage渲染它。这是我的代码:

游戏:

package com.vos.rekenspel;


public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static final int WIDTH = 130;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;

public static final String NAME = "REKENSPEL";

private JFrame frame;

public boolean running = false;

public int tickCount = 0;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

public static SpriteSheet sheet;
public static Sprite enemy1;
public static Sprite enemy2;
public static Sprite enemy3;

public Game() {

    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    frame = new JFrame(NAME);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());

    frame.add(this, BorderLayout.CENTER);
    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

private void init(){

    sheet = new SpriteSheet("/sprite_sheet.png");

    enemy1 = new Sprite(sheet, 1, 1);

}

public synchronized void start(){

    running = true;

    new Thread(this).start();

}

public synchronized void stop(){

    running = false;

}   
public void run() {

    init();

    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D/60D;

    int ticks = 0;
    int frames = 0;


    long lastTimer = System.currentTimeMillis();
    double delta = 0;

    while(running){

        long now = System.nanoTime();
        delta += (now - lastTime) / nsPerTick;

        lastTime = now;

        boolean shouldRender = false;

        while(delta >= 1){

            ticks++;
            tick();
            delta -= 1;

            shouldRender = true;

        }

        if(shouldRender){

        frames++;
        render();

        }
        if(System.currentTimeMillis() - lastTimer >= 1000){

            lastTimer += 1000;

            System.out.println("Frames: " + frames + ", Ticks:" + ticks);

            ticks = 0;
            frames = 0;

        }

    }

}

private void tick() {

    tickCount++;

    for(int i = 0; i < pixels.length; i++){

        pixels[i] = i * tickCount * i * tickCount; 

    }

}

private void render() {

    BufferStrategy bs = getBufferStrategy();

    if(bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());

    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

    g.dispose();
    bs.show();

}

public static void main(String[] args){

    new Game().start();

}

}

SpriteSheet + Sprite:

package com.vos.rekenspel.gfx;

public class SpriteSheet {

private BufferedImage sheet;

public SpriteSheet(String path){

    try {
        sheet = ImageIO.read(getClass().getResource(path));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public BufferedImage getSprite(int x, int y){
    return sheet.getSubimage(x*8-8, y*8-8, 8, 8);



}

}

package com.vos.rekenspel.gfx;

public class Sprite {

public static SpriteSheet sheet;

public BufferedImage image;

public Sprite(SpriteSheet sheet, int x, int y){

    image = sheet.getSprite(x, y);

}

public BufferedImage getBufferedImage(){

    return image;

}

}

敌人+实体:

package com.vos.rekenspel.entity;

public class Enemy extends Entity{

public Enemy(int x, int y, int width, int height, boolean solid) {
    super(x, y, width, height, solid);
}

public void render(Graphics g){

    g.drawImage(Game.enemy1.getBufferedImage(), x, y, width, height, null);

}
}
package com.vos.rekenspel.entity;

public class Entity {

public int x, y;
public int width, height;

public boolean solid;

public Entity(int x, int y, int width, int height, boolean solid){

    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.solid = solid;
}

public void render(Graphics g){

}

}

但我无法将其显示在屏幕上。 有谁知道解决方案?

提前致谢。

亲切的问候, 汤姆

1 个答案:

答案 0 :(得分:0)

永远不会调用Enemy类的渲染方法。