Search code examples
javaswingawtgraphics2d

Java AWT/Swing : paintComponent issue with custom JPanel(s)


Ok, so basically, what I have so far:

  • A main class that creates a custom JFrame (ApplicationWindow).
  • An ApplicationWindow class that extends JFrame and acts as the window.
  • A MapDisplayPanel class that extends JPanel and is meant to (using GridLayout) display an 8x8 grid of:
  • A MapBlock class that extends JPanel.
  • MapBlocks are contained in a class to contain game data, GameData.java

It all seems to work, except that only one MapBlock is painted to screen.

Code:

Main.java

public class Main {

public static void main(String[] args) {
    final ApplicationWindow window = new ApplicationWindow();

    window.setVisible(true);
}
}

ApplicationWindow.java

public class ApplicationWindow extends JFrame {

public ApplicationWindow()
{
    setTitle("Heroes!");
    setLocationRelativeTo(null);
    setSize(800,600);
//  setLayout(new BorderLayout());

    JPanel map = new MapDisplayPanel();

    add(map);//, BorderLayout.CENTER);
}
}

MapDisplayPanel.java

public class MapDisplayPanel extends JPanel{
GameData game = null;

public MapDisplayPanel()
{
    game = new GameData();
    setLayout(new GridLayout(game.getWidth(),game.getHeight()));
    setBackground(Color.CYAN);

    MapBlock[][] map = game.getMap();
    for(MapBlock[] aBlk : map)
    {
        for(MapBlock blk : aBlk)
        {
            if(blk != null){add(blk);}
        }
    }
}
}

MapBlock.java

public class MapBlock extends JPanel{
private int xPos = -1, yPos = -1;

//Constructors
public MapBlock(int x, int y, int terrain)
{
    this.xPos = x;
    this.yPos = y;
    this.terrain = terrain;
    setPreferredSize(new Dimension(50,50));
}

//Methods
@Override
public void paintComponent(Graphics g)
{
    //setBackground(Color.GREEN);
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    g.setColor(Color.MAGENTA);
    g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);

    /*String out = Integer.toString(this.getX()) + Integer.toString(this.getY());
    System.out.println(out); THIS WAS FOR DEBUG*/
}

//Accessors, mutators
public int getTerrain()
{return terrain;}

public int getX()
{return xPos;}
public int getY()
{return yPos;}

}

And finally, GameData.java

public class GameData{

//Members
private MapBlock[][] map = null;
private int mapWidth = 8; private int mapHeight = 8;

//Constructors
public GameData()
{
    map = new MapBlock[mapWidth][mapHeight];
    for(int x = 0; x < mapWidth; x++)
    {
        for(int y = 0; y < mapHeight; y++)
        {
            map[x][y] = new MapBlock(x,y,1);
        }
    }
}

//Accessors, mutators
public MapBlock[][] getMap()
{return map;}

public int getWidth()
{return mapWidth;}

public int getHeight()
{return mapHeight;}

}

As I said, the problem is that only the top left MapBlock object is being drawn to screen. I have tested this hardcore, it seems that all the components are being properly added, and paintComponent is at least invoked for every one. Here is a picture of my output:

https://i.sstatic.net/x5fVR.jpg

Please help!!


Solution

  • You're overriding getX and getY in MapBlock which is being used by the layout manager to position all instances of the component

    public int getX() {
       return xPos;
    }
    
    public int getY() {
       return yPos;
    }
    

    Either remove them or rename the methods.