Ok, donc en gros, ce que j'ai jusqu'à présent:
- Une classe principale qui crée un JFrame (ApplicationWindow) personnalisé.
- Une classe ApplicationWindow qui étend JFrame et agit en tant que fenêtre.
- Une classe MapDisplayPanel qui étend JPanel et est conçue pour (avec GridLayout) afficher une grille 8x8 de:
- Une classe MapBlock qui étend JPanel.
- Les MapBlocks sont contenus dans une classe contenant les données du jeu, GameData.java.
Tout semble fonctionner, sauf qu'un seul MapBlock est peint à l'écran.
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;}
}
Et enfin, 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;}
}
Comme je le disais, le problème est que seul le coin supérieur gaucheL'objet MapBlock est dessiné à l'écran. J'ai testé ce hardcore, il semble que tous les composants sont correctement ajoutés, et paintComponent est au moins invoqué pour chacun. Voici une photo de ma sortie:
S'il vous plaît aider!
Réponses:
4 pour la réponse № 1Vous êtes primordial getX
et getY
dans MapBlock
qui est utilisé par le gestionnaire de disposition pour positionner toutes les instances du composant
public int getX() {
return xPos;
}
public int getY() {
return yPos;
}
Supprimez-les ou renommez les méthodes.