wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java not for NASA? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java not for NASA?" Watch "Java not for NASA?" New topic
Author

Java not for NASA?

Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
Hi everyone,
I am experimenting with GUIs and am trying to do something relatively simple. I have a countdown program (hence the NASA reference) that displays numbers in a textfield. Well, technically it counts UP, but that is beside the point. Anyway, I have a separate thread to do the counting, and it seems to work, excpet that periodically the numbers in the textfield flash off for a second, but the counting still continues, and then the numbers reappear. I am suspecting that this has something to do with Java repainting the GUI. Anyway, here is the code, its pretty basic:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Supercount
{
public static void main(String args[])
{
Frame frame=new Frame("Dan's Mad Project");
frame.setSize(200,200);
Window dan=new Window(frame);
dan.setLocation(0,0);
dan.setSize(300,300);
CountPanel panel=new CountPanel();
dan.add(panel);
dan.show();
frame.show();
Thread runner=new Thread(panel);
runner.start();
}
}
public class CountPanel extends JPanel implements Runnable
{
JTextField countField;
public CountPanel()
{
countField=new JTextField(20);
add(countField);
}
public void run()
{
int count;
for(count=0;count<10000;count++)
{
countField.setText(""+count);
repaint();// My attempt to fix the problem
}
}

}
Anyone have any ideas on how I can get a nice smooth counting display? I suppose slowing down the counting would work, but are there any other ways?
Thanks in advance,
Dan
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Sure; do it with messages instead. Create a timer bit that acts as an event source; then let the GUI act as a listener to those events.
This has the benefit of allowing you to capture countdown information asynchronously, i.e., "as it happens," rather than when your thread is ready to execute the update.
You'd need an event object and listener interface to do this, a la:

A class that sends event objects does so by providing a framework for registering listeners (who all implement the interface). When an "event" occurs, the event source constructs an event, and iterates through the list of TimerListeners it knows about, invoking timerTicked() on each one.
If I am properly worshipped (i.e., compensated), I can provide the rest, or you can show a little pride and do it yourself.
Hope this helps,
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
That does help quite a bit. I have a chapter in a book that shows how to make custom events so I'll work from that. One question though. In my original code I can understand why the countup would not occur at a constant rate (since it would depend on when the thread for painting was executed) but why would there be periods of a second or so when there was nothing in the text field? Is it because during those times the entire GUI is being cleared and then painted?
Actually on second thought, I lied, I don't understand why the countup is not at a constant rate. I'm not doing anything to the GUI so why is it painting it over and over again? (assuming that I am correct that this is the cause for the jerky count rate)
Also I just wanted to say that the Certification Study Guide you co-wrote was a damn fine piece of work. I doubt I would have scored as high on the SCJP exam if I didn't have that book.
Dan
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Dan Temple:
Also I just wanted to say that the Certification Study Guide you co-wrote was a damn fine piece of work. I doubt I would have scored as high on the SCJP exam if I didn't have that book.

Completely off topic, but I'm also looking at this book for my certification (trying to get work to buy it for me first). Also, I know where to chase a refund if it doesn't work
(I'm sure Michael doesn't mind the plug!)
Dave.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Sigh, well, y'know, I was actually just trying to help a *little*. Then you have to go and praise RHE. Man.
Couple things here.
I inserted a Thread.sleep(1000) call in your code and it worked out very nicely. Tamped it down to 100 ms a shot and it was still decent, with some flicker on my PII-400 with plenty of stuff sucking up memory alongside.
Maybe calling repaint() 1000 times as fast as you possibly can is in fact the problem? I didn't get the long absence of a number like you did, just plenty o' flicker. Try another system, see if the observable results are the same. It also strikes me that if you actually paint the numbers, it might look a lot better.
Too lazy to look up whether JTextField is double-buffered; I sure hope it isn't, based on what I saw. If not, adding that in would certainly smooth out the displayables.
And of course, thank you very much for the kind words. I'm glad you found RHE useful.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited October 29, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java not for NASA?
 
Similar Threads
Java Screensaver! ... almost ...
Separating form code in a JFrame Class
Mediatortype javaprog requiring solution
using a bean to put slider on panel
Mediatortype javaprog requiring solution