import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.Timer.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class TimerFun extends Thread implements ActionListener
{
//Data Types (Attibrutes)
private JPanel jp1, jp2, jp3, jp4, jp5,
jp6, jp7, jp8, rainbowColor,
pbjp;
private JLabel jlDate;
private JFrame gui = new JFrame("Kevin T. Whetstone's FUN with Timers & Threads");
private JProgressBar jpb = new JProgressBar();
//GUI maker to make coding easier
public void GUIMaker()
{
//JFrame objects
gui.setLayout(new BorderLayout());
gui.setSize(350,350);
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
//JMenuBar objects
JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiExit = new JMenuItem("Exit");
JMenuItem jmiAbout = new JMenuItem("About");
//adding JMenuBar objects to the JFrame, gui
jmFile.add(jmiExit);
jmHelp.add(jmiAbout);
jmb.add(jmFile);
jmb.add(jmHelp);
gui.setJMenuBar(jmb);
//Mnemonic objects
jmFile.setMnemonic(KeyEvent.VK_F);
jmHelp.setMnemonic(KeyEvent.VK_H);
jmiExit.setMnemonic(KeyEvent.VK_X);
jmiAbout.setMnemonic(KeyEvent.VK_A);
//Adding ActionListener
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);
//Date & Time
jlDate = new JLabel();
Time t1 = new Time(jlDate);
java.util.Timer t2 = new java.util.Timer();
t2.scheduleAtFixedRate(new Time(jlDate), 10, 10);
gui.add(jlDate, BorderLayout.NORTH);
//Adding Progress Bars
pbjp = new JPanel();
jpb = new JProgressBar();
pbjp.add(new JLabel("Progress Bar: "));
progressBar pbInner = new progressBar();
pbjp.add(jpb);
jpb.setOrientation(JProgressBar.HORIZONTAL);
jpb.setMinimum(0);
jpb.setMaximum(100);
jpb.setValue(0);
jpb.setStringPainted( true );
jpb.setIndeterminate(false);
gui.add(pbjp, BorderLayout.SOUTH);
jpb.setString("Three seconds before starting" );
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
pbInner.start();
//Rainbow Color & Timer Color
rainbowColor = new JPanel(new GridLayout(7,0));
jp1.setBackground(Color.red);
rainbowColor.add(jp1);
jp2.setBackground(Color.orange);
rainbowColor.add(jp2);
jp3.setBackground(Color.yellow);
rainbowColor.add(jp3);
jp4.setBackground(Color.green);
rainbowColor.add(jp4);
jp5.setBackground(Color.blue);
rainbowColor.add(jp5);
jp6.setBackground(new Color(70,0,130));
rainbowColor.add(jp6);
jp7.setBackground(new Color(238,130,238));
rainbowColor.add(jp7);
gui.add(rainbowColor, BorderLayout.CENTER);
java.util.Timer c = new java.util.Timer();
c.scheduleAtFixedRate(new ColorBar(jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8), 2000, 500);
}
//Progress Bar with Thread
protected class progressBar extends Thread
{
public void run()
{
jpb.setString(null);
jpb.setStringPainted(true);
jpb.setIndeterminate(false);
int count = 0;
for(count =0; count<=100; count++)
{
jpb.setValue(count);
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
jpb.setStringPainted(true);
jpb.setString("Cylons Scanning");
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
jpb.setIndeterminate(true);
}
}
//ColorBars for background color with timer
class ColorBar extends TimerTask
{
JPanel jp1, jp2, jp3, jp4,
jp5, jp6, jp7, jp8;
public ColorBar(JPanel jp1, JPanel jp2, JPanel jp3, JPanel jp4, JPanel jp5, JPanel jp6, JPanel jp7, JPanel jp8)
{
this.jp1 = jp1;
this.jp2 = jp2;
this.jp3 = jp3;
this.jp4 = jp4;
this.jp5 = jp5;
this.jp6 = jp6;
this.jp7 = jp7;
this.jp8 = jp8;
}
public void run()
{
jp8.setBackground(jp1.getBackground());
jp1.setBackground(jp2.getBackground());
jp2.setBackground(jp3.getBackground());
jp3.setBackground(jp4.getBackground());
jp4.setBackground(jp5.getBackground());
jp5.setBackground(jp6.getBackground());
jp6.setBackground(jp7.getBackground());
jp7.setBackground(jp8.getBackground());
}
}
//Time with Date, time (hour, min, second)
class Time extends TimerTask
{
JLabel jlDate;
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
public Time(JLabel jlDate)
{
this.jlDate = jlDate;
}
public void run()
{
Calendar curCal = Calendar.getInstance();
Date curTime = curCal.getTime();
jlDate.setText(sdf.format(curTime));
jlDate.setFont(new Font("Arial", Font.BOLD, 20));
jlDate.setForeground(Color.red);
jlDate.setHorizontalAlignment(0);
}
}
public void actionPerformed(ActionEvent e)
{
Object select = e.getActionCommand();
if(select == "Exit")
{
try
{
Thread.sleep(2000);
System.exit(0);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
else if(select == "About")
{
String stringFormat = String.format("Fun with Timers and Threads\nby: Kevin T. Whetstone\nFebruary 19, 2014");
JOptionPane.showMessageDialog(null, stringFormat);
}
}
public TimerFun()
{
GUIMaker();
}
public static void main(String[] args)
{
new TimerFun();
}
}