Bonjour,

Je suis actuellement en train de faire un projet pour mes cours mais je suis bloqué sur la partie dessin.
Notre projet est de développer un Snake avec J2ME.

pour développer le snake, j'ai créé 3 classes :
* le Launcher qui est la middlet
* une classe Ecran qui hérite de la classe Canvas et qui calcul la résolution de l'écran et qui divise l'écran en ligne et colonnes en fonction de celle ci
* la classe Snake qui elle gère une matrice pour le snake, gère son comportement et le dessin.

Dans la théorie cela semble bien mais il y a un problème. Dans mon launcher, j'instancie la classe Snake. dans le constructeur du Snake, j’instancie l'Ecran pour pouvoir l'utiliser dans la classe Snake. Pour moi, la classe Ecran allait s'exécuter jusqu'au bout puis la classe snake aurait continuer après. Mais cela ne se passe pas comme ca. la méthode Paint() de l'Ecran s'exécute après le constructeur de Snake. Ce qui fausse tout.

Je voudrais savoir si il existe un moyen d'appeler la méthode paint de l'Ecran avant de finir mon constructeur du Snake ?

EDIT : J'ai essayer avec la méthode repaint() mais sans succès

Je vous joins mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
public class Launcher extends MIDlet {
 
	public Launcher() {
		// TODO Auto-generated constructor stub
	}
 
	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
 
	}
 
	protected void pauseApp() {
		// TODO Auto-generated method stub
 
	}
 
	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		Snake snake = new Snake(this);	
		Display.getDisplay(this).setCurrent(snake);
	}
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
public class Ecran extends Canvas
{
	private int widthScreen;
	private int heightScreen;
 
	private int sizeOfSnake;
	private int nbColumns;
	private int nbRows;
 
	public Ecran()
	{}
 
	public Ecran(int p_sizeOfSnake)
	{
		sizeOfSnake = p_sizeOfSnake;
	}
 
	protected void paint(Graphics g)
	{
		widthScreen = g.getClipWidth();
		heightScreen = g.getClipHeight();
 
		calcNbOfRowsColumns();
 
		// Affiche dans le log
		System.out.println();
		System.out.println("| **** Création de l'écran **** |");
		System.out.println("Résolution : " + widthScreen + "*" + heightScreen + "px");
		System.out.println("Nombre de colonnes : " + nbColumns);
		System.out.println("Nombre de lignes : " + nbRows);
	}
 
	protected void calcNbOfRowsColumns()
	{
		nbColumns = (widthScreen - 2) / sizeOfSnake;
		nbRows = (heightScreen - 2) / sizeOfSnake;
	}
 
	public int getWidthScreen()
	{
		return widthScreen;
	}
 
	public int getHeightScreen()
	{
		return heightScreen;
	}
 
	public int getSizeOfSnake()
	{
		return sizeOfSnake;
	}
 
	public int getNbColumns()
	{
		return nbColumns;
	}
 
	public int getNbRows()
	{
		return nbRows;
	}
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
public class Snake extends Canvas
{
	private Ecran screen;
	private int sizeOfSnake = 5;
 
	private int matrice[][] = new int[400][400];
 
 
	public Snake(Launcher mid)
	{
		screen = new Ecran(sizeOfSnake);;
		screen.repaint();
		Random r = new Random();
		int x, y;		
 
		Display.getDisplay(mid).setCurrent(screen);
 
		for(int i = 0; i < screen.getNbColumns(); i++)
			for(int j = 0; j < screen.getNbRows(); j++)
				matrice[j][i] = 0;
 
		x = (r.nextInt() % 100) + 1;
		if(x < 0)
			x *= -1;
 
		y = (r.nextInt() % 100) + 1;
		if(y < 0)
			y *= -1;
 
		matrice[x][y] = 9;
 
		x = (r.nextInt() % 100) + 1;
		if(x < 0)
			x *= -1;
 
		y = (r.nextInt() % 100) + 1;
		if(y < 0)
			y *= -1;
 
		matrice[x][y] = 3;
		matrice[x+1][y] = 2;
		matrice[x+2][y] = 1;
 
 
		System.out.println(screen.getNbRows()+" | "+screen.getNbColumns());
		for(int i = 0; i < screen.getNbRows(); i++)
		{
			for(int j = 0; j < screen.getNbColumns(); j++)
				System.out.println(matrice[j][i]);
 
			System.out.println();
		}
	}
 
	protected void paint(Graphics g)
	{
		System.out.println(screen.getNbRows()+" | "+screen.getNbColumns());
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, screen.getWidthScreen(), screen.getHeightScreen());
		g.setColor(0, 0, 0);
		g.fillRect(1, 1, screen.getNbColumns()*sizeOfSnake, screen.getNbRows()*sizeOfSnake);
 
		g.fillRect(5, 5, sizeOfSnake, sizeOfSnake);
	}
}
Merci d'avance,
Clément