Sunday, 7 April 2013

Fan Which can be started,stoped and reversed

FanControl.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FanControl extends JPanel{
    private JButton btnStart = new JButton("Start");
    private JButton btnStop = new JButton("Stop");
    private JButton btnReverse = new JButton("Reverse");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    public Fan fan = new Fan();
    private JScrollBar jscbHort = new JScrollBar(JScrollBar.HORIZONTAL);
    public FanControl(){
        setLayout(new BorderLayout());
        p1.setLayout(new GridLayout(1,3,0,0));
        p1.add(btnStart);
        p1.add(btnStop);
        p1.add(btnReverse);
        p2.setLayout(new GridLayout(1,1,0,0));
        p2.add(jscbHort);
        add(p1,BorderLayout.NORTH);
        add(fan,BorderLayout.CENTER);
        add(p2,BorderLayout.SOUTH);
        btnStart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.start();
            }
        });
        btnStop.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.stop();
            }
        });
        btnReverse.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.reverse();
            }
        });
        jscbHort.addAdjustmentListener(new AdjustmentListener(){
            public void adjustmentValueChanged(AdjustmentEvent ae){
                double value = jscbHort.getValue();
                double maximumValue = jscbHort.getMaximum();
                int delay = p2.getWidth() - (int)(value * p2.getWidth()/maximumValue);
                delay = delay/10;
                System.out.println(delay);
                fan.setSpeed(delay);
            }
        });
    }
}


FanControlApplet.java

import java.awt.*;
import javax.swing.*;
public class FanControlApplet extends JApplet{
    FanControl fanControl = new FanControl();
    public void init(){
        add(fanControl);
    }
    public static void main(String []args){
        JFrame frame = new JFrame("Fan Control");
        FanControlApplet applet = new FanControlApplet();
        applet.init();
        frame.add(applet);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

An Alarm Clock

AlarmClock.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.net.URL;
import java.applet.*;
public class AlarmClock extends JPanel{
    private JLabel jlblHour = new JLabel("Hour");
    private JLabel jlblMinute = new JLabel("Minute");
    private JLabel jlblSecond = new JLabel("Second");
    private JTextField jtfHour = new JTextField();
    private JTextField jtfMinute = new JTextField();
    private JTextField jtfSecond = new JTextField();
    private JPanel p1 = new JPanel();
    private JCheckBox jchk = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    AudioClip audioClip;
    private JPanel p2 = new JPanel();
    private int hour;
    private int minute;
    private int second;
    SetAlarm frame = new SetAlarm();
    int i = 0;
    private static Calendar gCalendar = new GregorianCalendar();
    public static int alarmHour = 0;
    public static int alarmMinute = 0;
    public static int alarmSecond = 0;
    Timer timer2;
    public AlarmClock(){
        setLayout(new BorderLayout());
        Timer timer1 = new Timer(1000,new TimerListener1());
        timer1.start();
        p1.setLayout(new GridLayout(3,2,5,5));
        p1.add(jlblHour);
        p1.add(jtfHour);
        p1.add(jlblMinute);
        p1.add(jtfMinute);
        p1.add(jlblSecond);
        p1.add(jtfSecond);
        gCalendar = new GregorianCalendar();
        hour = gCalendar.get(Calendar.HOUR);
        minute = gCalendar.get(Calendar.MINUTE);
        second = gCalendar.get(Calendar.SECOND);
        jtfHour.setText("" + hour);
        jtfMinute.setText("" + minute);
        jtfSecond.setText("" + second);
        p1.setBorder(new LineBorder(Color.BLACK,1));
        p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
        p2.add(jchk);
        p2.add(btnSetAlarm);
        jchk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                if(jchk.isSelected())
                {
                    timer2 = new Timer(0,new TimerListener2());
                    timer2.start();
                }
            }
        });
        btnSetAlarm.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
        add(p1,BorderLayout.CENTER);
        add(p2,BorderLayout.SOUTH);
    }
    public class TimerListener2 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            if((hour == alarmHour)&&(minute == alarmMinute)&&(second == alarmSecond))
            {
                URL urlForAudio = getClass().getResource("image/Bbpledge.wav");
                audioClip = Applet.newAudioClip(urlForAudio);
                audioClip.loop();
            }
        }
    }
    public void setTime(){
        setCurrentTime();
        jtfHour.setText("" + hour);
        jtfMinute.setText("" + minute);
        jtfSecond.setText("" + second);
    }
    class TimerListener1 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            setTime();
        }
    }
    public void setCurrentTime(){
        gCalendar = new GregorianCalendar();
        hour = gCalendar.get(Calendar.HOUR);
        minute = gCalendar.get(Calendar.MINUTE);
        second = gCalendar.get(Calendar.SECOND);
    }
    public static void setAlarmTime(int hour,int minute,int second){
        alarmHour = hour;
        alarmMinute = minute;
        alarmSecond = second;
    }
}


AlarmClockFrame.java

import javax.swing.*;
public class AlarmClockFrame extends JFrame{
    public AlarmClockFrame(){
        add(new AlarmClock());
    }
    public static void main(String []args){
        AlarmClockFrame frame = new AlarmClockFrame();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}