When I override the paint method of my GamePanel class, then, when I try to open the MazeGUI Design Tab, I get this error:
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)
If I delete this line at GamePanel.java at paint override function
g.drawImage(maze.wall, 400, 400, null);
the design tab at MazeGUI already works. Very strange...
Anyway, if I run the java application, it displays the image without any errors.
Here I leave both classes java files: 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);
}
}
MazeGUI.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();
}
}
}
Ah, let me add this:
- I think all this problem is related to the loadImages function at MazeGUI, but I think I do it all alright. Already lost so many hours trying to fix this.
- Keep in mind: no error when I run the app (it even shows the image in the app) but I get the error above when I try to open the Design tab :/