【问题标题】:Adding a Image Within a switch - PaintComponent Override在开关中添加图像 - PaintComponent 覆盖
【发布时间】:2017-12-07 18:37:13
【问题描述】:

我正在尝试将一个小 burger.jpg img 添加到我正在制作的游戏中。每当在 Switch 中传递 'f'(food) 时,都应添加汉堡。我在我的代码中实现这一点时遇到问题。

我最初只是填充一个橙色矩形g2.fillRect(),但我想用实际图像替换它。我试过缩放图像并添加它,但它没有显示出来。如果我在 for 循环的末尾替换g2.fillRect,它会使每个方块都变成一个汉堡,而不仅仅是'f'

我该怎么做?

private class Game extends JPanel {

    BufferedImage img = null;

    @Override
    public void paintComponent(Graphics g2) {

        super.paintComponent(g2);

        try {

            img = ImageIO.read(new File("src/burger.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedImage otherImage = new BufferedImage(50, 50,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = otherImage.createGraphics();

        int width = getWidth() / game.getBoardWidth();
        int height = getHeight() / game.getBoardHeight();

        char[] symbols = game.toString().toCharArray();

        int x = 0;
        int y = 0;

        for (char c : symbols) {

            switch (c) {
            case 'Q':
                g2.setColor(Color.BLACK);
                break;
            case 'H':
                g2.setColor(Color.GREEN);
                break;
            case 'f':
                //g2.setColor(Color.ORANGE);
                g2.drawImage(img, x, y, width, height, null);
                break;
            case '-':
                g2.setColor(Color.WHITE);
                break;
            case '\n':
                y += height;
                x = 0; 
            }

           //g2.drawImage(img, x, y, width, height, null);
           g2.fillRect(x, y, width, height);
            x += width;
        }
    }

}

【问题讨论】:

    标签: java image swing switch-statement paintcomponent


    【解决方案1】:

    尚未测试,但这是您的程序中正在发生的事情:

    1. 获取符号
      • 如果 ('f') 然后 drawImage 然后 fillRect

    你想要的是:

    1. 获取符号
      • 如果('f') 则drawImage 否则fillRect

    所以你比较:

    if (symbol == 'f') {
        g2.drawImage(img, x, y, width, height, null);
    } else {
        g2.fillRect(x, y, width, height);
    }
    

    或者我会做的是:

    switch (c) {
        case 'Q':
            g2.setColor(Color.BLACK);
            g2.fillRect(x, y, width, height);
            break;
        case 'H':
            g2.setColor(Color.GREEN);
            g2.fillRect(x, y, width, height);
            break;
        case 'f':
            //g2.setColor(Color.ORANGE);
            g2.drawImage(img, x, y, width, height, null);
            break;
        case '-':
            g2.setColor(Color.WHITE);
            g2.fillRect(x, y, width, height);
            break;
        case '\n':
            y += height;
            x = 0; 
    }
    

    并注释掉或删除最后一次调用:

    g2.fillRect(x, y, width, height);
    

    提示:

    try {
        img = ImageIO.read(new File("src/burger.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    这些行应该在构造函数中,而不是在 paintComponent 方法中,Swing 会多次调用它,这样做可能会减慢或阻塞/冻结您的应用程序,直到它完成读取为止。

    另外,您没有使用 otherImage 变量,因此您也可以将其删除...

    【讨论】:

    • 效果很好@Frakcool。谢谢。您会推荐另一种读取图像的方法吗?
    • 是的,作为embedded-resource,我们也鼓励这样做,但正如我所说,在构造函数中这样做一次
    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多