0

我已经编写了一个 Sokoban 克隆作为我的学习计划的项目。自从升级到 JDK 7 后,我遇到了以下问题:当我JPanel第一次调用我的 repaint() 方法时,我的 JPanel 右侧出现一个“阴影图像”,如下所示:之前: 之后:

我不知道是什么原因造成的,所以不太清楚要添加哪个代码。这是paint()方法:

public void paint(Graphics g) {

    int x;
    int y;

    // draw column
    for (int k = 0; k < field.getField().length; k++) {
        y = (tileWidth * k); // Get y coordinate

        // draw line
        for (int l = 0; l < field.getField()[0].length; l++) {
            FieldObj now = field.getField()[k][l];

            x = (tileWidth * l); // Get x coordinate

            // Wall
            if (now instanceof Wall)
                g.drawImage(Wall, x, y, tileWidth, tileWidth, null);
            // Box
            else if (now instanceof Box && ((Box) now).getStandingOnGoal())
                g.drawImage(BoxOnG, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Box)
                g.drawImage(Box, x, y, tileWidth, tileWidth, null);
            // Man on Goal
            else if (now instanceof Man
                    && field.getMan().getStandingOnGoal()
                    && field.getMan().getOr() == 0)
                g.drawImage(ManGU, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man
                    && field.getMan().getStandingOnGoal()
                    && field.getMan().getOr() == 1)
                g.drawImage(ManGL, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man
                    && field.getMan().getStandingOnGoal()
                    && field.getMan().getOr() == 2)
                g.drawImage(ManGR, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man
                    && field.getMan().getStandingOnGoal()
                    && field.getMan().getOr() == 3)
                g.drawImage(ManGD, x, y, tileWidth, tileWidth, null);
            // Man
            else if (now instanceof Man && field.getMan().getOr() == 0)
                g.drawImage(ManU, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man && field.getMan().getOr() == 1)
                g.drawImage(ManL, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man && field.getMan().getOr() == 2)
                g.drawImage(ManR, x, y, tileWidth, tileWidth, null);
            else if (now instanceof Man && field.getMan().getOr() == 3)
                g.drawImage(ManD, x, y, tileWidth, tileWidth, null);
            // Floor
            else if (now instanceof Floor && ((Floor) now).getGoal())
                g.drawImage(Goal, x, y, tileWidth, tileWidth, null);
            else
                g.drawImage(Floor, x, y, tileWidth, tileWidth, null);
        }
    }

}

有什么建议么?你想看的其他代码?我会很感激任何帮助。一个

4

1 回答 1

2

不要覆盖paint()!!!

自定义绘画是通过重写paintComponent()方法完成的,第一个语句通常应该是:

super.paintComponent(g);

确保在绘画之前清除背景。

阅读有关自定义绘画的 Swing 教程以了解基础知识。

于 2013-09-20T19:28:53.427 回答