Learning Java
Custom Search

Previous Next

Part D - Wake Up - Play Radio Station

In this step, we will add a method of accessing a radio station to provide sound to aid in gradually waking up. Currently in our list of radio stations, cboxPickRadio, we have used the place holders

    radio station 1
    radio station 2
    radio station 3

We will replace this list with valid internet radio stations.

    http://www.famefm.fm/
    http://wmlive.vitalstreamcdn.com/live_eonwmss_vitalstream_com_radioone-wpze-fm-fast
    http://player.cbsradio.com/player/CBSRadio_Player.html?id=90&onestat=wins
    http://kbaq.org/livestream/connect.asx
    http://www.energymix.com/
    http://www.radiojamaica.com/content/view/974/95/
    http://gateway.andohs.net/player/?sid=3012&nid=2920

The user will be able to select a radio station from this list and it will be played when the Play button is pushed. We will set it up with a default radio station to start. Also, it will be played 10 minutes after the alarm sounds. To select from the list, we will have to implement the ItemListener interface. We will override the method itemStateChanged which is activated whenever a item is selected from our list.

public class WakeUp extends JApplet implements Runnable, ActionListener,
 ItemListener {

    String currRadio = "http://www.famefm.fm/";

    cboxPickRadio.addItemListener(this);

    public void itemStateChanged(ItemEvent evt) {
        Object source = evt.getSource();
        if (source == cboxPickRadio) {
            Object newPick = evt.getItem();
            currRadio = newPick.toString();
        }
    }

We will now activate the code for the Play button. We will have to use the class URL to access the internet page for the radio station. We use the method showDocument to display the URL. This method is part of the interface AppletContext. We want to show it in a different page than the one with our WakeUp applet, so we use the option "_blank".

import java.net.*;

        btnPlayRadio.addActionListener(this);

    public void actionPerformed(ActionEvent evt) {
	if (evt.getSource().equals(btnPlayRadio))  {
		txtStatusInfo.setText("Playing your radio station.");
	    try {
		URL load1 = new URL(currRadio);
		getAppletContext().showDocument(load1, "_blank");
	    } catch (MalformedURLException e) {
		showStatus("Bad URL:" + currRadio);
		txtStatusInfo.setText("Bad URL:" + currRadio);
	    }
	}

If the user doesn't like our list of radio stations they can add their own station since we made cboxPickRadio editable. We have provided a radio station locator website to find other interesting stations. We want to activate the Find button to go to this particular website.

        btnSrchRadio.addActionListener(this);

    public void actionPerformed(ActionEvent evt) {

	if (evt.getSource().equals(btnSrchRadio))  {
		txtStatusInfo.setText("Go to radio locator.");
	    try {
		URL load2 = new URL(txtSrchRadio.getText());
		getAppletContext().showDocument(load2, "_blank");
	    } catch (MalformedURLException e) {
		showStatus("Bad URL:" + txtSrchRadio.getText());
		txtStatusInfo.setText("Bad URL:" + txtSrchRadio.getText());
	    }
	}

Now we want to do the code to cause the radio to play 10 minutes after the alarm has gone off. To speed testing, you might want to add only 1 minute, ckDatePlus10.add(Calendar.MINUTE, 1).

    Calendar ckDatePlus10;
    boolean doCheckRadio = false;

	ckDatePlus10 = new GregorianCalendar();

	ckDatePlus10.set(yy, mm, dd, ckHour, ckMin, 00);
	ckDatePlus10.add(Calendar.MINUTE, 10);
	if (ckDatePlus10.compareTo(cal) < 0) 
	    ckDatePlus10.add(Calendar.DATE, 1);

	    if (doCheckRadio)
		if (ckDatePlus10.compareTo(currtime) < 0) {
		    runTheRadio();
		}

    public void runTheSound() {
	doCheckRadio = true;

    public void runTheRadio() {
	doCheckRadio = false;
	txtStatusInfo.setText("Playing the Radio.");
	try {
	    URL load3 = new URL(currRadio);
	    getAppletContext().showDocument(load3, "_blank");
	} catch (MalformedURLException e) {
	    showStatus("Bad Radio URL:" + currRadio);
	    txtStatusInfo.setText("Bad Radio URL:" + currRadio);
	}
   }

Here is the final listing.

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.AudioClip;
import java.net.*;

public class WakeUp extends JApplet implements Runnable, ActionListener,
				 ItemListener {
    DteTimePanel dtTimePane  = new DteTimePanel();
    JLabel lblWakeUpTime     = new JLabel("Wake Up Time");
    JTextField txtWakeUpTime = new JTextField("20:33", 5);
    JTextField txtSound      = new JTextField("train.wav");
    JTextField txtSrchRadio  = 
		new JTextField("http://www.radio-locator.com/");
    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;
    Calendar ckDatePlus10;
    boolean doCheck = false;
    boolean doCheckRadio = false;
    String currRadio = "http://www.famefm.fm/";

    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);
        btnSrchRadio.addActionListener(this);
	panButtons.add(btnSrchRadio);
        btnPlayRadio.addActionListener(this);
	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.addItemListener(this);

        cboxPickRadio.addItem("http://www.famefm.fm/");
cboxPickRadio.addItem
("http://wmlive.vitalstreamcdn.com/live_eonwmss_vitalstream_com_radioone-wpze-fm-fast");
cboxPickRadio.addItem
("http://player.cbsradio.com/player/CBSRadio_Player.html?id=90&onestat=wins");
        cboxPickRadio.addItem("http://kbaq.org/livestream/connect.asx");
        cboxPickRadio.addItem("http://www.energymix.com/");
        cboxPickRadio.addItem("http://www.radiojamaica.com/content/view/974/95/");
       cboxPickRadio.addItem("http://gateway.andohs.net/player/?sid=3012&nid=2920");
        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();
	ckDatePlus10 = 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);
	ckDatePlus10.set(yy, mm, dd, ckHour, ckMin, 00);
	ckDatePlus10.add(Calendar.MINUTE, 10);
	if (ckDatePlus10.compareTo(cal) < 0) 
	    ckDatePlus10.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();
		}

	    if (doCheckRadio)
		if (ckDatePlus10.compareTo(currtime) < 0) {
		    runTheRadio();
		}
	}
    }

    public void runTheSound() {
	doCheck = false;
	doCheckRadio = true;
        if (alarmSound != null)
            alarmSound.loop();
    }

    public void runTheRadio() {
	doCheckRadio = false;
	txtStatusInfo.setText("Playing the Radio.");
	try {
	    URL load3 = new URL(currRadio);
	    getAppletContext().showDocument(load3, "_blank");
	} catch (MalformedURLException e) {
	    showStatus("Bad Radio URL:" + currRadio);
	    txtStatusInfo.setText("Bad Radio URL:" + currRadio);
	}
   }

    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();
	    }
	}

	if (evt.getSource().equals(btnPlayRadio))  {
		txtStatusInfo.setText("Playing your radio station.");
	    try {
		URL load1 = new URL(currRadio);
		getAppletContext().showDocument(load1, "_blank");
	    } catch (MalformedURLException e) {
		showStatus("Bad URL:" + currRadio);
		txtStatusInfo.setText("Bad URL:" + currRadio);
	    }
	}

	if (evt.getSource().equals(btnSrchRadio))  {
	    txtStatusInfo.setText("Go to radio locator.");
	    try {
		URL load2 = new URL(txtSrchRadio.getText());
		getAppletContext().showDocument(load2, "_blank");
	    } catch (MalformedURLException e) {
		showStatus("Bad URL:" + txtSrchRadio.getText());
		txtStatusInfo.setText("Bad URL:" + txtSrchRadio.getText());
	    }
	}
    }

    public void itemStateChanged(ItemEvent evt) {
        Object source = evt.getSource();
        if (source == cboxPickRadio) {
            Object newPick = evt.getItem();
            currRadio = newPick.toString();
        }
    }
}

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();
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved