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! :)

[7619 byte] By [DishSoapMonkeya] at [2007-11-27 3:37:38]
# 1

When code is executed in the GUI Thread then the GUI never gets a chance to repaint itself until the code is finished executing.

Using a loop like that is a terrible way to waste time. The CPU will loop at 100%.

You should be using a [url http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]Swing Timer[/url].

camickra at 2007-7-12 8:40:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Using a loop like that is a terrible way to waste

> time. The CPU will loop at 100%.

>

> You should be using a [url

> http://java.sun.com/docs/books/tutorial/uiswing/misc/t

> imer.html]Swing Timer[/url].

ty ty ty ... hahaha i knew using a for loop wasnt the way to go for what i was doing... i just didnt know what else to turn to....

i've been sitting here fooling around with the Swing Timer...but with my program i cant quite figure out a way see if the event is the timer or the buttons...

Two questions came to me...

is there a way to set the action command as u would for a JButton? (setActionCommand is for buttons... but i didnt see one like this in the documentation for the timer....)

and

is there a way to set the timer up with a different listener?

thnx a lot :)

DishSoapMonkeya at 2007-7-12 8:40:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

i edited my code to this:

(i flagged the parts i changed to use the Swing Timer with a "//NEW" comment)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.Scanner;

public class x5 extends JFrame implements ActionListener

{

JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;

int first, second, third, trials = 0;

Timer closeCount = new Timer(2500, this); //NEW

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 );

}

public void actionPerformed( ActionEvent evt)

{

if(evt.getActionCommand() == null) //NEW

System.exit(0);

else

{

int guess = Integer.parseInt(evt.getActionCommand());

getContentPane().setBackground(Color.yellow);

if(!onePassed)

{

if(guess == first)

onePassed = true;

}

else if(!twoPassed)

{

if(guess == second)

twoPassed = true;

else

onePassed = false;

}

else if(guess == third)

{

trials = 0;

getContentPane().setBackground(Color.green);

repaint();

closeCount.start(); //NEW

}

else

{

onePassed = false;

twoPassed = false;

}

trials = trials + 1;

if(trials == 3)

{

trials = 0;

getContentPane().setBackground(Color.red);

}

repaint();

}

}

public static void 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 );

}

}

is this a proficient use of the Swing Timer?

DishSoapMonkeya at 2007-7-12 8:40:55 > top of Java-index,Desktop,Core GUI APIs...
# 4

Create a separate ActionListener for the Timer:

Timer closeTimer new Timer(1000, new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

});

camickra at 2007-7-12 8:40:55 > top of Java-index,Desktop,Core GUI APIs...
# 5
SWEET! i knew there was something stupid like that that i was missing! i <3 you!thnx
DishSoapMonkeya at 2007-7-12 8:40:55 > top of Java-index,Desktop,Core GUI APIs...