Hey, so we have an assignment in Java. We are asked to make a GUI program that imitates the functions and interface of a microwave oven. I got some parts of the program working now. But I encountered a problem in the code of the JButton action listener.
This error is about repaint in the loop that's in JButton code for the action listener. The function of the JButton which is named "Start" is supposed to start the countdown timer of the microwave oven. I got the countdown working really nice. But the problem is when It comes to repainting the remaining time which is a Graphics(a drawString). It seems that the repaint() gets invoked in the loop but since the loop for the JButton hasn't finished yet all the repaint() calls get invoked but are not invoked immediatley. It waits for the loop to terminate then invokes all the repaint() call all at the same time, and obviously, the only repaint() that wil be seen will be the last. I'm looking for a way to have it immediately repainted everytime it loops without having to wait for the loop to terminate. And am also using Thread.sleep(1000) so that it decrements every second.
I'm really sorry I still don't know how to post a code in a proper way.
Here is the code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MicrowaveOven extends JFrame implements ActionListener
{
private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bStart, bStopReset, bOpenClose;
private static JLabel time;
private static int x = 0, y = 0, z = 0, test = 0;
public static void main(String[] args)
{
MicrowaveOven groupTwo = new MicrowaveOven();
groupTwo.setTitle("Computer Programming");
groupTwo.setSize(800, 500);
groupTwo.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
groupTwo.setLayout(null);
groupTwo.setVisible(true);
groupTwo.add(b7);
groupTwo.add(b8);
groupTwo.add(b9);
groupTwo.add(b4);
groupTwo.add(b5);
groupTwo.add(b6);
groupTwo.add(b1);
groupTwo.add(b2);
groupTwo.add(b3);
groupTwo.add(b0);
groupTwo.add(bStopReset);
groupTwo.add(bStart);
groupTwo.add(bOpenClose);
//groupTwo.add(time);
}
public MicrowaveOven()
{
b7 = new JButton("7"); b7.setSize(45, 30); b7.setLocation(584, 110);
b8 = new JButton("8"); b8.setSize(45, 30); b8.setLocation(637, 110);
b9 = new JButton("9"); b9.setSize(45, 30); b9.setLocation(690, 110);
b4 = new JButton("4"); b4.setSize(45, 30); b4.setLocation(584, 150);
b5 = new JButton("5"); b5.setSize(45, 30); b5.setLocation(637, 150);
b6 = new JButton("6"); b6.setSize(45, 30); b6.setLocation(690, 150);
b1 = new JButton("1"); b1.setSize(45, 30); b1.setLocation(584, 190);
b2 = new JButton("2"); b2.setSize(45, 30); b2.setLocation(637, 190);
b3 = new JButton("3"); b3.setSize(45, 30); b3.setLocation(690, 190);
b0 = new JButton("0"); b0.setSize(45, 30); b0.setLocation(584, 230);
bStopReset = new JButton("Stop/Reset"); bStopReset.setSize(98, 30); bStopReset.setLocation(637, 230);
bStart = new JButton("Start"); bStart.setSize(150, 30); bStart.setLocation(584, 270);
bOpenClose = new JButton("Open/Close"); bOpenClose.setSize(150, 80); bOpenClose.setLocation(584, 320);
//time = new JLabel(" " + x + ":" + y + z); time.setFont(new Font("TimesRoman", Font.BOLD, 40)); time.setLocation(615, 55); time.setSize(100, 30);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b0.addActionListener(this);
bStopReset.addActionListener(this);
bStart.addActionListener(this);
bOpenClose.addActionListener(this);
}
public static String timeInput(String number)
{
if (z == 0)
{
if (y != 0)
{
x = y;
y = z;
z = Integer.parseInt(number);
}
else if (x != 0)
{
x = x;
y = y;
z = z;
}
else
z = Integer.parseInt(number);
}
else if (y == 0)
{
if (x != 0)
{
x = x;
y = y;
z = z;
}
else
{
y = z;
z = Integer.parseInt(number);
}
}
else if (x == 0)
{
x = y;
y = z;
z = Integer.parseInt(number);
}
return number;
}
public void actionPerformed(ActionEvent click)
{
boolean countdown = true;
switch (click.getActionCommand())
{
case "0":
{
timeInput("0");
repaint();
break;
}
case "1":
{
timeInput("1");
repaint();
break;
}
case "2":
{
timeInput("2");
repaint();
break;
}
case "3":
{
timeInput("3");
repaint();
break;
}
case "4":
{
timeInput("4");
repaint();
break;
}
case "5":
{
timeInput("5");
repaint();
break;
}
case "6":
{
timeInput("6");
repaint();
break;
}
case "7":
{
timeInput("7");
repaint();
break;
}
case "8":
{
timeInput("8");
repaint();
break;
}
case "9":
{
timeInput("9");
repaint();
break;
}
case "Stop/Reset":
{
x = 0;
y = 0;
z = 0;
repaint();
break;
}
case "Start":
{
while (countdown)
{
if (z != 0)
z--;
else if (z == 0)
{
if (y != 0)
{
y--;
z = 9;
}
}
if (y == 0)
{
if ((x != 0) && (z == 0) && (test == 0))
{
y = 0;
z = 0;
test = 1;
}
else if ((x != 0) && (z == 0) && (test == 1))
{
x--;
y = 5;
z = 9;
}
}
if ((x == 0) && (y == 0) && (z == 0))
countdown = false;
System.out.println(x+ ":" + y + z);
repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException iE){}
}
break;
}
default:
}
}
public void paint(Graphics mWave)
{
super.paint(mWave);
mWave.drawRect(20, 40, 760, 440);
mWave.drawRect(50, 70, 500, 370);
mWave.drawRect(565, 80, 200, 40);
Font timerFont = new Font("TimesRoman", Font.BOLD, 40);
mWave.setFont(timerFont);
mWave.drawString(" " + x + ":" + y + z, 625, 115);
}
}