aspose file tools*
The moose likes Applets and the fly likes Why is screen flashing when I call repaint()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Why is screen flashing when I call repaint()?" Watch "Why is screen flashing when I call repaint()?" New topic
Author

Why is screen flashing when I call repaint()?

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I'm pretty new to applets, but I'm trying to turn my Fractal
explorer program into an applet.
In the original application, I was calling the paintComponent(Graphics g) method, and within this, I was displaying a image, as well as other things, and whenever I had to repaint() the frame many times, it was fine.

But now, I'm using an applet and a paint(Graphics g) method. Again I'm initially displaying an image, and a moving box, but whenever repaint() is called, the screen flashes, the basic code is shown below:


I didn't want to use a JFrame, becuz I wanted the output to go into a rectangle on the HTML screen.
There is an alternative I can think of, using an OverlayLayout, but
I would rather see if there is anyway around this first.
Thanks for any help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Short answer: use a JApplet instead of an Applet, and use paintComponent(). You'll get double-buffering just like you got in your JFrame.


[Jess in Action][AskingGoodQuestions]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ahh, but hmmm.
I was invoking this from an HTML file using the tag:


So would that still be ok, to use that?

OK, sorry, I just checked API, and JApplet extends Applet, so it should be ok.
[ November 02, 2005: Message edited by: colin shuker ]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I just tried extending JApplet instead of applet, and using paintComponent
instead of paint, and it didn't work.
Can't see why?
Any ideas?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
it didn't work.


What does that mean?


Ping & DNS - my free Android networking tools app
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Oh sorry, I should have explained better.
Using JApplet instead of Applet, and paintComponent(Graphics g) instead of paint(Graphics g) now just shows a blank grey screen, except for the panel of 4 buttons at the bottom.

I've created a new small JApplet (see my other post about JApplet), I can't
even get this to draw a line.
I'm probably missing something stupid, but I don't know what.
Any advice is much appreciated, thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
I've never used JApplets, but there's an introduction over at java.sun.com.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I was looking at it earlier.
I looked at an example, they had an inner class of the class that extends
JApplet, which extended JPanel.
I copied this idea for my applet, but it wouldn't upload when I tried to
use Geocities for my website, because of the $ in the inner class name.

But then I figured out how to write the classes separatly and it worked.
Its taken me the whole day to get this far, but I've finally done it.
 
jQuery in Action, 2nd edition
 
subject: Why is screen flashing when I call repaint()?