Steamworker
Messages postés16Date d'inscriptionmardi 10 août 2010StatutMembreDernière intervention12 août 2010
-
12 août 2010 à 10:03
Steamworker
Messages postés16Date d'inscriptionmardi 10 août 2010StatutMembreDernière intervention12 août 2010
-
12 août 2010 à 10:15
Bonjour,
Hypothèse 1 : J'ai construit un une interface swing basique.
package net.sobert.JavaCities.UI;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.sobert.JavaCities.core.District;
import net.sobert.JavaCities.core.Road;
import net.sobert.JavaCities.core.Util;
public class GameWindow extends JScrollPane {
/**
*
*/
private static final long serialVersionUID = -3683600883622540901L;
public JPanel panel;
public JPanel[] panelGraphs;
private MainWindow mw;
private ImageIcon tile_empty_land ;
private ImageIcon tile_road_v ;
private ImageIcon tile_habit_1 ;
public GameWindow(MainWindow parent) {
//Init Tiles
tile_empty_land = new ImageIcon(UIConst.TILE_LAND_EMPTY);
tile_road_v = new ImageIcon(UIConst.TILE_ROAD_V);
tile_habit_1 = new ImageIcon(UIConst.TILE_HABIT_LV1);
this.mw = parent;
panel = new JPanel();
panel.setVisible(true);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panelGraphs = new JPanel[Util.SIZE_X];
for (int i = 0 ; i < Util.SIZE_X ; i++) {
panelGraphs[i] = new JPanel();
panelGraphs[i].setVisible(true);
panelGraphs[i].setLayout(new BoxLayout(panelGraphs[i], BoxLayout.Y_AXIS));
panel.add(panelGraphs[i]);
}
this.getViewport().add(panel);
this.initTiles();
refreshTiles();
this.setVisible(true);
}
public void refreshTiles() {
resetTiles();
}
private void initTiles() {
for ( int i = 0 ; i < Util.SIZE_X ; i++) {
for ( int j = 0 ; j < Util.SIZE_Y ; j++) {
panelGraphs[i].add(new LandTile(i, j, mw, this));
}
}
}
private void resetTiles() {
for ( int i = 0 ; i < Util.SIZE_X ; i++) {
for ( int j = 0 ; j < Util.SIZE_Y ; j++) {
LandTile lt = getTile(i,j);
lt.setIcon(tile_empty_land);
}
}
}
private LandTile getTile(int x, int y) {
return (LandTile)panelGraphs[x].getComponentAt(0, y*UIConst.TILE_Y);
}
public void SelectLand(LandTile tile) {
LandTile tileSele = getTile(tile.X, tile.Y);
tileSele.setIcon(new ImageIcon(UIConst.TILE_LAND_SELECTED)) ;
}
}
LandTile se construit comme suit (héritant de JLabel) :
public LandTile(int X, int Y, MainWindow parent, GameWindow gw) {
super(new ImageIcon(UIConst.TILE_LAND_EMPTY));
this.parent = parent ;
this.gw = gw ;
this.X = X ;
this.Y = Y ;
this.setToolTipText(X+":"+Y);
Problème 1 : Quand j'execute, j'ai un NullPointerException avec la pile ci-dessous :
Exception in thread "main" java.lang.NullPointerException
at net.sobert.JavaCities.UI.GameWindow.resetTiles(GameWindow.java:66)
at net.sobert.JavaCities.UI.GameWindow.refreshTiles(GameWindow.java:49)
at net.sobert.JavaCities.UI.GameWindow.(GameWindow.java:44)
at net.sobert.JavaCities.UI.MainWindow.(MainWindow.java:33)
at net.sobert.JavaCities.launcher.Launcher.main(Launcher.java:14)
Solution 1 : J'ai tenté de mettre un sleep entre le initTiles et le refreshTiles, me disant qu'il fallait peut-être un peu de temps avant que les ressources ne soit disponibles. Echec !
Hypothèse 2 : A la base, j'avais fait une grossière erreur de conception. Le refreshTiles était l'équivalent de la méthode initTiles (bonjour la mémoire saturée au bout d'un peu de temps). Par contre la méthode de récupération des LandTiles (getTile) fonctionne, car SelectTile fonctionnait parfaitement.
Problème 2 : En chassant cette surcharge mémoire, j'ai donc séparé initTiles et refreshTiles. Depuis, je m'arrache les cheveux.
Steamworker
Messages postés16Date d'inscriptionmardi 10 août 2010StatutMembreDernière intervention12 août 2010 12 août 2010 à 10:10
J'ai oublié une petite précision :
Lorsque j'execute en pas à pas, mon panelGraphs est bien rempli des LandTiles lorsque j'arrive à la méthode getTile.
Le problème c'est que le getComponentAt ne réussi pas trop.
UIConst.TILE_Y = 48 sinon, ce qui est la taille de mes LandTiles.