rendered paste body/* * To change this template, choose Tools | Templates * and open the template in the editor. */package timer;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.Timer;import javax.sound.sampled.*;import java.io.*;/** * * @author tarsonis */public class Window extends JFrame implements MouseListener { private JTextField hours, minutes, seconds; private JButton start, reset, pause; private JPanel top, bottom; private Timer time; private int ihours = 0, iminutes = 0, iseconds = 0; private boolean finished = false; private Clip readySound; public Window() { super("Timer - (c) tarsonis, 2008"); this.init(); } private void init() { this.setSize(400, 130); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(new BorderLayout()); this.initPanels(); this.initFields(); this.initButtons(); this.initSound(); time = new Timer(); this.setVisible(true); } private void initPanels() { top = new JPanel(); top.setSize(400,80); this.add(top, BorderLayout.NORTH); bottom = new JPanel(); bottom.setSize(400,50); this.add(bottom, BorderLayout.SOUTH); } private void initFields() { JLabel seperator1 = new JLabel(":"); seperator1.setFont(new Font("Verdana",Font.BOLD,42)); JLabel seperator2 = new JLabel(":"); seperator2.setFont(new Font("Verdana",Font.BOLD,42)); hours = new JTextField(); this.setHours(ihours); hours.setFont(new Font("Verdana",Font.BOLD,42)); minutes = new JTextField(); this.setMinutes(iminutes); minutes.setFont(new Font("Verdana",Font.BOLD,42)); seconds = new JTextField(); this.setSeconds(iseconds); seconds.setFont(new Font("Verdana",Font.BOLD,42)); top.add(hours); top.add(seperator1); top.add(minutes); top.add(seperator2); top.add(seconds); } private void initButtons() { start = new JButton("Start"); start.addMouseListener(this); bottom.add(start); reset = new JButton("Reset"); reset.addMouseListener(this); bottom.add(reset); pause = new JButton("Pause"); pause.addMouseListener(this); bottom.add(pause); } private void initSound() { try { AudioInputStream ais = AudioSystem.getAudioInputStream( new File("default.wav")); AudioFormat af = ais.getFormat(); int size = (int) (af.getFrameSize() * ais.getFrameLength()); byte[] audio = new byte[size]; DataLine.Info info = new DataLine.Info(Clip.class, af, size); ais.read(audio, 0, size); readySound = (Clip) AudioSystem.getLine(info); readySound.open(af, audio, 0, size); } catch (Exception e) { JOptionPane.showMessageDialog(new Frame(), "File default.wav not found or not suitable.\n" + "Please use a RAW WAVE File.", "File not found", JOptionPane.ERROR_MESSAGE); System.exit(0); } } public void mouseClicked(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON1) { if(e.getSource() == start && !finished) { try { iseconds = Integer.parseInt(seconds.getText()); } catch (NumberFormatException ex) { iseconds = 0; } try { iminutes = Integer.parseInt(minutes.getText()); } catch (NumberFormatException ex) { iminutes = 0; } try { ihours = Integer.parseInt(hours.getText()); } catch (NumberFormatException ex) { ihours = 0; } while(iseconds > 60) { iminutes += 1; iseconds -= 60; this.setSeconds(iseconds); } while(iminutes > 60) { ihours += 1; iminutes -= 60; this.setMinutes(iminutes); this.setHours(ihours); } time.scheduleAtFixedRate(new TimeTask(ihours, iminutes,iseconds,this), 0, 1000); this.changeEditable(); } else if (e.getSource() == reset) { this.finished = false; this.resetTimer(); hours.setText("00"); minutes.setText("00"); seconds.setText("00"); this.changeEditable(); } else if (e.getSource() == pause) { this.resetTimer(); iseconds = Integer.parseInt(seconds.getText()); iminutes = Integer.parseInt(minutes.getText()); ihours = Integer.parseInt(hours.getText()); } } } public void jobFinished() { this.finished = true; this.resetTimer(); readySound.start(); readySound.setFramePosition(0); JOptionPane.showMessageDialog(new Frame(), "Time is up!", "Information", JOptionPane.INFORMATION_MESSAGE); } public void resetTimer() { time.cancel(); time = null; time = new Timer(); } public void changeEditable() { hours.setEditable(!hours.isEditable()); minutes.setEditable(!minutes.isEditable()); seconds.setEditable(!seconds.isEditable()); } public void setSeconds(int s) { String st = ""; if(s < 10) { st = "0"; } this.seconds.setText(st+s); } public void setMinutes(int m) { String s = ""; if(m<10) { s = "0"; } this.minutes.setText(s+m); } public void setHours(int h) { String s = ""; if(h < 10) { s = "0"; } this.hours.setText(s+h); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { }}