So I am working on making a version of "Who what to be a millionaire" in netbeans and I am having problems with the timer. My working code basically changes the color of the number to red after 11 seconds and it disappears( turns white ) at 1 second. What i am trying to do is make the numbers flash after second 6 from 5,4,3,2,1 flash. But i can't find a was to make that happen. I have tried changing
Thread.sleep(1000);
so i can write a more detailed if statement like
if (counter < 5.75 )
g.setColor(Color.WHITE);
if (counter < 5.25 )
g.setColor(Color.BLACK);
but it didn't work..
This is what I've done up to now:
package timer2;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class thread extends JPanel implements Runnable {
private static Object ga;
int counter;
Thread cd;
public void start() {
counter =30 ;
cd = new Thread(this);
cd.start();
}
public void stop()
{
cd = null;
}
public void run() {
while (counter>0 && cd!=null) {
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
--counter;
}
}
public void paintComponent( Graphics g)
{
repaint();
super.paintComponent(g);
g.setColor(Color.BLACK);
if (counter < 1 )
g.setColor(Color.WHITE);
g.setFont(new Font("Times New Roman",Font.BOLD,35));
if (counter < 11)
g.setColor(Color.RED);
if (counter < 1 )
g.setColor(Color.WHITE);
g.setFont(new Font("Times New Roman",Font.BOLD,100));
g.drawString(String.valueOf(counter),600,600);
}
public static void main(String[] args) {
JFrame j=new JFrame();
thread t=new thread();
t.setBackground(Color.WHITE);
t.start();
j.add(t);
j.setVisible(true);
j.getContentPane().setBackground( Color.WHITE );
j.setBounds(-8,-8,500,500);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
repaint()
out of paintComponent. This should never be called from there.while (true)
loop, but rather will want to use the much simpler Swing Timer to do your counting.Also I recommend that you try to improve the formatting of your code that you post in here and your code in general. Good formatting including using an indentation style that is uniform and consistent will help others (us!) to better understand your code, and more importantly, it will help you to better understand your code and thus fix your own bugs. Also it shows that you're willing to put in extra effort to make it easier for the volunteers here to help you, and that effort is much appreciated.
To flash -- use a Timer with a shorter period, say 200 mSec, and change the color within the Timer.
e.g.,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.*;
@SuppressWarnings("serial")
public class CountDownTimer extends JPanel {
private static final String FORMAT = "%02d";
private static final Color[] TIMER_COLORS = {Color.BLACK, Color.WHITE};
private static final int LABEL_PTS = 90;
private static final Font TIMER_LABEL_FONT = new Font("Times New Roman", Font.BOLD, LABEL_PTS);
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 100;
public static final int FLASH_TIME = 6;
private JLabel timerLabel = new JLabel("");
private Timer timer;
private int timerColorIndex = 0;
public CountDownTimer(int seconds) {
setTimerCount(seconds);
setLayout(new GridBagLayout());
add(timerLabel);
timer = new Timer(TIMER_DELAY, new TimerListener(seconds));
timer.start();
}
public final void setTimerCount(int count) {
String text = String.format(FORMAT, count);
timerLabel.setText(text);
timerLabel.setFont(TIMER_LABEL_FONT);
}
public void flash() {
timerColorIndex++;
timerColorIndex %= TIMER_COLORS.length;
timerLabel.setForeground(TIMER_COLORS[timerColorIndex]);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class TimerListener implements ActionListener {
private int mSeconds;
public TimerListener(int secondsLeft) {
this.mSeconds = 1000 * secondsLeft;
}
@Override
public void actionPerformed(ActionEvent e) {
mSeconds -= TIMER_DELAY;
int seconds = (mSeconds + 999) / 1000;
if (seconds < FLASH_TIME) {
flash();
}
setTimerCount(seconds);
if (seconds == 0) {
((Timer) e.getSource()).stop();
}
}
}
private static void createAndShowGui() {
int seconds = 20;
CountDownTimer mainPanel = new CountDownTimer(20);
JFrame frame = new JFrame("CountDownTimer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}