Eredeath
February 20th, 2011, 06:24 AM
Please excuse me if my terminology is incorrect. I'm a beginner at Java and learning it on my own. I'm trying to create an interval timer where once the timer is started the window will turn yellow for five seconds, then green for a user set time and red for a user set time. Here is my class int_timer:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Eredeath
* @version .01b
*/
public class int_timer implements ActionListener
{
private static String[] INPUTS = new String[3];
private String TIMEON = "2";
private String TIMEOFF = "2";
private String ROUNDS = "2";
private JTextField TonIn, ToffIn, round;
public Container intScreen;
public void screen(Container ContentPane) {
JLabel TonPrompt, ToffPrompt, roundPrompt;
JButton startButton;
intScreen = ContentPane;
intScreen.removeAll();
intScreen.repaint();
JPanel okayPanel, InPanel;
intScreen.setLayout(new BorderLayout());
//Text Field inputs
InPanel = new JPanel(new GridLayout(0,1));
//input for Time on
TonPrompt = new JLabel();
TonPrompt.setText("Enter Time on in seconds: ");
TonPrompt.setSize(150,5);
InPanel.add(TonPrompt);
TonIn = new JTextField(TIMEON);
TonIn.setColumns(5);
InPanel.add(TonIn);
//TonIn.addActionListener(this);
//input for Time off
ToffPrompt = new JLabel();
ToffPrompt.setText("Enter Time off in seconds: ");
ToffPrompt.setSize(150,5);
InPanel.add(ToffPrompt);
ToffIn = new JTextField(TIMEOFF);
ToffIn.setColumns(5);
InPanel.add(ToffIn);
//TonIn.addActionListener(this);
//input for Rounds
roundPrompt = new JLabel();
roundPrompt.setText("Enter number of rounds: ");
roundPrompt.setSize(150,5);
InPanel.add(roundPrompt);
round = new JTextField(ROUNDS);
round.setColumns(1);
InPanel.add(round);
//TonIn.addActionListener(this);
intScreen.add(InPanel, BorderLayout.CENTER);
//create okay button
okayPanel = new JPanel(new FlowLayout());
startButton = new JButton("Start!");
startButton.setSize(30, 30);
okayPanel.add(startButton);
intScreen.add(okayPanel, BorderLayout.SOUTH);
intScreen.validate();
//action listener for start button
startButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
//string array
INPUTS[0] = TonIn.getText();
INPUTS[1] = ToffIn.getText();
INPUTS[2] = round.getText();
if (validateInputs(INPUTS)){
RunIntTimer(INPUTS);
}
}
public boolean validateInputs(String[] inputs){
for (int i = 0; i < inputs.length; i++) {
try{
Integer.parseInt(inputs[i]);
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "please enter digits only.");
return false;
}
}
return true;
}
public int[] inputsToInts(String[] inputs){
int[] intin = new int[inputs.length];
for (int i = 0; i < inputs.length; i++){
intin[i] = Integer.parseInt(inputs[i]);
}
return intin;
}
public void RunIntTimer(String[] inputs){
//get the times and rounds and convert them to integers
int[] timeRnds = new int[inputs.length];
timeRnds = inputsToInts(inputs);
int timeon = timeRnds[0];
int timeoff = timeRnds[1];
int rounds = timeRnds[2];
//clear the container
//intScreen = ContentPane;
intScreen.removeAll();
intScreen.repaint();
int rnds = rounds; //dummy variable for rounds
do {
if (rounds == rnds){
bgYellow();
fiveSecWait();
}
bgGreen();
Timer(timeon);
bgRed();
Timer(timeoff);
rounds--;
} while (rounds > 0);
}
private void fiveSecWait(){
long fiveSecWait = System.currentTimeMillis();
while (fiveSecWait > (System.currentTimeMillis() - 5000)){
}
}
private void Timer(int time){
long timenow = System.currentTimeMillis();
while (timenow > (System.currentTimeMillis() - (time * 1000))){
}
}
private void bgYellow(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.YELLOW);
}
private void bgGreen(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.GREEN);
}
private void bgRed(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.RED);
}
}
The portion I'm having the difficulty with is my RunIntTimer method. Instead of clearing the container and coloring the background yellow for 5 seconds then green then red it will look like it froze then after 13 seconds turn red.
How do I get it to change before the end of the method?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Eredeath
* @version .01b
*/
public class int_timer implements ActionListener
{
private static String[] INPUTS = new String[3];
private String TIMEON = "2";
private String TIMEOFF = "2";
private String ROUNDS = "2";
private JTextField TonIn, ToffIn, round;
public Container intScreen;
public void screen(Container ContentPane) {
JLabel TonPrompt, ToffPrompt, roundPrompt;
JButton startButton;
intScreen = ContentPane;
intScreen.removeAll();
intScreen.repaint();
JPanel okayPanel, InPanel;
intScreen.setLayout(new BorderLayout());
//Text Field inputs
InPanel = new JPanel(new GridLayout(0,1));
//input for Time on
TonPrompt = new JLabel();
TonPrompt.setText("Enter Time on in seconds: ");
TonPrompt.setSize(150,5);
InPanel.add(TonPrompt);
TonIn = new JTextField(TIMEON);
TonIn.setColumns(5);
InPanel.add(TonIn);
//TonIn.addActionListener(this);
//input for Time off
ToffPrompt = new JLabel();
ToffPrompt.setText("Enter Time off in seconds: ");
ToffPrompt.setSize(150,5);
InPanel.add(ToffPrompt);
ToffIn = new JTextField(TIMEOFF);
ToffIn.setColumns(5);
InPanel.add(ToffIn);
//TonIn.addActionListener(this);
//input for Rounds
roundPrompt = new JLabel();
roundPrompt.setText("Enter number of rounds: ");
roundPrompt.setSize(150,5);
InPanel.add(roundPrompt);
round = new JTextField(ROUNDS);
round.setColumns(1);
InPanel.add(round);
//TonIn.addActionListener(this);
intScreen.add(InPanel, BorderLayout.CENTER);
//create okay button
okayPanel = new JPanel(new FlowLayout());
startButton = new JButton("Start!");
startButton.setSize(30, 30);
okayPanel.add(startButton);
intScreen.add(okayPanel, BorderLayout.SOUTH);
intScreen.validate();
//action listener for start button
startButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
//string array
INPUTS[0] = TonIn.getText();
INPUTS[1] = ToffIn.getText();
INPUTS[2] = round.getText();
if (validateInputs(INPUTS)){
RunIntTimer(INPUTS);
}
}
public boolean validateInputs(String[] inputs){
for (int i = 0; i < inputs.length; i++) {
try{
Integer.parseInt(inputs[i]);
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "please enter digits only.");
return false;
}
}
return true;
}
public int[] inputsToInts(String[] inputs){
int[] intin = new int[inputs.length];
for (int i = 0; i < inputs.length; i++){
intin[i] = Integer.parseInt(inputs[i]);
}
return intin;
}
public void RunIntTimer(String[] inputs){
//get the times and rounds and convert them to integers
int[] timeRnds = new int[inputs.length];
timeRnds = inputsToInts(inputs);
int timeon = timeRnds[0];
int timeoff = timeRnds[1];
int rounds = timeRnds[2];
//clear the container
//intScreen = ContentPane;
intScreen.removeAll();
intScreen.repaint();
int rnds = rounds; //dummy variable for rounds
do {
if (rounds == rnds){
bgYellow();
fiveSecWait();
}
bgGreen();
Timer(timeon);
bgRed();
Timer(timeoff);
rounds--;
} while (rounds > 0);
}
private void fiveSecWait(){
long fiveSecWait = System.currentTimeMillis();
while (fiveSecWait > (System.currentTimeMillis() - 5000)){
}
}
private void Timer(int time){
long timenow = System.currentTimeMillis();
while (timenow > (System.currentTimeMillis() - (time * 1000))){
}
}
private void bgYellow(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.YELLOW);
}
private void bgGreen(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.GREEN);
}
private void bgRed(){
//Container contentPane = getContentPane( );
intScreen.setBackground(Color.RED);
}
}
The portion I'm having the difficulty with is my RunIntTimer method. Instead of clearing the container and coloring the background yellow for 5 seconds then green then red it will look like it froze then after 13 seconds turn red.
How do I get it to change before the end of the method?