Mercurial > hg > ristipolku
diff 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 |
line wrap: on
line diff
--- a/game/Engine.java Mon Jan 31 08:13:11 2011 +0200 +++ b/game/Engine.java Mon Jan 31 16:05:36 2011 +0200 @@ -18,6 +18,83 @@ import javax.sound.sampled.*; +enum Sound { + PIECE_PLACED("placed.wav"), + MUSIC_GAME("gamemusic.wav"); + + final String name; + private Clip clip; + + Sound(String name) + { + this.name = name; + } + + public boolean initialized() + { + return clip != null; + } + + public static void load(String path) throws IOException + { + for (Sound snd : Sound.values()) { + String filename = path + snd.name; + ResourceLoader res = new ResourceLoader(filename); + if (res == null || res.getStream() == null) + { + throw new IOException("Could not load audio resource '"+filename+"'.\n"); + } + try { + AudioInputStream is = AudioSystem.getAudioInputStream(res.getStream()); + } + catch (UnsupportedAudioFileException e) { + throw new IOException("Unsupported audio file format for '"+filename+"'.\n"); + } + catch (IOException e) + { + throw new IOException("Could not load audio resource '"+filename+"'.\n"); + } + } + } + + public void play() + { + if (clip == null) + return; + + clip.setFramePosition(0); + clip.start(); + } + + public void loop(int n) + { + if (clip == null) + return; + + if (n < 0) + clip.loop(Clip.LOOP_CONTINUOUSLY); + else + clip.loop(n); + } + + public void stop() + { + if (clip == null) + return; + + if (clip.isRunning()) + clip.stop(); + } + + public boolean isPlaying() + { + if (clip == null) + return false; + + return clip.isRunning(); + } +} + class PathInfo { public int in, inX, inY, out, outX, outY; @@ -194,7 +271,7 @@ public class Engine extends JPanel - implements Runnable, KeyListener + implements Runnable, KeyListener, MouseListener { Thread animThread; boolean animEnable = false; @@ -202,6 +279,7 @@ BufferedImage lautaBG = null, lautaBGScaled = null; Dimension oldDim; float clock; + Sound sounds; public Engine() { @@ -212,20 +290,33 @@ try { - lautaBG = ImageIO.read(new File("board.png")); + ResourceLoader res = new ResourceLoader("graphics/board.png"); + lautaBG = ImageIO.read(res.getStream()); + sounds.load("sounds/"); } catch (IOException e) { - System.out.print("lol\n"); +/* JOptionPane.showMessageDialog(null, - "Could not load background image.", + "Could not load some of the resources.", "Initialization error", JOptionPane.ERROR_MESSAGE); +*/ + System.out.print("Could not load some of the resources.\n"); + } + + lauta = new GameBoard(); + addKeyListener(this); + addMouseListener(this); + + // Get initial focus + if (!hasFocus()) + { + System.out.print("Engine(): requesting focus\n"); + requestFocus(); } - lauta = new GameBoard(); - addKeyListener(this); - + sounds.MUSIC_GAME.loop(-1); } public void startThreads() @@ -250,6 +341,21 @@ } } + public void mousePressed(MouseEvent e) { } + public void mouseEntered(MouseEvent e) { } + public void mouseExited(MouseEvent e) { } + public void mouseReleased(MouseEvent e) { } + + public void mouseClicked(MouseEvent e) + { + System.out.print("mouseClicked()\n"); + if (!hasFocus()) + { + System.out.print("requesting focus\n"); + requestFocus(); + } + } + public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; @@ -277,13 +383,6 @@ lauta.paint(g2, 100, 150, 60); // Other elements - - // Request focus to keep keyboard input coming - if (!hasFocus()) - { - System.out.print(".\n"); - requestFocus(); - } } public void keyTyped(KeyEvent e)