Subject: repaint() before System.exit(0)
I have a program that is supposed to be like a combination lock... its lame, but for the sake of programming later projects,i would like to know how i can get my JFrame to repaint() prior to System.exit(0). My program is supposed to repaint the background of my GUI to a color corresponding to ur access to the combination lock...
What i cant seem to do is get my program to reset the background color, repaint, wait, then close... It seems like it only repaints once it finishes with my actionPerformed( ActionEvent evt), but i need it to repaint prior to that because my program will close before control flow gets to that point!
:-/
(i havent gotten the colors to show up exactly how i want them, so the logic of the program might seem a little silly ;-))
Here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
publicclass x5extends JFrameimplements ActionListener
{
JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
int first, second, third, trials = 0;
boolean onePassed = false,
twoPassed =false;
public x5(int f,int s,int t)
{
first = f;
second = s;
third = t;
btn0 =new JButton("0");
btn1 =new JButton("1");
btn2 =new JButton("2");
btn3 =new JButton("3");
btn4 =new JButton("4");
btn5 =new JButton("5");
btn6 =new JButton("6");
btn7 =new JButton("7");
btn8 =new JButton("8");
btn9 =new JButton("9");
btn0.setActionCommand("0" );
btn1.setActionCommand("1" );
btn2.setActionCommand("2" );
btn3.setActionCommand("3" );
btn4.setActionCommand("4" );
btn5.setActionCommand("5" );
btn6.setActionCommand("6" );
btn7.setActionCommand("7" );
btn8.setActionCommand("8" );
btn9.setActionCommand("9" );
getContentPane().setLayout(new FlowLayout() );
getContentPane().setBackground(Color.yellow);
getContentPane().add( btn1 );
getContentPane().add( btn2 );
getContentPane().add( btn3 );
getContentPane().add( btn4 );
getContentPane().add( btn5 );
getContentPane().add( btn6 );
getContentPane().add( btn7 );
getContentPane().add( btn8 );
getContentPane().add( btn9 );
getContentPane().add( btn0 );
btn0.addActionListener(this );
btn1.addActionListener(this );
btn2.addActionListener(this );
btn3.addActionListener(this );
btn4.addActionListener(this );
btn5.addActionListener(this );
btn6.addActionListener(this );
btn7.addActionListener(this );
btn8.addActionListener(this );
btn9.addActionListener(this );
setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
}
publicvoid actionPerformed( ActionEvent evt)
{
int guess = Integer.parseInt(evt.getActionCommand());
getContentPane().setBackground(Color.yellow);
if(!onePassed)
{
if(guess == first)
onePassed =true;
}
elseif(!twoPassed)
{
if(guess == second)
twoPassed =true;
else
onePassed =false;
}
elseif(guess == third)
{
getContentPane().setBackground(Color.green);
repaint();
/*this repaint is completely ignored until(hypothetically) the control flow finishes the actionPerformed( ActionEvent evt)*/
for(int j=0;j<1000000000;j++)
;
System.exit(0);
}
else
{
onePassed =false;
twoPassed =false;
}
trials = trials + 1;
if(trials == 3)
{
trials = 0;
getContentPane().setBackground(Color.red);
}
repaint();
}
publicstaticvoid main ( String[] args )
{
Scanner scan =new Scanner(System.in);
int first, second, third;
System.out.print("Enter first digit: ");
first = scan.nextInt();
System.out.print("Enter second digit: ");
second = scan.nextInt();
System.out.print("Enter third digit: ");
third = scan.nextInt();
x5 frame =new x5(first, second, third) ;
frame.setSize( 150, 150 );
frame.setVisible(true );
}
}
Any suggestions/criticism would be appreciated! :)

