2016-04-26 4 views
0

私はスプライトシートを作成し、スプライトシートを1つ作成しました。しかし、私はそのスプライトを表示したい。メインクラス(Gameと呼ばれる)、Spriteクラス、SpriteSheetクラス、Entityクラス、Enemyクラスがあります。メインクラスでは、メインゲームループ(ゲームになる)、実行メソッド、その他のもの、initメソッド、およびレンダリングメソッドがあります。私はBufferedImageでレンダリングしたい。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 +スプライト:

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

あなたの質問は何ですか?何が効いていないのですか? – user3437460

+0

すみません、そこに入れるのを忘れました。私の質問は:私はそれが画面上に表示される方法を知りません – Tom

答えて

0

は敵のクラスからあなたのrenderメソッドが呼び出されることはありません。

+0

私はとても愚かです私はそれを忘れています。ありがとうございました! – Tom

関連する問題