Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

java - How would I go about making my paint for my JFrame automatically update?

Currently, I have a JFrame that contains a JPanel. The JPanel is the paint for my program. Paint meaning all of my Graphics g.drawString things. Right now, it only updates the display whenever the user interacts with the JFrame, but I want it to continuously update (repaint()) itself WITHOUT using a while loop (too much CPU usage).

Anyone know how I could do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you need to do is to inform the swing component whenever you know that part of the image on your panel changed. The normal way to do this is to, as you said, call repaint() and if you know the rectangle that was 'invalidated' you can also indicated that.

Depending on the events that cause the contents of the JPanel, you need to change your application design so that the presentation part of your application 'listens' to changes in the data underneath and repaints when these changes occur.

There is another method called paintImmediately() which might do for certain situations, but you have to describe a little more your scenario and in which cases are you needing to repaint continuously.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...