Skip to content

Instantly share code, notes, and snippets.

@jake7864
Created July 19, 2012 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jake7864/3141255 to your computer and use it in GitHub Desktop.
Save jake7864/3141255 to your computer and use it in GitHub Desktop.
a little upgrade to my stop watch program, this stop watch is now an alarm
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
This is my upgraded StopWatch program, this version has an alarm that sounds when the counter reaches 0, I
have got rid of all of the old comments & I have only commented on here where I have changed things from
version 1. To make this work you will have to put a wav file
with the name sound.wav into a folder then add that folder to the projects build path, if you right click
your java project in Eclipse then
go to properties & then click Java Build Path, then click the Libraries tab, then Click the Add Class Folder
button.
go to your project & select the folder you put sound.wav in, then click ok, then refresh(F5).
if you use netBeens or some other IDE then try googling "java change build path" + your IDE's name
*/
public class StopWatchV2 extends JFrame
{
//swing components
private JPanel panel = new JPanel();
private JTextField text = new JTextField();
private JLabel label = new JLabel();
private JLabel whiteLabel = new JLabel();
private JButton button = new JButton();
//constants
static final int WIDTH = 66;
static final int HEIGHT = 120;
static final int FPS = 100;
static final long OPTIMAL_TIME = 1000000000 / FPS;
//other
private boolean running = true;
private boolean countStart = false;
private boolean countOver = false;
private double counter = 0;
private Font font = new Font("Verdana", 0, 8);
private DecimalFormat numFormat = new DecimalFormat();
//new
public static synchronized void playSound(final String path)
{
new Thread(new Runnable()
{
public void run()
{
try
{
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(StopWatchV2.class.getResourceAsStream(path));
clip.open(inputStream);
clip.start();
}
catch (Exception e) {System.exit(0);}
}
}).start();
}
public static void main(String args[]){new StopWatchV2();}
public StopWatchV2()
{
setSize(new Dimension(WIDTH, HEIGHT));
setDefaultCloseOperation(3);
getContentPane().add(panel);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
panel.setLayout(null);
makeGui();
repaint();
long lastLoopTime = System.nanoTime();
while(running)
{
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double)OPTIMAL_TIME);
update(delta);
paintGui();
try{Thread.sleep((lastLoopTime-System.nanoTime())/1000000 + 10 );}
catch(Exception ex){}
}
}
public String customFormat(String pattern, double value)
{
numFormat.applyPattern(pattern);
return numFormat.format(value);
}
private void makeGui()
{
whiteLabel.setBounds(WIDTH/2-2, 0, WIDTH, HEIGHT);
label.setBounds(WIDTH/2-2, 8, WIDTH, 10);
text.setBounds(WIDTH/2-2, 33, WIDTH, 21);
button.setBounds(WIDTH/2-2, 62, WIDTH, 18);
text.setText("0.00");
label.setText("ALARM SET");//changed
button.setText("start");
label.setFont(font);
text.setBackground(Color.gray);
panel.setBackground(Color.black);
whiteLabel.setBackground(Color.white);
whiteLabel.setOpaque(true);
panel.add(label);
panel.add(text);
panel.add(button);
panel.add(whiteLabel);
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(countStart) countStart = false;
else
{
countStart = true;
try
{
counter = Integer.parseInt(text.getText());
}
catch(NumberFormatException ex) {text.setText("0.00");}
}
}
}
);
}
private void update(double delta)
{
if(countStart)
{
counter -= delta*.01;
if(counter <= 0)
{
counter = 0;
countStart = false;
countOver = true;
}
if(countOver) playSound("/sound.wav");//new, plays sound when the counter reaches 0
}
}
private void paintGui()
{
if(countStart)
{
text.setText(customFormat("0.00", counter));
text.setEnabled(false);
button.setText("stop");
}
else
{
text.setEnabled(true);
button.setText("start");
if(countOver) text.setText(customFormat("0.00", counter));
countOver = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment