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

问题描述

所以我正在做这个冒险/进步游戏,我使用的是spriteSheet中的图块(每个图块是一个字母),我会将其用于许多不同的目的,但我不知道为什么我的程序是返回黑色图像,而不是从较小的图像(每个图像都包含一个字母)构建的图像。当我返回 scaledImage 图像时,该程序可以运行,但是只返回一个字母。应该发生的事情是,我将单词分为字母并使用 BufferedImage Graphics2D 从spriteSheet获取匹配的字母,该部分有效,但是当我绘制时它返回到缓冲图像,然后返回黑色图像。

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,Image.SCALE_DEFAULT);
                    wordImage.drawImage(scaledImage,l*size,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",150)); 
p.add(play);
f.add(p);

解决方法

经过一段时间的研究,我发现字母没有显示的原因是因为它们是黑色的,而 BufferedImage 的背景也是黑色的。所以字母是显示出来的,但由于背景的原因“不可见”。