Note that there are some explanatory texts on larger screens.

plurals
  1. POJPanel displaying weird error when drawing image
    primarykey
    data
    text
    <p>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.</p> <p>Picture of the error (see the upper left corner) <img src="https://i.imgur.com/Ty6Vi4o.png" alt="Board error"></p> <p>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?</p> <p><strong>PROBLEM FIXED</strong></p> <p>Anser: I accidentally overrided the getX and getY methods of JPanel. I changed the name on them and now it works perfectly.</p> <p>Reversi.java</p> <pre><code>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); } }); } } </code></pre> <p>GameBoard.java</p> <pre><code>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 &lt; 8 ; row++) { for(int col = 0 ; col &lt; 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 &lt; 8 ; row++) for(int col = 0 ; col &lt; 8 ; col++) gameBoard[row][col].repaint(); } } </code></pre> <p>GameFrame.java</p> <pre><code>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); } } </code></pre> <p>GameTile.java</p> <pre><code>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; } } </code></pre> <p>Link to image (which should be named tile.png): <a href="https://i.imgur.com/ejmCtui.png" rel="nofollow noreferrer">http://i.imgur.com/ejmCtui.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload