2

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.stack.imgur.com/x5fVR.jpg

Please help!!

S.V.
  • 1,181
  • 6
  • 23
  • As it stands, your `paintComponent` is doing nothing that changing the background color of the panel and add a `LineBorer` wouldn't achieve. I assume that this is test code you going to add more functionality to the `MapBlock#paintComponent` ... – MadProgrammer May 12 '13 at 23:41
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) Don't set the size of top level containers. Instead layout the content & call `pack()`. 4) Don't extend frame or other top level containers. Instead create & use an instance of one. 5) For frame positioning, you cannot go by `setLocationByPlatform(true)`. See [this answer](http://stackoverflow.com/a/7143398/418556) for demo. – Andrew Thompson May 13 '13 at 04:30

1 Answers1

4

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.

Reimeus
  • 158,255
  • 15
  • 216
  • 276