Reputation: 41
So I am making this adventure/progression game and for the font I am using tiles from a spriteSheet (each tile is a letter) which I will use for many different purposes, and I can't figure out why my program is returning a black Image instead of an Image built from smaller Images (each containing a letter). The program works when I return the scaledImage Image but that only returns a single letter. What is supposed to happen is I divide the word in a letter and use a BufferedImage and Graphics2D to get the matching letter from the spriteSheet, that part works, but when I draw it to the Buffered Image, it returns a black Image.
BufferedImage spriteSheet;
String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", " " };
public ImageIcon getWord(String word, int x, int size) throws IOException {
String tempLetter;
try {
spriteSheet = ImageIO.read(new File("img/SpriteSheet.png"));
} catch (IOException e) {
e.printStackTrace();
}
Image testImage = null;
BufferedImage bi = new BufferedImage(size * word.length(), size, BufferedImage.TYPE_INT_RGB);
Graphics2D wordImage = bi.createGraphics();
Image subImage;
Image scaledImage;
for (int l = 0; l < word.length(); l++) {
tempLetter = word.substring(l, l + 1);
for (int i = 0; i < alphabet.length; i++) {
if (tempLetter.equalsIgnoreCase(alphabet[i])) {
subImage = spriteSheet.getSubimage(2 + (2 * i) + (i * 200), 2, 200, 200);
scaledImage = subImage.getScaledInstance(size, size, Image.SCALE_DEFAULT);
wordImage.drawImage(scaledImage, l*size, 0, null);
testImage = scaledImage;
} else {
continue;
}
}
}
wordImage.dispose();
File wordFile = new File("img/word.png");
ImageIO.write(bi, "png", wordFile);
ImageIcon ic = new ImageIcon("img/word.png");
wordFile.delete();
return ic;
}
This is how I call the method:
JButton play = new JButton();
play.setBounds(300, 300, 600, 150);
play.setIcon(sc.getWord("play", 600, 150));
p.add(play);
f.add(p);
Upvotes: 1
Views: 165
Reputation: 41
After a while of research, I found out that the reason the letters were not showing was because they are black and the background of the BufferedImage was also black. So the letters were showing but were "invisible" because of the background.
Upvotes: 2