import java.awt.*;
import java.awt.event.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class GUITimer
{
static boolean isRecording = true;
static int timeSpoken = 0;
static final boolean moreMagic = true;
static int[] records = new int[5];
public static void main(String[] args)
{
CenteredFrame frame = new CenteredFrame();
//TextIO.readFile("records.txt");
recordUpdate( timeSpoken );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (moreMagic){
frame.show();
}
}
public static void recordUpdate(int spoken){
int[] tempArray = new int[5];
TextIO.readFile("records.txt");
for(int i = 0; i < 5; i++){
tempArray[i] = TextIO.getlnInt();
}// end for
for(int i = 0; i < 5; i++){
if (spoken >= tempArray[i]){ //if new number is greater than a number
for(int j = 0; j < (tempArray.length - i - 1); j++){
tempArray[j] = tempArray[j + 1];
}//end for
tempArray[i] = spoken;
break;
}//end if
/*
if (i == tempArray[i-1]){
break;}
*/
}//end for
GUITimer.records = tempArray.clone();
for(int i = 0; i < 5; i++){
TextIO.putln(GUITimer.records[i]);
}//end for
}
}
class CenteredFrame extends JFrame
{
public CenteredFrame()
{
setTitle("Timer of sorts");
// get screen dimensions
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
// center frame in screen
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
PanelStuff panel = new PanelStuff();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}
class PanelStuff extends JPanel
{
static int seconds = 0;
static int minutes = 0;
static int hours = 0;
static int timeTemp = 0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
String secondsString = Integer.toString(seconds);
String minutesString = Integer.toString(minutes);
String hoursString = Integer.toString((hours));
if (seconds < 10)
{
secondsString = "0" + secondsString;
}
if (minutes < 10)
{
minutesString = "0" + minutesString;
}
if (hours < 10)
{
hoursString = "0" + hoursString;
}
String timeOutput = (hoursString + ":" + minutesString + ":" + secondsString);
// The string properties
g.setColor(new Color(250, 80, 0));
String message = timeOutput;
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
// measure the size of the message
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
// set (x,y) = top left corner of text
double x = (getWidth() - bounds.getWidth()) /2;
double y = (getHeight() - bounds.getHeight())/2;
//add ascent to y to reach the baseline
double ascent = -bounds.getY();
double baseY = y + ascent;
g.drawString(message, (int)x, (int)baseY);
// temp record test
f = new Font("Serif", Font.BOLD, 15);
g2.setFont(f);
g.drawString(Integer.toString(GUITimer.records[0]) +
Integer.toString(GUITimer.records[1]) +
Integer.toString(GUITimer.records[2]) +
Integer.toString(GUITimer.records[3]) +
Integer.toString(GUITimer.records[4]), 50, 50);
repaint();
}
public static void Convert()
{
timeTemp = GUITimer.timeSpoken;
minutes = timeTemp / 60;
seconds = timeTemp % 60;
{
hours = minutes / 60;
minutes = minutes % 60;
}
}
class CounterAction implements ActionListener{
public void actionPerformed(ActionEvent evt){
if (GUITimer.isRecording == true){
GUITimer.timeSpoken = GUITimer.timeSpoken + 1;
Convert();
}
else
return;
}
}
PanelStuff(){
//DAN was here
ActionListener listener = new CounterAction();
Timer timer = new Timer(1000, listener);
timer.start();
JButton startBtn = new JButton("Start/Stop");
JButton resetBtn = new JButton("Reset");
add(startBtn);
add(resetBtn);
ButtonActionStart startAction = new ButtonActionStart();
ButtonActionReset resetAction = new ButtonActionReset();
startBtn.addActionListener(startAction);
resetBtn.addActionListener(resetAction);
}
// button stuff
private class ButtonActionStart implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if (GUITimer.isRecording == false)
GUITimer.isRecording = true;
else
GUITimer.isRecording = false;
}
}
private class ButtonActionReset implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
TextIO.readFile("records.txt");
GUITimer.recordUpdate(GUITimer.timeSpoken);
GUITimer.timeSpoken = 0;
}
}
}