如何从多个较小的图像构建一幅图像

huangapple go评论71阅读模式
英文:

How do I build an Image from multiple smaller Images

问题

我正在制作这个冒险/进阶游戏,对于字体,我使用了来自精灵表(每个精灵是一个字母)的图块,我将其用于许多不同的目的,但我无法弄清楚为什么我的程序返回的是一个黑色图像,而不是由较小图像(每个包含一个字母)构建的图像。当我返回scaledImage图像时,程序是正常的,但那只返回一个单独的字母。预期的情况是,我将单词分割成字母,并使用BufferedImageGraphics2D从精灵表中获取匹配的字母,这一部分是正常的,但当我将其绘制到缓冲图像时,返回的却是一个黑色图像。

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 = { &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;i&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;,
&quot;t&quot;, &quot;u&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;, &quot; &quot; };
public ImageIcon getWord(String word, int x, int size) throws IOException {
String tempLetter;
try {
spriteSheet = ImageIO.read(new File(&quot;img/SpriteSheet.png&quot;));
} 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 &lt; word.length(); l++) {
tempLetter = word.substring(l, l + 1);
for (int i = 0; i &lt; 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(&quot;img/word.png&quot;);
ImageIO.write(bi, &quot;png&quot;, wordFile);
ImageIcon ic = new ImageIcon(&quot;img/word.png&quot;);
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(&quot;play&quot;, 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.

huangapple
  • 本文由 发表于 2020年10月22日 00:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64467747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定