Cześć.
Mam problem z budzikiem.
Problem polega na tym, że po ustawieniu godziny w której powinien zacząć dzwonić nie dzieje się totalnie nic.
import java.awt.*; // see above for details of
import java.util.*; // the classes and methods used
public class ClockAlarm extends Frame implements Runnable
{
Thread clockThread; // thread to do animation etc
int alarmh; // alarm hours (-1 if not set)
int alarmm; // alarm minutes
Image tickimages[]; // image array for tick-tock animations
Image bongimages[]; // image array for alarm ringing
int tick; // animation counter
boolean bong = false; // true when alarm ringing
Button qb, ab; // buttons to exit and set/stop alarm
TextField tstr; // displays current time (hh:mm:ss)
TextField astr; // displays alarm time (hh:mm)
static final int ALARM_IMAGES = 6; // number of alarm frames
static final int TICK_IMAGES = 2; // number of tick-tock frames
static final int ALARM_TICK = 150; // frame display time for alarm
static final int CLOCK_TICK = 1000; // frame dislay time for tick-tock
public static void main(String args[])
{
new ClockAlarm(); // make an alarm clock
}
// constructor
public ClockAlarm() {
super("Java ClockAlarm");
Panel imgpanel;
int i;
// pick a common font
Font f = new Font("TimesRoman", Font.PLAIN, 12);
setFont(f); // give the Frame a font
setBackground(Color.LIGHT_GRAY); // and a background colour
tick = 0;
// we need to load images - so we need a Toolkit
Toolkit t = Toolkit.getDefaultToolkit();
// load the tick-tock images
tickimages = new Image[TICK_IMAGES];
for (i =0 ; i < TICK_IMAGES; i++)
tickimages[i] = t.getImage("tick"+ (i+1) + "zegarek.gif");
// load the alarm images
bongimages = new Image[ALARM_IMAGES];
for (i =0 ; i < ALARM_IMAGES; i++)
bongimages[i] = t.getImage("bong"+ (i+1) + "anigif.gif");
// layout the window
setLayout(new BorderLayout());
imgpanel = new Panel();
qb = new Button(" Quit "); // Quit button
ab = new Button("Set Alarm"); // Set/Stop alarm button
tstr = new TextField(""); // time display
tstr.setEditable(false); // is read-only
astr = new TextField(""); // and so is alarm display
astr.setEditable(false);
Panel q = new Panel();
q.setLayout(new BorderLayout());
q.add("North",tstr);
q.add("South",astr);
q.add("West",qb);
q.add("East",ab);
add("Center",imgpanel);
add("South",q);
// next panel reserves space for the animation images
imgpanel.resize(100,40);
//imgpanel.hide(); // hide it, so images will show
pack();
reshape(100,100,180,160); // makes ourselves a reasonable size
show(); // make ourselves visible
stopAlarm(); // ensure alarm is off
clockThread = new Thread(this); // create background thread
clockThread.start(); // and start it running
}
// this method turns a SMALL integer into a 2 character string
// e.g. 12 -> "12" and 9 -> "09"
String twoDigits(int i) {
String s;
s = "" + (100 + i); // ensure it's got 3 digits
return (s.substring(1));
}
public void setAlarm(int h, int m) {
// set the alarm display to show the alarm time
astr.setText(" Alarm set for " + h + ":" + twoDigits(m) + " ");
alarmh = 0; alarmm =0; // remember the alarm time
tick = 0; // just to play safe
bong = false; // we're not ringing
}
public void soundAlarm(String t) {
// set the alarm display to show we're ringing
new AePlayWave("C:\\Users\\Turin\\Documents\\NetBeansProjects\\ClockAlarm\\train.wav").start();
astr.setText(" " + t + " alarm ringing!");
ab.setLabel("Stop Alarm");// label the buttton "Stop"
bong = true; // we are ringing
}
public void stopAlarm() {
// set the alarm display to show there isn't one set
astr.setText(" Alarm not set");
ab.setLabel("Set Alarm"); // label the button "Set"
alarmh = -1; // indicate there's no alarm
tick = 0; // restart animations
bong = false; // we're not ringing
}
// Handle events that have occurred
public boolean handleEvent(Event evt)
{
switch(evt.id) {
case Event.WINDOW_DESTROY: // time to go
System.exit(0);
return true;
default:
// all other events get default handling
return super.handleEvent(evt);
}
}
public boolean action(Event e, Object o) {
int nh, nm;
if (qb == e.target) { // Quit button clicked
clockThread.stop(); // kill the background thread
clockThread = null; // and forget it
System.exit(0); // then go
} else if (ab == e.target) { // Either Stop or Set
if (bong) { // if it's Stop
stopAlarm(); // we stop it
} else { // else we ask for a new time
Date d = new Date();
nh = d.getHours();
nm = d.getMinutes() + 1;
SetAlarmDialog ab = new SetAlarmDialog(this," " + nh + ":" +
twoDigits(nm) + " ");
ab.show();
}
}
return true;
}
// this is the background animation thread, responsible for:
// - displaying the time each second
// - showing tick-tock animations when alarm is off
// - showing alarm animations when alarm is on
public void run() {
int nh, nm;
// loop until the application dies
while (clockThread != null) {
// find out what time it is
Date d = new Date();
nh = d.getHours();
nm = d.getMinutes();
if (bong == false && alarmh == nh && alarmm == nm) {
// it's time to ring the alarm
soundAlarm("" + alarmh + ":" + twoDigits(alarmm));
}
// do whatever animation is needed
repaint();
// then wait a bit (depending on what state we're in
try {
clockThread.sleep(bong ? ALARM_TICK : CLOCK_TICK);
} catch (InterruptedException e){
}
}
}
public void update(Graphics g) {
paint(g); // don't clear the background
}
public void paint(Graphics g) {
Date now = new Date();
// display the time
tstr.setText(now.getHours() + ":" + twoDigits(now.getMinutes()) + ":" +
twoDigits(now.getSeconds()));
if (bong) {
// we're ringing, so show the next alarm frame
g.drawImage(bongimages[tick], 60, 20, this);
// and increment to the next
tick = ((++tick)%ALARM_IMAGES);
} else {
// we're not ringing, so show the next tick-tock frame
g.drawImage(tickimages[tick], 60, 20, this);
// and increment to the next
tick = ((++tick)%TICK_IMAGES);
}
// is this necessary?
super.paint(g);
}
}
// this class handles getting a new Alarm time
class SetAlarmDialog extends Dialog
{
TextField at;
ClockAlarm parent;
public SetAlarmDialog(ClockAlarm par, String deftime)
{
// make a dialog
super(par, "Set Alarm Clock", true);
// fill it up with all the necessary widgets
TextField t;
parent = par;
t = new TextField("Enter new alarm time:");
t.setEditable(false);
add("West",t);
at = new TextField(deftime);
add("East",at);
Panel p = new Panel();
p.add("West", new Button("Set Alarm"));
p.add("East", new Button("Cancel"));
add("South",p);
pack(); // make it just fit
resize(preferredSize());
move(200,200);
}
public boolean handleEvent(Event evt)
{
String s;
int h, m;
switch(evt.id) {
case Event.WINDOW_DESTROY: // time to go
dispose();
return true;
case Event.ACTION_EVENT:
if ("Set Alarm".equals(evt.arg)) {
// OK button was pressed
// try to parse the time as "hh:mm" or "hh/mm" or "hh mm"
StringTokenizer st = new StringTokenizer(at.getText(),":/ ");
try {
// get the hour
s = st.nextToken();
// convert to an integer
h = Integer.parseInt(s);
// get the minute
s = st.nextToken();
// convert to an integer
m = Integer.parseInt(s);
} catch (Exception e) {
// ignore errors
h = -1; m = -1;
}
// if valid time, reset the alarm
if (h != -1)
parent.setAlarm(h,m);
else
parent.stopAlarm();
} else if ("Cancel".equals(evt.arg)) {
// Cancel button was pressed - do nothing
}
dispose();
return true;
default:
return false;
}
}
Klasa odpowiadająca za odtwarzanie dźwięku.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clockalarm;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
*
* @author Turin
*/
public class AePlayWave extends Thread {
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
enum Position {
LEFT, RIGHT, NORMAL
};
public AePlayWave(String wavfile) {
filename = wavfile;
curPosition = Position.NORMAL;
}
public AePlayWave(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println("Wave file not found: " + filename);
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
Czy jest ktoś w stanie napisać mi co jest nie tak i jak to poprawić?