JFrame精灵表黑屏

时间:2016-11-12 18:12:30

标签: java eclipse java-8 jframe

所以我一直在尝试按照教程

但它不起作用。这是一个使用JFrame制作精灵表的教程。我可以得到其他的东西,但由于某种原因,这不会成功。它应该显示平铺图像,但它显示黑屏。


Game.java

package ca.colescheler.game;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import ca.colescheler.game.gfx.Screen;
import ca.colescheler.game.gfx.SpriteSheet;

@SuppressWarnings("unused")
public class Game extends Canvas implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 9086760045199246082L;



private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();


public boolean running;

private static final long serialVesionUID = 1;

public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE=3;
public static final String NAME="Game";

private JFrame frame;

int tickCount=1;

private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");

private Screen screen;


public static long getSerialvesionuid() {
    return serialVesionUID;
}




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



}

public void init() {
    screen = new Screen(WIDTH,HEIGHT,new SpriteSheet("/sprite_sheet.png"));
}

public void tick() {
    tickCount++;


}






long now = System.nanoTime();





public synchronized void stop() {
    // TODO Auto-generated method stub

}

public synchronized void start() {
    // TODO Auto-generated method stub
    //new Thread(this).start();
    running = true;
    run();
    frames = 1;
    ticks = 1;

}

static int frames = 0;
static int ticks = 0;



public void run() {

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


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



    while (running) {


        init();

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

        boolean shouldRender = false;

        while (delta >= 1) {
            ticks++;
            tick();
            delta -= 1;
            shouldRender = true;

        }


        if (shouldRender) {
            render();
        }



        if (System.currentTimeMillis() - lastTimer > 1000) {
            lastTimer += 1000;
            System.out.println(frames + ", " + ticks);
            frames=0;
            ticks=0;
        }
    }



}

public void render() {      

    BufferStrategy bs = this.getBufferStrategy(); 
    if (bs == null) {
           this.createBufferStrategy(3); 
           return;
    }





    screen.render(pixels,  0,  WIDTH);




    Graphics g = bs.getDrawGraphics();
    //g.drawRect(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.java

    package ca.colescheler.game.gfx;

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

import javax.imageio.ImageIO;

public class SpriteSheet {
    public String path;
    public int width;
    public int height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;

        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (image == null) {
            return;
        }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0, 0, width, height, null, 0, width);

        //AARRGGBB

        for (int i=0; i<pixels.length;i++) {
            pixels[i] = (pixels[i] & 0xff)/64;
        }

        for(int i=0; i<8;i++) {
            System.out.println(pixels[i]);
        }
        //85*0


    }
}

Screen.java

package ca.colescheler.game.gfx;

public class Screen {
    public static final int MAP_WIDTH = 64;
    public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;

    public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH];
    public int[] colours = new int[MAP_WIDTH * MAP_WIDTH * 4];

    public int xOffset = 0;
    public int yOffset = 0;

    public int width;
    public int height;

    public SpriteSheet sheet;

    public Screen(int width, int height, SpriteSheet sheet) {
        this.width = width;
        this.height = height;
        this.sheet = sheet;

        for (int i=1;i<MAP_WIDTH*MAP_WIDTH;i++) {
            colours[i*4+0] = 0xff00ff;
            colours[i*4+1] = 0x00ffff;
            colours[i*4+2] = 0xffff00;
            colours[i*4+3] = 0xffffff;
        }
    }

    public void render(int[] pixels, int offset, int row) {
        for (int yTile = yOffset>>3;yTile <-(yOffset + height) >>3; yTile++) {
            int yMin = yTile * 8 - yOffset;
            int yMax = yMin + 8;
            if (yMin < 0) yMin = 0;
            if (yMax > height) yMax = height;

            for (int xTile = xOffset >>3; xTile <=(xOffset+width) >>3; xTile++) {
                int xMin = xTile * 8 - xOffset;
                int xMax = xMin + 8;
                if (xMin < 0) xMin = 0;
                if (xMax > width) xMax = height;

            int tileIndex = (xTile &(MAP_WIDTH_MASK)) + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH;

            for (int y = yMin;y<yMax;y++) {
                int sheetPixel = ((y+yOffset) & 7) * sheet.width + ((xMin + xOffset) & 7);
                int tilePixel = offset + xMin + y * row;
                for (int x=xMin; x<xMax;x++) {
                    int colour = tileIndex * 4 + sheet.pixels[sheetPixel++];
                    pixels[tilePixel++]=colours[colour];
                }

            }

        }
    }
    }}

Link to tutorial

1 个答案:

答案 0 :(得分:-1)

文件Screen.java中有一个错字。您应在以下行中将height更改为width

if (xMax > width) xMax = height;