Bonjour ,
Mon programme est simple , c'est une interface graphique avec un sprite qui marche.
J'ai une erreur que je n'arrive pas a identifier dans mon programme, a chaque fois il me balance à l'excution :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at robotGraphique.graphicalRenderer(robotGraphique.java:104)
at robotGraphique$1.windowOpened(robotGraphique.java:55)
at java.awt.Window.processWindowEvent(Window.java:1187)
at javax.swing.JFrame.processWindowEvent(JFrame.java:266)
at java.awt.Window.processEvent(Window.java:1148)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; public class robotGraphique implements KeyListener{ //nom des fichiers sprites a charger private static final String strImgMove = "024.png"; private static final String strImgDie = "dead.png"; //Index des images de deplacement du robot private static final int IMG_DOWN = 0; private static final int IMG_LEFT = 1; private static final int IMG_RIGHT = 2; private static final int IMG_UP = 3; //coordonées maximum du robot private final static int MAX_HEIGHT=520; private final static int MIN_HEIGHT=200; private final static int MAX_WIDTH=740; private final static int MIN_WIDTH=0; //coordonnée du robot private int x=MAX_WIDTH/2; private int y=MAX_HEIGHT/2; private BufferStrategy strategy; private Graphics buffer; private Image[][] imgTab; private Image img; private Image imgDie; private JFrame fenetre; private MediaTracker MT; public robotGraphique(){ imgTab = new Image[4][4]; //On creer une nouvelle fenetre fenetre = new JFrame("Controle robot"); //On lui definit un fond d'ecran gris fenetre.setBackground(Color.GRAY); //On récupére la taille de l'ecran Dimension dimEcran = Toolkit.getDefaultToolkit().getScreenSize(); fenetre.setSize(800,600); //On definit la place de la fenetre sur l'ecran fenetre.setLocation((dimEcran.width-800)/2,(dimEcran.height-600)/2); //On la rend visible fenetre.setVisible(true); //On ajoute le KeyListener et WindowListener fenetre.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){fenetre.dispose();} public void windowActivated(WindowEvent evt){graphicalRenderer();} public void windowOpened(WindowEvent e){graphicalRenderer();} public void windowClosed(WindowEvent evt){System.exit(0);}} ); fenetre.addKeyListener(this); //On gére nous meme l'affichage fenetre.setIgnoreRepaint(true); //Mise en place du buffering fenetre.createBufferStrategy(2); strategy = fenetre.getBufferStrategy(); //On charge les images dans le buffer imageLoad(); //On verifie que les images soient chargées if(MT.checkAll()==false) System.exit(-48000); graphicalRenderer(); } public static void main(String args[]){ new robotGraphique(); } private void imageLoad(){ int index=0; CropImageFilter c; MT =new MediaTracker(fenetre); //On charge notre image img = Toolkit.getDefaultToolkit().getImage(strImgMove); for(int ligne=0;ligne<4;ligne++){ for(int col=0;col<4;col++){ c = new CropImageFilter(32*col,48*ligne,32,48); imgTab[ligne][col] =fenetre.createImage( new FilteredImageSource(img.getSource(), c)); //ajoute l'image au media tracker [index est une reference sur l'image dans MT] MT.addImage(imgTab[ligne][col],index); } } img = Toolkit.getDefaultToolkit().getImage(strImgDie); c = new CropImageFilter(0,0,32,48); img = imgDie = fenetre.createImage( new FilteredImageSource(img.getSource(), c)); MT.addImage(imgDie,index); try { MT.waitForAll(); } catch (InterruptedException e) { System.out.println("MT.waitForAll() "+e); } } public void graphicalRenderer(){ buffer = strategy.getDrawGraphics(); buffer.setColor(Color.LIGHT_GRAY); buffer.fillRect(0,0,800,600); buffer.drawImage(img,x,y,null); strategy.show(); } public void keyPressed(KeyEvent key){ } public void keyReleased(KeyEvent key){ deplace(key.getKeyCode()); } public void keyTyped(KeyEvent key){} public void deplace(int keyValue){ int i=0; switch(keyValue){ case 37:{ do{ i++; if(i==4) i=0; try{ img = imgTab[IMG_LEFT][i]; x-=2; if(x<MIN_WIDTH) x=MIN_WIDTH; Thread.sleep(125); graphicalRenderer(); }catch(Exception e){ System.out.println("MOVE LEFT : "+e); } }while(i!=0); break; } case 38:{ do{ i++; if(i==4) i=0; try{ img = imgTab[IMG_UP][i]; y-=2; if(y<MIN_HEIGHT) y=MIN_HEIGHT; Thread.sleep(125); graphicalRenderer(); }catch(Exception e){ System.out.println("MOVE UP : "+e); } }while(i!=0); break; } case 39:{ do{ i++; if(i==4) i=0; try{ img = imgTab[IMG_RIGHT][i]; x+=2; if(x>MAX_WIDTH) x=MAX_WIDTH; Thread.sleep(125); graphicalRenderer(); }catch(Exception e){ System.out.println("MOVE RIGHT : "+e); } }while(i!=0); break; } case 40:{ do{ i++; if(i==4) i=0; try{ img = imgTab[IMG_DOWN][i]; y+=2; if(y>MAX_HEIGHT) y=MAX_HEIGHT; Thread.sleep(125); graphicalRenderer(); }catch(Exception e){ System.out.println("MOVE DOWN : "+e); } }while(i!=0); break; } case 35:{ img = imgDie; graphicalRenderer(); } } } }
Partager