gpt4 book ai didi

java - 如何使用像素数组从 Sprite 表中获取 Sprite ?

转载 作者:行者123 更新时间:2023-12-01 13:50:19 24 4
gpt4 key购买 nike

我想使用 RGB 值的像素数组(在 .render() 方法中)从 Sprite 表中获取 Sprite 。像素数组应该保存整个图像的 RGB 值,以便稍后使用,以便从图像中获取各个 Sprite 。有人能准确解释我该怎么做吗?我刚刚得到一张黑色图像。

代码:

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")); 读取文件“MarioSpriteSheet.png”;

spriteSheet = ImageIO.read(getClass().getResourceAsStream("/MarioSpriteSheet.png"));

(图像资源文件夹已在类路径中。

新问题:

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)

最佳答案

该代码在这里抛出异常,然后将其忽略。更改表单的代码

catch (Exception e) { 
..

至:

catch (Exception e) { 
e.printStackTrace(); // very informative!
..

关于java - 如何使用像素数组从 Sprite 表中获取 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20014059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com