Board index » jbuilder » Wanting a Random timer

Wanting a Random timer


2005-03-08 02:22:32 AM
jbuilder2
Hi All,
This program will demo a random rate change while the
dropping of the word "Hello" on the screen. This design was
prompted by the fact that having once set the timer then it
should not be revised while running. Is that correct? or is
there a better way to affect a sporadic rate of falling of the
word hello on the screen using a timer, and while the program
is running?
package testinggraphicsoozingahelloframe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GraphicsOozingAHelloFrame
extends JComponent {
public int aNumber, maxCount, count;
public Timer myTimer;
public int delay, width, height;
ActionListener taskPerformer;
public GraphicsOozingAHelloFrame() {
count = 0;
aNumber = 10;
maxCount = 40;
delay = 1000;
taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// was aNumber++;
if (aNumber == maxCount) {
myTimer.stop();
return;
}
// this will force it to stand still sometimes.
// if a zero is added then it remains at that height,
// if a one then it moves down to the next position.
repaint();
int addNum = (int)(Math.random() *2);
aNumber = aNumber + addNum;
repaint();
}
};
// only is entered once on start hence delay is set only once
myTimer = new Timer(delay, taskPerformer);
myTimer.start();
}
public static void main(String[] args) {
GraphicsOozingAHelloFrame
GraphicsOozingAHelloFrame1 = new
GraphicsOozingAHelloFrame();
JFrame frame = new
JFrame("GraphicsOozingAHelloFrame");
frame.getContentPane().add(new
GraphicsOozingAHelloFrame());
// set size + visiblity
frame.setSize(150, 150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
;
}
public void paint(Graphics g) {
System.out.println("Now in Paint");
g.setColor(Color.white);// wipes the slate clean.
g.fillRect(0,0,150, 150);
g.setColor(Color.blue);
System.out.println("aNumber = "+ aNumber);
g.drawString("Hello", 10, aNumber);
}
}
 
 

Re:Wanting a Random timer

oops what I intended to show is:
..........
// this will force it to stand still sometimes.
// if a zero is added then it remains at that height,
// if a one then it moves down to the next position.
int addNum = (int)(Math.random() *2);
aNumber = aNumber + addNum;
repaint();
}
};
...........
"Bob H" < XXXX@XXXXX.COM >wrote:
Quote

Hi All,

This program will demo a random rate change while the
dropping of the word "Hello" on the screen. This design was
prompted by the fact that having once set the timer then it
should not be revised while running. Is that correct? or is
there a better way to affect a sporadic rate of falling of the
word hello on the screen using a timer, and while the program
is running?

package testinggraphicsoozingahelloframe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GraphicsOozingAHelloFrame
extends JComponent {

public int aNumber, maxCount, count;
public Timer myTimer;
public int delay, width, height;
ActionListener taskPerformer;

public GraphicsOozingAHelloFrame() {
count = 0;
aNumber = 10;
maxCount = 40;
delay = 1000;
taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {
// was aNumber++;
if (aNumber == maxCount) {
myTimer.stop();
return;
}
// this will force it to stand still sometimes.
// if a zero is added then it remains at that height,
// if a one then it moves down to the next position.

repaint();
int addNum = (int)(Math.random() *2);
aNumber = aNumber + addNum;

repaint();
}

};
// only is entered once on start hence delay is set only once
myTimer = new Timer(delay, taskPerformer);
myTimer.start();
}

public static void main(String[] args) {
GraphicsOozingAHelloFrame
GraphicsOozingAHelloFrame1 = new
GraphicsOozingAHelloFrame();
JFrame frame = new
JFrame("GraphicsOozingAHelloFrame");
frame.getContentPane().add(new
GraphicsOozingAHelloFrame());
// set size + visiblity
frame.setSize(150, 150);
frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
;

}
public void paint(Graphics g) {
System.out.println("Now in Paint");
g.setColor(Color.white);// wipes the slate clean.
g.fillRect(0,0,150, 150);
g.setColor(Color.blue);
System.out.println("aNumber = "+ aNumber);
g.drawString("Hello", 10, aNumber);
}
}


 

Re:Wanting a Random timer

On 3/7/2005 at 1:22:32 PM, Bob H wrote:
Quote
This program will demo a random rate change while the
dropping of the word "Hello" on the screen. This design was
prompted by the fact that having once set the timer then it
should not be revised while running. Is that correct?
The javax.swing.Timer does not deal well with the delay being changed
while it is running. When the Timer is running, it is on the Timer queue,
and the queue is not notified of the change. As a result, the change does
not take effect until after the Timer fires again, and it is re-queued.
I suspect that it would be possible to make this work, but the TimerQueue
runs on one thread and the Timer's Runnable runs on the event thread, so
there is clearly a possibility of a race condition.
Quote
or is there a better way to affect a sporadic rate of falling of
the word hello on the screen using a timer, and while the program
is running?
A Timer can be used in repeat mode or non-repeat mode. You can set the
Timer to not repeat and then set the new delay time and start it again
every time it fires.
Make sure you set the *InitialDelay*. That is the time used when the
timer is started, and is therefore the only time that has an effect when
repeat is false. The InitialDelay value is set to the Delay value when
the timer is initialized, and setting the Delay will not change it.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:Wanting a Random timer

Thanks John for your reply.
I appreciate it.
Bob H
"John McGrath [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
On 3/7/2005 at 1:22:32 PM, Bob H wrote:

>This program will demo a random rate change while the
>dropping of the word "Hello" on the screen. This design was
>prompted by the fact that having once set the timer then it
>should not be revised while running. Is that correct?

The javax.swing.Timer does not deal well with the delay being changed
while it is running. When the Timer is running, it is on the Timer queue,
and the queue is not notified of the change. As a result, the change does
not take effect until after the Timer fires again, and it is re-queued.

I suspect that it would be possible to make this work, but the TimerQueue
runs on one thread and the Timer's Runnable runs on the event thread, so
there is clearly a possibility of a race condition.

>or is there a better way to affect a sporadic rate of falling of
>the word hello on the screen using a timer, and while the program
>is running?

A Timer can be used in repeat mode or non-repeat mode. You can set the
Timer to not repeat and then set the new delay time and start it again
every time it fires.

Make sure you set the *InitialDelay*. That is the time used when the
timer is started, and is therefore the only time that has an effect when
repeat is false. The InitialDelay value is set to the Delay value when
the timer is initialized, and setting the Delay will not change it.

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html