JAVA. Need someone to help me to debug my code? Thank you. Write code fragments
ID: 3855788 • Letter: J
Question
JAVA. Need someone to help me to debug my code? Thank you.
Write code fragments to create a timer object that is fired every 10 seconds. Every 10 seconds, the background color of the window changes to a new random color.
Write the timer-related code that would go in the constructor and the complete listener class.
For full credit, use a timer object of type javax.swing.Timer and a listener class that implements ActionListener.
my code:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class CountDownGUI extends JFrame {
private Container myCP;
private JTextField counterDisplay;
private Timer timer;
private int counter;
private int initialCount = 10;
private int speed;
public CountDownGUI() {
super("counting Down");
myCP = getContentPane();
JPanel mainPanel = new JPanel();
myCP.add(mainPanel);
setSize(200,200);
Random generator = new Random();
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
Color randomColor = new Color(r,g,b);
mainPanel.setBackground(randomColor);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
counterDisplay = new JTextField(20);
counterDisplay.setEditable(false);
myCP.add(counterDisplay);
counter = initialCount;
counterDisplay.setText(Integer.toBinaryString(counter));
speed = 1000;
timer = new Timer(speed, new TimerListener());
timer.restart();
}
private class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if (counter >0){
counter --;
}else {
counter = initialCount;
CountDownGUI.setBackground(randomColor);
}
counterDisplay.setText(Integer.toString(counter));
}}
public static void main(String[] args){
CountDownGUI frame = new CountDownGUI();
}
}
Explanation / Answer
Hi ,
Please find below the working code: -
Color of output is changing after every 10 seconds. Let me know if you find more problem in this in comments. I'll love to help more...
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class CountDownGUI extends JFrame {
private Container myCP;
private JTextField counterDisplay;
private Timer timer;
private int counter;
private int initialCount = 10;
private int speed;
JPanel mainPanel;
public CountDownGUI() {
super("counting Down");
myCP = getContentPane();
mainPanel = new JPanel();
myCP.add(mainPanel);
setSize(200, 200);
mainPanel.setBackground(getColor());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
counterDisplay = new JTextField(20);
counterDisplay.setEditable(false);
myCP.add(counterDisplay);
counter = initialCount;
counterDisplay.setText(Integer.toBinaryString(counter));
speed = 1000;
timer = new Timer(speed, new TimerListener());
timer.restart();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (counter > 0) {
counter--;
} else {
counter = initialCount;
mainPanel.setBackground(getColor());
}
counterDisplay.setText(Integer.toString(counter));
}
}
public Color getColor() {
Random generator = new Random();
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
Color randomColor = new Color(r, g, b);
return randomColor;
}
public static void main(String[] args) {
CountDownGUI frame = new CountDownGUI();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.