Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI display(rendering) problem

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49775
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49775
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see if you can work out the changes

 
Campbell Ritchie
Sheriff
Pie
Posts: 49775
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I could, thank you. And it worked.


Oh, maybe you meant that question for John Eipe
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Oh, maybe you meant that question for John Eipe

yes, it was meant for the OP (sorry for the confusion)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49775
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think I was confused?
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! it worked. Thanks Michael.

Infact, yesterday i was reading about SwingWorker class but i could solve this problem.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic