Need time waster code or something.

  • Follow


I'm trying to "flash" a button, by changing its icon from one color to 
another.

It works with the mouse events (on when clicked, off when released), but 
when I try to run the following code, I don't see a change.  I see 
something happen to the button (the color square gets ever so slightly 
smaller), but the color doesn't change.  If I comment out the code where 
it sets the color back off, it stays on, so I know its executing.

I'm assuming my for-loop wait mechanism is the problem, and might be 
going by so fast I can't see it.  I've tried larger numbers and the same 
problem.

Is there a better wait mechanism?


public void flashButton()
{
	button.setIcon(onIcon);
	for (long i = 0; i < 1000000; i++)
	{

	}
	button.setIcon(offIcon);
}
0
Reply Taunto 4/30/2006 1:28:35 AM

Taunto wrote:
> I'm trying to "flash" a button, by changing its icon from one color to 
> another.
> 
> It works with the mouse events (on when clicked, off when released), but 
> when I try to run the following code, I don't see a change.  I see 
> something happen to the button (the color square gets ever so slightly 
> smaller), but the color doesn't change.  If I comment out the code where 
> it sets the color back off, it stays on, so I know its executing.
> 
> I'm assuming my for-loop wait mechanism is the problem, and might be 
> going by so fast I can't see it.  I've tried larger numbers and the same 
> problem.
> 
> Is there a better wait mechanism?
> 
> 
> public void flashButton()
> {
>     button.setIcon(onIcon);
>     for (long i = 0; i < 1000000; i++)
>     {
> 
>     }
>     button.setIcon(offIcon);
> }

You can't do it that way.  You need a timer or a thread that sleeps. 
Here is an example of a JLabel that I wrote that displays the time.  You 
should be able to get where you want to go from this.

package com.knutejohnson.components;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class JTimeLabel extends JLabel implements ActionListener {
     private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
     private javax.swing.Timer timer = new javax.swing.Timer(250,this);

     public JTimeLabel() {
         super();
         timer.start();
     }

     public JTimeLabel(String text) {
         super(text);
         timer.start();
     }

     public JTimeLabel(String text, int horizontalAlignment) {
         super(text,horizontalAlignment);
         timer.start();
     }

     public JTimeLabel(String text, int horizontalAlignment, String 
pattern) {
         super(text, horizontalAlignment);
         this.sdf = new SimpleDateFormat(pattern);
         timer.start();
     }

     public void actionPerformed(ActionEvent ae) {
         setText(sdf.format(new Date()));
     }

     public static void main(String[] args) {
         JFrame f = new JFrame();
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.add(new JTimeLabel("24:00:00",JLabel.CENTER));
         f.pack();
         f.setVisible(true);
     }
}

-- 

Knute Johnson
email s/nospam/knute/
0
Reply Knute 4/30/2006 5:16:26 AM


Forgive the top posting.

Thanks, I'll try this.  I was trying a timer from some other code I 
found, and I kept getting painted into a corner trying to pass it 
non-static variables.  I'm trying to pass it the icons for the on color 
and the off color.


Knute Johnson wrote:
> Taunto wrote:
> 
>> I'm trying to "flash" a button, by changing its icon from one color to 
>> another.
>>
>> It works with the mouse events (on when clicked, off when released), 
>> but when I try to run the following code, I don't see a change.  I see 
>> something happen to the button (the color square gets ever so slightly 
>> smaller), but the color doesn't change.  If I comment out the code 
>> where it sets the color back off, it stays on, so I know its executing.
>>
>> I'm assuming my for-loop wait mechanism is the problem, and might be 
>> going by so fast I can't see it.  I've tried larger numbers and the 
>> same problem.
>>
>> Is there a better wait mechanism?
>>
>>
>> public void flashButton()
>> {
>>     button.setIcon(onIcon);
>>     for (long i = 0; i < 1000000; i++)
>>     {
>>
>>     }
>>     button.setIcon(offIcon);
>> }
> 
> 
> You can't do it that way.  You need a timer or a thread that sleeps. 
> Here is an example of a JLabel that I wrote that displays the time.  You 
> should be able to get where you want to go from this.
> 
> package com.knutejohnson.components;
> 
> import java.awt.*;
> import java.awt.event.*;
> import java.text.*;
> import java.util.*;
> import javax.swing.*;
> 
> public class JTimeLabel extends JLabel implements ActionListener {
>     private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
>     private javax.swing.Timer timer = new javax.swing.Timer(250,this);
> 
>     public JTimeLabel() {
>         super();
>         timer.start();
>     }
> 
>     public JTimeLabel(String text) {
>         super(text);
>         timer.start();
>     }
> 
>     public JTimeLabel(String text, int horizontalAlignment) {
>         super(text,horizontalAlignment);
>         timer.start();
>     }
> 
>     public JTimeLabel(String text, int horizontalAlignment, String 
> pattern) {
>         super(text, horizontalAlignment);
>         this.sdf = new SimpleDateFormat(pattern);
>         timer.start();
>     }
> 
>     public void actionPerformed(ActionEvent ae) {
>         setText(sdf.format(new Date()));
>     }
> 
>     public static void main(String[] args) {
>         JFrame f = new JFrame();
>         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>         f.add(new JTimeLabel("24:00:00",JLabel.CENTER));
>         f.pack();
>         f.setVisible(true);
>     }
> }
> 


0
Reply Taunto 4/30/2006 10:12:06 PM

Taunto wrote:
> Forgive the top posting.
> 
> Thanks, I'll try this.  I was trying a timer from some other code I 
> found, and I kept getting painted into a corner trying to pass it 
> non-static variables.  I'm trying to pass it the icons for the on color 
> and the off color.
> 

Don't pass them.  Extend Button and add a method that changes the icons. 
  Then all you have to have is a reference to the Button.

Pseudo-code:

class TauntoButton extends Button {
     Icon onIcon, offIcon;
     public TauntoButton(String text, Icon onIcon, Icon offIcon) {
        super(text);
        this.onIcon = onIcon;
        this.offIcon = offIcon;
     }

     public setStatus(boolean state) {
         if (state)
             setIcon(onIcon);
         else
             setIcon(offIcon);

-- 

Knute Johnson
email s/nospam/knute/
0
Reply Knute 4/30/2006 10:51:36 PM

On Sat, 29 Apr 2006 18:28:35 -0700, Taunto wrote:

> I'm trying to "flash" a button, by changing its icon from one color to 
> another.
> 
> It works with the mouse events (on when clicked, off when released), but 
> when I try to run the following code, I don't see a change.  I see 
> something happen to the button (the color square gets ever so slightly 
> smaller), but the color doesn't change.  If I comment out the code where 
> it sets the color back off, it stays on, so I know its executing.
> 
> I'm assuming my for-loop wait mechanism is the problem, and might be 
> going by so fast I can't see it.  I've tried larger numbers and the same 
> problem.

Busy-waiting is never, ever a good idea on modern computers for anything
outside kernel code.  It wastes huge amounts of CPU time doing nothing.

What you want to do is change the color, then create a Timer that will
change it back and create another Timer...

> Is there a better wait mechanism?
> 
> 
> public void flashButton()
> {
> 	button.setIcon(onIcon);
> 	for (long i = 0; i < 1000000; i++)
> 	{
> 
> 	}
> 	button.setIcon(offIcon);
> }

0
Reply Owen 5/2/2006 1:26:20 AM

> I'm assuming my for-loop wait mechanism is the problem, and might be going 
> by so fast I can't see it.  I've tried larger numbers and the same 
> problem.
>
> Is there a better wait mechanism?

public void flashButton() {
    button.setIcon(onIcon);
    button.paintImmediately(<icon rect>);
    try {
        Thread.sleep(<time amount>);
    }
    catch(InterruptedException ex) {
        //ignore
    }
    button.setIcon(offIcon);
}

Andrey

-- 
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


0
Reply Andrey 5/2/2006 8:36:19 PM

Ok, this is me from another account.

I had something like that, Audrey, and it works, but switched it to a
timer to try to solve my current problem.

NOW, I need to flash some buttons in succession, and not overlap each
other.

Here's my code for flashing the button:

	public void flashButton()
	{
		buttonFlash bf = new buttonFlash();
	}

	public class buttonFlash
	{
		Toolkit toolkit;
		Timer timer;


		public buttonFlash()
		{
			toolkit = Toolkit.getDefaultToolkit();
			timer = new Timer();
			//button.setIcon(onIcon);
			timer.schedule(new RemindTask(), 250, 250);
			button.setIcon(offIcon);
		}

		class RemindTask extends TimerTask
		{
			public void run()
			{
				button.setIcon(onIcon);

				System.exit(0);   //Stops the AWT thread
								  //(and everything else).
			}
    	}

	}


Here's the code for calling button flashes in succession:

public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if (source == exitButton)
		{
			System.exit(0);
		}
		else if (source == runButton)
		{
			// reset values and counter
			blueButton.resetGame();


			blueButton.flashButton();

			try
			{
				Thread.sleep(250);
			}
			catch (InterruptedException ie) {}


			redButton.flashButton();
			greenButton.flashButton();
			yellowButton.flashButton();

		}

		else if (source == validateButton)
		{
			//check answers against generated values
		}

	}

The code between the blueButton and the redButton does not do the job.
In fact, most of the time, I can't see the blueButton flash, but it
does sometimes, and partially other times, so I know its going off.

So, how do I put a lag between these flashButton calls?

0
Reply duh 5/4/2006 8:00:41 PM

> Here's my code for flashing the button:
<snip>

Please post compilable example (SSCCE)

Andrey

-- 
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


0
Reply Andrey 5/4/2006 9:54:49 PM

"duh" <nodamnspamok@yahoo.com> writes:

> The code between the blueButton and the redButton does not do the job.
> In fact, most of the time, I can't see the blueButton flash, but it
> does sometimes, and partially other times, so I know its going off.
> 
> So, how do I put a lag between these flashButton calls?

The problem is that you are sleeping in the same thread (Event
Dispatch Thread - EDT) as the GUI uses to actually do the drawing.
You are in that thread when the actionPerformed method is called.
You will need to create and start a new thread to do what you want.  The
part of the code that changes the interface needs to be executed in the
EDT, probably by calling SwingUtilities.invokeLater 


-- 
Thomas A. Russ,  USC/Information Sciences Institute
0
Reply tar 5/5/2006 3:58:51 PM

Andrey Kuznetsov wrote:
>>Here's my code for flashing the button:
> 
> <snip>
> 
> Please post compilable example (SSCCE)
> 
> Andrey
> 

One problem is that the code needs some jpg files for the button colors. 
  Don't know how I could post those here.

I'll see if I can just change the background color somehow.

-- 
-------------------------
"Work like no one is watching,
Dance like you've never been hurt, and
Love like you don't need the money"

\
=8{B
  \
0
Reply duh 5/9/2006 2:50:24 AM

duh wrote:
> Andrey Kuznetsov wrote:
> 
>>> Here's my code for flashing the button:
>>
>>
>> <snip>
>>
>> Please post compilable example (SSCCE)
>>
>> Andrey
>>
> 
> One problem is that the code needs some jpg files for the button colors. 
>  Don't know how I could post those here.
> 
> I'll see if I can just change the background color somehow.
> 

Ok, I got rid of the jpgs, but the button only has one "pressed" color 
(light grey), no matter what I define it as.  Probably getting 
overridden by the internal button press code.  But, this should compile.

Any help appreciated.  I've been wrangling with this for two weeks, I 
have no idea where to go with this, and the text book and the teacher 
are not helping.


//Main app file


import javax.swing.*;

class SimonApp
{
	public static void main(String[] args)
	{
		// pull up form
		JFrame frame = new SimonFrame();
		frame.setVisible(true);


	}
}

/////////////////////////////////////////////////////////////////////

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Thread;
import java.awt.Color;

class SimonFrame extends JFrame
{

	public SimonFrame()
	{

		setTitle("Simon");
		setSize(600, 400);
		centerWindow(this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new SimonContentPane();
		this.add(panel);

	}

	public void centerWindow(Window w)
	{
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		setLocation((d.width - w.getWidth())/2, (d.height - w.getHeight())/2);
	}

}

class SimonContentPane extends JPanel implements ActionListener
{
	private JButton 	runButton;
	private JButton 	exitButton;
	private JButton 	validateButton;
	private int[] answers = new int[4];
	private	int counter;

	private ColorButtonPanel 	blueButton, redButton, greenButton, yellowButton;

	public SimonContentPane()
	{
		int[] generated = new int[4];


		/* add parameters for button number, and size of answer array */

		blueButton = new ColorButtonPanel(new java.awt.Color(0, 0, 255), new 
java.awt.Color(0,0,128), 0);
		this.add(blueButton);

		redButton = new ColorButtonPanel(new java.awt.Color(255,0,0), new 
java.awt.Color(128,0,0), 1);
		this.add(redButton);

		greenButton = new ColorButtonPanel(new java.awt.Color(0, 255,0), new 
java.awt.Color(0, 128,0), 2);
		this.add(greenButton);

		yellowButton = new ColorButtonPanel(new java.awt.Color(0,255,255), new 
java.awt.Color(0, 128, 128), 3);
		this.add(yellowButton);

		runButton = new JButton("NEW GAME");
		runButton.addActionListener(this);
		this.add(runButton);

		validateButton = new JButton("CHECK ANSWERS");
		validateButton.addActionListener(this);
		this.add(validateButton);

		exitButton = new JButton("EXIT");
		exitButton.addActionListener(this);
		this.add(exitButton);

	}




	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if (source == exitButton)
		{
			System.exit(0);
		}
		else if (source == runButton)
		{
			// reset values and counter
			blueButton.resetGame();


			blueButton.flashButton();

			try
			{
				Thread.sleep(250);
			}
			catch (InterruptedException ie) {}


			redButton.flashButton();
			greenButton.flashButton();
			yellowButton.flashButton();

		}

		else if (source == validateButton)
		{
			//check answers against generated values
		}

	}

}


///////////////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import java.awt.Toolkit;
import java.awt.Color;

//import java.lang.Thread;

class ColorButtonPanel extends JPanel implements MouseListener
{
	private static int[] answers = new int[4];
	private static int counter;

	private int buttonValue;

	private JButton		button;

	private Color		onColor, offColor;
	private JTextField  textField;

	public ColorButtonPanel(Color offColor, Color onColor, int buttonValue)


	{

		this.onColor = onColor;
		this.offColor = offColor;


		//display panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		//onIcon = new ImageIcon(onColorFile);
		//offIcon = new ImageIcon(offColorFile);
		this.buttonValue = buttonValue;

		button = new JButton("      \n\n\n\n\n\n\n\n");
		button.setBackground(offColor);

		//button.setSize(20,20);

		button.addMouseListener(this);
		buttonPanel.add(button);

		counter = -1;

		//add panels to main panel
		this.setLayout(new BorderLayout());
		this.add(buttonPanel, BorderLayout.SOUTH);

	}

	public int getAnswers(int slot)
	{
		return answers[slot];
	}

	public int getCounter()
	{
		return counter;
	}

	public void resetGame()
	{
		counter = -1;
		answers[0] = -1;
		answers[1] = -1;
		answers[2] = -1;
		answers[3] = -1;

	}

	//public javax.swing.Icon getOnIcon()
	//{
	//	return onIcon;
	//}

	//public javax.swing.Icon getOffIcon()
	//{
	//	return offIcon;
	//}


	public void flashButton()
	{
		buttonFlash bf = new buttonFlash();
		//bf.run();

	}

	/*class buttonFlash extends Thread
	{
		public void run()
		{

			button.setIcon(onIcon);

			try
			{
				Thread.sleep(250);

				button.setIcon(offIcon);

			}
			catch (InterruptedException e) {}
		}
	}*/


	public class buttonFlash
	{
		Toolkit toolkit;
		Timer timer;


		//public buttonFlash()


		class RemindTask extends TimerTask
		{
			public void run()
			{
				button.setBackground(onColor);

				//System.exit(0);   //Stops the AWT thread
								  //(and everything else).
			}
     	}

	}
	public void mouseExited(MouseEvent e)
	{}

	public void mouseEntered(MouseEvent e)
	{}

	public void mousePressed(MouseEvent e)
	{
		button.setBackground(onColor);
	}

	public void mouseReleased(MouseEvent e)
	{
		button.setBackground(offColor);
		counter++;
		try
		{
			answers[counter] = buttonValue;
		}
		catch (ArrayIndexOutOfBoundsException aob)
		{
			System.out.println("Too many button presses");
		}
	}

	public void mouseClicked(MouseEvent e)
	{}
}





-- 
-------------------------
"Work like no one is watching,
Dance like you've never been hurt, and
Love like you don't need the money"

\
=8{B
  \
0
Reply duh 5/9/2006 3:40:44 AM

On Mon, 08 May 2006 19:50:24 -0700, duh <nosoupforU@yoohoo.com> wrote,
quoted or indirectly quoted someone who said :

>One problem is that the code needs some jpg files for the button colors. 
>  Don't know how I could post those here.
If you have a website, you could post the whole kaboodle as a jar.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply Roedy 5/9/2006 3:52:15 AM

11 Replies
153 Views

(page loaded in 0.142 seconds)

Similiar Articles:


















7/8/2012 8:30:31 PM


Reply: