I'm having a weird error with my board game. The board consists of a 2D array with GameTiles, which is a subclass of JPanel. It all works perfectly fine when I have the size (600,500) but whenever I change it I get a weird error in the upper left corner.
Picture of the error (see the upper left corner)
What makes it even weirder is that when I created a new project, just to try it out, it worked perfectly. The code is exactly the same for the painting and I'm not experiencing any error with it. Can it be something else that's causing this problem?
PROBLEM FIXED
Anser: I accidentally overrided the getX and getY methods of JPanel. I changed the name on them and now it works perfectly.
Reversi.java
package org.reversi;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.reversi.gui.GameFrame;
public class Reversi {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GameFrame frame = new GameFrame("Reversi");
frame.setSize(600,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
GameBoard.java
package org.reversi.gui;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JPanel;
public class GameBoard extends JPanel{
private GameTile[][] gameBoard = new GameTile[8][8];
public GameBoard() {
initiateLayout();
}
private void initiateLayout() {
setLayout(new GridLayout(8, 8));
for(int row = 0 ; row < 8 ; row++) {
for(int col = 0 ; col < 8 ; col++) {
gameBoard[row][col] = new GameTile(col,row);
add(gameBoard[row][col]);
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int row = 0 ; row < 8 ; row++)
for(int col = 0 ; col < 8 ; col++)
gameBoard[row][col].repaint();
}
}
GameFrame.java
package org.reversi.gui;
import javax.swing.JFrame;
public class GameFrame extends JFrame {
private GameBoard gameBoard;
/**
* Creates a new frame.
* @param gameTitle the title of the frame
*/
public GameFrame(String gameTitle) {
setTitle(gameTitle);
gameBoard = new GameBoard();
add(gameBoard);
}
}
GameTile.java
package org.reversi.gui;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class GameTile extends JPanel {
private BufferedImage image;
private int x;
private int y;
public GameTile(int x, int y) {
this.x = x;
this.y = y;
try {
this.image = ImageIO.read(new File("tile.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Link to image (which should be named tile.png): http://i.imgur.com/ejmCtui.png