我想使用RGB值的像素数组(在.render()
方法中)从我的精灵表中获取精灵。假设像素阵列保持整个图像的RGB值以便稍后使用,以便从图像中获得各个精灵。有人能解释我怎么能这样做吗?我只是拍了一张黑色的照片。
代码:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class SpritePractice extends Canvas implements Runnable{
private JFrame frame;
public final static int WIDTH = 200, HEIGHT = 200;
public final static int SCALE = 2;
private final static Dimension dimens= new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
private BufferedImage image;
private Graphics g;
private long nanoSecond = 1000000000;
private double tick = nanoSecond/60;
private boolean running = false;
private int pixelsFromImage[];
private int pixel[][];
private static DateFormat dateFormat = new SimpleDateFormat("[" + "yyyy/MM/dd HH:mm:ss"
+"]");
private static DateFormat dateFormat2 = new SimpleDateFormat("[" + "HH:mm:ss" + "]");
public SpritePractice()
{
frame = new JFrame("Bomberman");
frame.setSize(dimens);
frame.setMinimumSize(dimens);
frame.setMaximumSize(dimens);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setVisible(true);
init();
}
public void init()
{
long startTime = System.nanoTime();
Calendar cal = Calendar.getInstance();
System.out.println("START: " + dateFormat.format(cal.getTime()));
}
public void run()
{
long now = System.nanoTime();
long lastTick = System.nanoTime();
long lastSecond = System.nanoTime();
int frames = 0;
while(running)
{
now = System.nanoTime();
Calendar cal = Calendar.getInstance();
if(now-lastTick >= tick)
{
lastTick = now;
tick();
try {
render();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frames++;
}
if(now-lastSecond >= nanoSecond)
{
lastSecond = now;
System.out.println(dateFormat2.format(cal.getTime()) + "FPS: " + frames);
frames = 0;
}
}
}
public void tick()
{
//updates values
}
public void render() throws IOException
{
BufferStrategy bs = getBufferStrategy();
if(bs==null)
{
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
BufferedImage spriteSheet = new BufferedImage(WIDTH*2,HEIGHT*2, BufferedImage.TYPE_INT_RGB);
try
{
spriteSheet = ImageIO.read(new File("/res/MarioSpriteSheet.png"));
}catch (IOException e)
{
}
finally
{
pixelsFromImage = ((DataBufferInt) spriteSheet.getRaster().getDataBuffer()).getData();
}
BufferedImage sprite = new BufferedImage(WIDTH*2,HEIGHT*2, BufferedImage.TYPE_INT_RGB);
sprite.getRaster().setPixels(50, 50, 100, 100, pixelsFromImage);
g.drawImage(sprite,100,100,100,100,null);
g.dispose();
bs.show();
//renders graphics
}
public synchronized void start()
{
running = true;
run();
}
public synchronized void stop()
{
running = false;
}
public static void main(String[] args)
{
new SpritePractice().start();
}
}
更新(1):
- 可以通过更改spriteSheet = ImageIO.read(getClass().getResourceAsStream("/res/MarioSpriteSheet.png"));
到spriteSheet = ImageIO.read(getClass().getResourceAsStream("/MarioSpriteSheet.png"));
(图像res文件夹已经在CLASS PATH。
新问题:
Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
at SpritePractice.render(SpritePractice.java:115)
at SpritePractice.run(SpritePractice.java:75)
at SpritePractice.start(SpritePractice.java:125)
at SpritePractice.main(SpritePractice.java:133)
答案 0 :(得分:1)
该代码在此抛出异常然后忽略。更改表单的代码
catch (Exception e) {
..
到:
catch (Exception e) {
e.printStackTrace(); // very informative!
..