I want to get sprites from my sprite sheet using a pixel array of RGB values (in the .render()
method). The pixel array is supposed to hold the RGB values of the entire image to later be used in order to get the individual sprites from the image. Can someone explain exactly how I can do that? I'm just getting a black image.
Code:
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();
}
}
UPDATE (1):
-Able to read the file, "MarioSpriteSheet.png" by changing spriteSheet = ImageIO.read(getClass().getResourceAsStream("/res/MarioSpriteSheet.png"));
to spriteSheet = ImageIO.read(getClass().getResourceAsStream("/MarioSpriteSheet.png"));
(the image res folder was already in CLASS PATH.
New problem:
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)