Solved

how to force repaint()?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 34
mysql jsp example issue 32 50
ejb mdb examples 1 8
Java program running SQL query 5 37
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

829 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