Mercurial > hg > ristipolku
comparison game/Engine.java @ 18:4507a431b410
More work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 31 Jan 2011 16:05:36 +0200 |
parents | 59ff451750fb |
children | df494a65bf8c |
comparison
equal
deleted
inserted
replaced
17:0dfa894b873d | 18:4507a431b410 |
---|---|
16 import game.*; | 16 import game.*; |
17 | 17 |
18 import javax.sound.sampled.*; | 18 import javax.sound.sampled.*; |
19 | 19 |
20 | 20 |
21 enum Sound { | |
22 PIECE_PLACED("placed.wav"), | |
23 MUSIC_GAME("gamemusic.wav"); | |
24 | |
25 final String name; | |
26 private Clip clip; | |
27 | |
28 Sound(String name) | |
29 { | |
30 this.name = name; | |
31 } | |
32 | |
33 public boolean initialized() | |
34 { | |
35 return clip != null; | |
36 } | |
37 | |
38 public static void load(String path) throws IOException | |
39 { | |
40 for (Sound snd : Sound.values()) { | |
41 String filename = path + snd.name; | |
42 ResourceLoader res = new ResourceLoader(filename); | |
43 if (res == null || res.getStream() == null) | |
44 { | |
45 throw new IOException("Could not load audio resource '"+filename+"'.\n"); | |
46 } | |
47 try { | |
48 AudioInputStream is = AudioSystem.getAudioInputStream(res.getStream()); | |
49 } | |
50 catch (UnsupportedAudioFileException e) { | |
51 throw new IOException("Unsupported audio file format for '"+filename+"'.\n"); | |
52 } | |
53 catch (IOException e) | |
54 { | |
55 throw new IOException("Could not load audio resource '"+filename+"'.\n"); | |
56 } | |
57 } | |
58 } | |
59 | |
60 public void play() | |
61 { | |
62 if (clip == null) | |
63 return; | |
64 | |
65 clip.setFramePosition(0); | |
66 clip.start(); | |
67 } | |
68 | |
69 public void loop(int n) | |
70 { | |
71 if (clip == null) | |
72 return; | |
73 | |
74 if (n < 0) | |
75 clip.loop(Clip.LOOP_CONTINUOUSLY); | |
76 else | |
77 clip.loop(n); | |
78 } | |
79 | |
80 public void stop() | |
81 { | |
82 if (clip == null) | |
83 return; | |
84 | |
85 if (clip.isRunning()) | |
86 clip.stop(); | |
87 } | |
88 | |
89 public boolean isPlaying() | |
90 { | |
91 if (clip == null) | |
92 return false; | |
93 | |
94 return clip.isRunning(); | |
95 } | |
96 } | |
97 | |
21 class PathInfo | 98 class PathInfo |
22 { | 99 { |
23 public int in, inX, inY, out, outX, outY; | 100 public int in, inX, inY, out, outX, outY; |
24 | 101 |
25 public PathInfo(int in, int inX, int inY, int out, int outX, int outY) | 102 public PathInfo(int in, int inX, int inY, int out, int outX, int outY) |
192 } | 269 } |
193 } | 270 } |
194 | 271 |
195 | 272 |
196 public class Engine extends JPanel | 273 public class Engine extends JPanel |
197 implements Runnable, KeyListener | 274 implements Runnable, KeyListener, MouseListener |
198 { | 275 { |
199 Thread animThread; | 276 Thread animThread; |
200 boolean animEnable = false; | 277 boolean animEnable = false; |
201 GameBoard lauta = null; | 278 GameBoard lauta = null; |
202 BufferedImage lautaBG = null, lautaBGScaled = null; | 279 BufferedImage lautaBG = null, lautaBGScaled = null; |
203 Dimension oldDim; | 280 Dimension oldDim; |
204 float clock; | 281 float clock; |
282 Sound sounds; | |
205 | 283 |
206 public Engine() | 284 public Engine() |
207 { | 285 { |
208 BufferedImage img; | 286 BufferedImage img; |
209 clock = 0; | 287 clock = 0; |
210 | 288 |
211 System.out.print("Engine() constructor\n"); | 289 System.out.print("Engine() constructor\n"); |
212 | 290 |
213 try | 291 try |
214 { | 292 { |
215 lautaBG = ImageIO.read(new File("board.png")); | 293 ResourceLoader res = new ResourceLoader("graphics/board.png"); |
294 lautaBG = ImageIO.read(res.getStream()); | |
295 sounds.load("sounds/"); | |
216 } | 296 } |
217 catch (IOException e) | 297 catch (IOException e) |
218 { | 298 { |
219 System.out.print("lol\n"); | 299 /* |
220 JOptionPane.showMessageDialog(null, | 300 JOptionPane.showMessageDialog(null, |
221 "Could not load background image.", | 301 "Could not load some of the resources.", |
222 "Initialization error", | 302 "Initialization error", |
223 JOptionPane.ERROR_MESSAGE); | 303 JOptionPane.ERROR_MESSAGE); |
224 } | 304 */ |
225 | 305 System.out.print("Could not load some of the resources.\n"); |
306 } | |
307 | |
226 lauta = new GameBoard(); | 308 lauta = new GameBoard(); |
227 addKeyListener(this); | 309 addKeyListener(this); |
228 | 310 addMouseListener(this); |
311 | |
312 // Get initial focus | |
313 if (!hasFocus()) | |
314 { | |
315 System.out.print("Engine(): requesting focus\n"); | |
316 requestFocus(); | |
317 } | |
318 | |
319 sounds.MUSIC_GAME.loop(-1); | |
229 } | 320 } |
230 | 321 |
231 public void startThreads() | 322 public void startThreads() |
232 { | 323 { |
233 System.out.print("startThreads()\n"); | 324 System.out.print("startThreads()\n"); |
245 if (animThread != null) | 336 if (animThread != null) |
246 { | 337 { |
247 animThread.interrupt(); | 338 animThread.interrupt(); |
248 animEnable = false; | 339 animEnable = false; |
249 animThread = null; | 340 animThread = null; |
341 } | |
342 } | |
343 | |
344 public void mousePressed(MouseEvent e) { } | |
345 public void mouseEntered(MouseEvent e) { } | |
346 public void mouseExited(MouseEvent e) { } | |
347 public void mouseReleased(MouseEvent e) { } | |
348 | |
349 public void mouseClicked(MouseEvent e) | |
350 { | |
351 System.out.print("mouseClicked()\n"); | |
352 if (!hasFocus()) | |
353 { | |
354 System.out.print("requesting focus\n"); | |
355 requestFocus(); | |
250 } | 356 } |
251 } | 357 } |
252 | 358 |
253 public void paintComponent(Graphics g) | 359 public void paintComponent(Graphics g) |
254 { | 360 { |
275 // Background, pieces | 381 // Background, pieces |
276 g2.drawImage(lautaBGScaled, 0, 0, null); | 382 g2.drawImage(lautaBGScaled, 0, 0, null); |
277 lauta.paint(g2, 100, 150, 60); | 383 lauta.paint(g2, 100, 150, 60); |
278 | 384 |
279 // Other elements | 385 // Other elements |
280 | |
281 // Request focus to keep keyboard input coming | |
282 if (!hasFocus()) | |
283 { | |
284 System.out.print(".\n"); | |
285 requestFocus(); | |
286 } | |
287 } | 386 } |
288 | 387 |
289 public void keyTyped(KeyEvent e) | 388 public void keyTyped(KeyEvent e) |
290 { | 389 { |
291 } | 390 } |