英文:
How do I build an Image from multiple smaller Images
问题
我正在制作这个冒险/进阶游戏,对于字体,我使用了来自精灵表(每个精灵是一个字母)的图块,我将其用于许多不同的目的,但我无法弄清楚为什么我的程序返回的是一个黑色图像,而不是由较小图像(每个包含一个字母)构建的图像。当我返回scaledImage图像时,程序是正常的,但那只返回一个单独的字母。预期的情况是,我将单词分割成字母,并使用BufferedImage和Graphics2D从精灵表中获取匹配的字母,这一部分是正常的,但当我将其绘制到缓冲图像时,返回的却是一个黑色图像。
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;
}
这是我调用方法的方式:
JButton play = new JButton();
play.setBounds(300, 300, 600, 150);
play.setIcon(sc.getWord("play", 600, 150));
p.add(play);
f.add(p);
英文:
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);
答案1
得分: 2
经过一段时间的研究,我发现字母未显示的原因是因为它们是黑色的,而BufferedImage的背景也是黑色的。因此,这些字母虽然已经显示出来,但由于背景的缘故,它们变得“不可见”。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论