aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes GUI display(rendering) problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI display(rendering) problem" Watch "GUI display(rendering) problem" New topic
Author

GUI display(rendering) problem

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Hi all,

I have a program that draws a pattern. The logic is correct and i get the final display but when Thread.sleep(1000) is added to see how its rendered; the frame gets frozen till the program completes.



Please help!


www.cs-repository.info
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
If my understanding is correct... all paintComponent code is executed on the EDT. So if your calling Thread.sleep inside paintComponent you are essentially going to freeze the program for whatever amount of time you specified. When that's done the program will resume execution after that point. The EDT is one thread which controls the painting in swing... if you freeze that then you are going to freeze all the GUI drawing.


("Anger is not an emotion, its a symptom of fear.")
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You have a very short time between successive sleep() periods; that is too short a time for the JVM to draw the GUI. Try changing the Thread.sleep() to System.in.read(); which will require some sort of keystroke (the enter key might be best, especially if you hold it down and it repeats automatically).
System.in.read() declares IOException, which you will have to handle.

By the way: Your repeated calls to Thread.sleep() represent duplicated code, which you should avoid. You should write a sleep() method instead. Never have an empty catch block. In this instance you can probably get away with it, but in other instances it can obscure nasty errors.

And I think that is correct about the event dispatch thread. Beware: some people may not be familiar with the EDT abbreviation.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Thank you folks. I'm just on the shores of Java GUI and have no knowledge about any higher concepts. Yet to read on this EDT stuff.

And thanks for your detailed reply, Campbell. I changed the code to


But it gives the same problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Sorry. I tried it with System.in.read(); and with repaint() in the loop repeatedly, and this line as the first line of paintComponent. . . but I couldn't get it to work. Sorry again.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
see if you can work out the changes

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Yes, I could, thank you. And it worked.


Oh, maybe you meant that question for John Eipe
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Oh, maybe you meant that question for John Eipe

yes, it was meant for the OP (sorry for the confusion)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What makes you think I was confused?
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Great! it worked. Thanks Michael.

Infact, yesterday i was reading about SwingWorker class but i could solve this problem.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
next part of the learning curve:

run the program, then minimize the frame to the taskbar.
when you restore it, your spider will (should) have disappeared.

to fix, you need to retain a collection of the 'lines' (arraylist will do),
so instead of changing the values of xx1 etc, you add a new 'line' of those coords
to the collection, and in paintComponent, the collection is iterated to repaint
each line in the collection.

also, as mentioned in one of the earlier replies, the first line of paintComponent is generally
super.paintComponent(g);

have a go, see if you can get it working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI display(rendering) problem