Hello, I’ve got a problem with a spritesheet I’m trying to make using Java 2D. I first load an external PNG into a BufferedImage using the ImageIO class and using a Sprite class I pass in the buffered spritesheet, where I want it in the frame (x, y), the top left hand point of the tile from the spritesheet that I want to extract (xOffset, yOffset) and the width and height of the tile.
The external PNG is just a spritesheet containing the characters a-z, 0-9 that I made in photoshop, with each tile being 20x20 pixels. I’ll post the code so you can see exactly what I mean, but what the problem is, is that when a new character is drawn using the Component classes paint method, the previously rendered characters disappear.
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Main
{
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
private Screen screen;
public Main()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
BufferedImage ss = null;
try
{
ss = ImageIO.read(new File("charSprite.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
screen = new Screen(WIDTH, HEIGHT);
screen.loadImage(new Sprite(ss, 0, 0, 20, 20).getSprite(), 20, 20);
screen.loadImage(new Sprite(ss, 0, 0, 20, 20).getSprite(), 40, 40);
frame.add(screen);
}
public static void main(String[] args)
{
new Main();
}
}
Here’s the Sprite class, for dissecting the tile from the spritesheet:
import java.awt.image.BufferedImage;
public class Sprite
{
private BufferedImage sprite;
public Sprite(BufferedImage ss, int xOffset, int yOffset, int width, int height)
{
sprite = ss.getSubimage(xOffset, yOffset, width, height);
}
public BufferedImage getSprite()
{
return sprite;
}
}
Here’s the screen class which holds the bufferedimage containing each element within the screen:
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Screen extends Component
{
private BufferedImage display = null;
private int[] pixels;
public Screen(int w, int h)
{
display = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
pixels = new int[w * h];
}
public void loadImage(BufferedImage im, int x, int y)
{
int[] p = im.getRGB(0, 0, im.getWidth(), im.getHeight(), null, 0, im.getWidth());
for(int y1 = 0; y1 < im.getHeight(); y1++)
{
for(int x1 = 0; x1 < im.getWidth(); x1++)
{
pixels[(y1 + y) * display.getWidth() + (x1 + x)] = p[y1 * im.getWidth() + x1];
}
}
display.setRGB(0, 0, display.getWidth(), display.getHeight(), pixels, 0, display.getWidth());
repaint();
}
public void paint(Graphics g)
{
g.drawImage(display, 0, 0, display.getWidth(), display.getHeight(), null);
}
}
I’m quite new to this and just trying to understand how swing works so any help would be appreciated. Thanks!
Paul