Posts Subscribe to This BlogComments

Follow Us

code alarm with java IDE

newbie….coding membuat alarm dengan java IDE………….

ini adalah source code nya yang lengkap


import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.AudioClip; public class WakeUp extends JApplet implements Runnable, ActionListener {
DteTimePanel dtTimePane = new DteTimePanel();
JLabel lblWakeUpTime = new JLabel(“Wake Up Time”);
JTextField txtWakeUpTime = new JTextField(“10:38″, 5);
JTextField txtSound = new JTextField(“Jurik.wav”);
JTextField txtSrchRadio = new JTextField(“Jurik.wav”);
JComboBox cboxPickRadio = new JComboBox();
JButton btnStartStop = new JButton(“Start”);
JButton btnSrchRadio = new JButton(“Find”);
JButton btnPlayRadio = new JButton(“Play”);
JTextField txtStatusInfo = new JTextField(“Status information”);
AudioClip alarmSound;
Thread thrSound;
int ckHour;
int ckMin;
Calendar ckDate;
boolean doCheck = false;
public void init() {
alarmSound = getAudioClip(getCodeBase(), txtSound.getText());
JPanel panOuter = new JPanel();
GridLayout gridlay1 = new GridLayout(3, 1);
panOuter.setLayout(gridlay1);
panOuter.add(dtTimePane);
JPanel panWakeUpTime = new JPanel();
FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT);
panWakeUpTime.setLayout(flow1);
panWakeUpTime.add(lblWakeUpTime);
panWakeUpTime.add(txtWakeUpTime);
JPanel panButtons = new JPanel();
GridLayout gridlay2 = new GridLayout(1, 3);
panButtons.setLayout(gridlay2);
panButtons.add(btnSrchRadio);
panButtons.add(btnPlayRadio);
btnStartStop.addActionListener(this);
panButtons.add(btnStartStop);
JPanel panSetAlarmTime = new JPanel();
GridLayout gridlay3 = new GridLayout(2, 2);
panSetAlarmTime.setLayout(gridlay3);
panSetAlarmTime.add(panWakeUpTime);
panSetAlarmTime.add(panButtons);
panSetAlarmTime.add(txtSound);
panSetAlarmTime.add(txtSrchRadio);
panOuter.add(panSetAlarmTime);
cboxPickRadio.addItem(“radio station 1″);
cboxPickRadio.addItem(“radio station 2″);
cboxPickRadio.addItem(“radio station 3″);
cboxPickRadio.setEditable(true);
JPanel panBottom = new JPanel();
GridLayout gridlay4 = new GridLayout(2, 1);
panBottom.setLayout(gridlay4);
panBottom.add(cboxPickRadio);
panBottom.add(txtStatusInfo);
panOuter.add(panBottom);
setContentPane(panOuter);
repaint();
}
public void start() {
String s1 = txtWakeUpTime.getText();
ckHour = Integer.parseInt(s1.substring(0, 2));
ckMin = Integer.parseInt(s1.substring(3, 5));
ckDate = new GregorianCalendar();
Calendar cal = new GregorianCalendar();
int mm = cal.get(Calendar.MONTH);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int yy = cal.get(Calendar.YEAR);
ckDate.set(yy, mm, dd, ckHour, ckMin, 00);
if (ckDate.compareTo(cal) < 0) ckDate.add(Calendar.DATE, 1);
if (thrSound == null) {
thrSound = new Thread(this);
thrSound.start();
}
}
public void stop() {
doCheck = false;
if (thrSound != null) {
if (alarmSound != null)
alarmSound.stop();
thrSound = null;
}
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
GregorianCalendar currtime = new GregorianCalendar();
if (doCheck)
if (ckDate.compareTo(currtime) < 0) {
runTheSound();
}
}
}
public void runTheSound() {
doCheck = false;
if (alarmSound != null)
alarmSound.loop();
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStartStop)) {
if (btnStartStop.getText().equals("Start")) {
btnStartStop.setText("Stop");
txtStatusInfo.setText("Alarm process started");
doCheck = true;
start();
} else if (btnStartStop.getText().equals("Stop")) {
btnStartStop.setText("Start");
txtStatusInfo.setText("Alarm process has been stopped.");
stop();
}
}
}
}
class DteTimePanel extends JPanel {
String lastTime = "";
public void paint(Graphics screen) {
GregorianCalendar xday = new GregorianCalendar();
String xtime = xday.getTime().toString();
Color fgColor = new Color(40, 120, 160);
Font type = new Font("Monospaced", Font.BOLD, 20);
screen.setFont(type);
screen.setColor(getBackground());
screen.drawString(lastTime, 5, 15);
screen.setColor(fgColor);
screen.drawString(xtime, 5, 15);
lastTime = xtime;
repaint();
}
}



0 komentar:

Posting Komentar