Solved

how to force repaint()?

Posted on 1998-12-24
3
872 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 52
eclipse buid path vs tomcat lib path 10 37
java example issue 5 42
Java array 21 78
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

685 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