-2

当我覆盖我的 GamePanel 类的 paint 方法时,当我尝试打开 MazeGUI 设计选项卡时,我收到此错误:

Internal Error
WindowBuilder encountered unexpected internal error. 

This could be caused by a WindowBuilder bug or by a misconfiguration issue, conflict,  partial update, etc.

Stack trace:
java.lang.NullPointerException
at maze.gui.GamePanel.paint(GamePanel.java:25)  
at javax.swing.JComponent.print(Unknown Source)  
at javax.swing.JComponent.paintChildren(Unknown Source)  
at javax.swing.JComponent.printChildren(Unknown Source)  
at javax.swing.JComponent.paint(Unknown Source)  
at javax.swing.JComponent.print(Unknown Source)  
at javax.swing.JComponent.paintChildren(Unknown Source)  
at javax.swing.JComponent.printChildren(Unknown Source)  
at javax.swing.JComponent.paint(Unknown Source)  

如果我在绘画覆盖函数中删除 GamePanel.java 中的这一行

g.drawImage(maze.wall, 400, 400, null);

MazeGUI 的设计选项卡已经可以使用。很奇怪...

无论如何,如果我运行 java 应用程序,它会显示图像而没有任何错误。

在这里,我留下两个类 java 文件:GamePanel.java:

package maze.gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GamePanel extends JPanel {
    private int comprimento_sprites;
    private MazeGUI maze;

/**
 * Create the panel.
 */
public GamePanel(MazeGUI m) {
    comprimento_sprites = 40;
    maze = m;
    setFocusable(true);
}

@Override
public void paint(Graphics g) {
    super.paint(g);

g.drawImage(maze.wall, 400, 400, null);
}

}

迷宫GUI.java:

package maze.gui;

import java.awt.EventQueue;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import maze.logic.Jogo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MazeGUI {

private JFrame frmMazeGame;
//IMAGES
    public BufferedImage wall;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MazeGUI window = new MazeGUI();
                window.frmMazeGame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MazeGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    //faz load das imagens do jogo
    loadImages();

    frmMazeGame = new JFrame();
    frmMazeGame.setTitle("Maze Game");
    frmMazeGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GamePanel tabuleiro = new GamePanel(this);
    frmMazeGame.getContentPane().add(tabuleiro);
}

private void loadImages(){

    try {
        wall = ImageIO.read(new File("images/bloco.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

啊,让我补充一下: - 我认为所有这些问题都与 MazeGUI 的 loadImages 功能有关,但我认为我做得很好。已经失去了很多小时试图解决这个问题。
- 请记住:运行应用程序时没有错误(它甚至在应用程序中显示图像)但是当我尝试打开“设计”选项卡时出现上述错误:/

4

1 回答 1

0

由于此行,您在加载设计选项卡时收到错误:

g.drawImage(maze.wall, 400, 400, null);

MazeGUI我对 WindowBuilder 没有太多经验,所以我不能告诉你它是如何工作的,但看起来你的对象好像GamePanelnull在设计模式下。这不是您的图像加载的问题,这仅仅是因为 WindowBuilder 的工作原理。

要加载设计选项卡,您只需null在其中进行检查:

if (maze != null && maze.wall != null) {
    g.drawImage(maze.wall, 400, 400, null);
}

至于让图像实际显示在“设计”选项卡中,恐怕我帮不了你。

于 2013-04-10T22:25:07.003 回答