Solved

how to force repaint()?

Posted on 1998-12-24
3
861 Views
Last Modified: 2008-03-17
Hi,

  My code has the following structure:

for(int i = 0; i < 10000; i++) {
  do some calculation;
  drawCanvas.repaint();
}

What I want is to do some calculation and plot the progress, and so on. But it seems that only after the loop is finished, the my "drawCanvas" is repainted(WM_PAINT message is blocked). May I know how to force painting? In MFC, we can use GetClientDC() to obtain a graphic device to draw on. Is there anything similar in Java? By the way, I need this to be done with JDK1.0.2 for maximum compatibility.
Thanks for your time.

wyldsj
0
Comment
Question by:wyldsj
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 20 total points
ID: 1230111
wyldsj, the repaint method notifys the awt thread that it needs to start a paint cycle. Now there are two options:
1. Your loop is done by the awt thread (for example inside one of the event handlers etc.). In that case, you have to change the design because the awt thread shouldn't be doing cpu intensive tasks.
2. You are doing the loop on a different thread, and your thread is very cpu intensive. There is a chance that the awt thread get very little reasources (cpu time), and what you can do is use the  command:  Thread.currentThread().yield();  After you are issueing the repaint(). This will cause the current thread to give up the cpu and give the awt thread the chance to do its work.

Hope this helps.
G
0
 

Author Comment

by:wyldsj
ID: 1230112
gadio: you are right. My loop is within an event handler. It actually occurs after a button is clicked. But I cannot figure out a different structure... Is there any suggestions from you?

wyldsj
0
 

Author Comment

by:wyldsj
ID: 1230113
Hi! I just figured it out myself and it worked now! Thanks for your help, gadio!

wyldsj
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now