File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes My Games Flashing!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "My Games Flashing!!" Watch "My Games Flashing!!" New topic
Author

My Games Flashing!!

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I decided to get rid of the flashing in my game by double buffering it. Now there isn't any flshing but... the objects are being repainted and then not being taken away from the applet when they move creating a line where there is suppose to be a ball bouncing up and down. If somebody can help me with this it would be most appreciated! Or, I had an idea to refresh the applet every say... 100 milliseconds. But I don't know who to use ImageInputStream class. If there is another way to do the double buffering without my problem, can somebody give me an example on how to refresh the applet so that previous drawn objects dissapear and the ball bounces normal?


Live And Let Learn.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You've implemented double-buffering incorrectly, plain and simple.

Someone suggested to you a few days ago that you simply use Swing, which gives you double-buffering for free, and you said something about not knowing Swing. It's really very simple:

1) Extend javax.swing.JApplet instead of java.applet.Applet

2) Don't override update()

3) Don't override paint(). Instead, override paintComponent():



4) The resulting applet is going to require the Sun Java Plugin to run -- i.e., it won't run in the crufty old JVM that comes with some old versions of Windows.

That's it -- you're done!

Now: why was this posted in the "Performance" forum? All of your threads really belong in Swing/AWT, where I'm moving this one. Note, also, by constantly starting new threads, you lose the context of previous discussion, and people who read your questions don't know how to help.


[Jess in Action][AskingGoodQuestions]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
So if this isn't doublebuffering... what is it? Also... I took out unwanted code but its not compiling. Is my code right... or is it that I have to upgrade my jdk package?


[ May 13, 2006: Message edited by: Martin vanPutten ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You'll have to forgive me, as I left out some important details. Nevertheless, the problems you're seeing with this version aren't all my fault, so we'll share the blame this time, OK?

Here is a recipe for making the double-buffering work in a JApplet. I'm sorry I didn't tell you this before but I frankly just forgot that JApplet isn't actually a true Swing component itself!

1) Move the paintComponent() method into an inner class that extends JPanel. Just surround your existing paintComponent method, unchanged with

private class Painter extends JPanel {

and

}

2) Move the setBackground() line from init() to be the first line in paintComponent()

3) In init(), add this line near the beginning:

add(new Painter());

4) (Your part) you didn't actually change the class to extends JApplet; it still extends Applet. Make that change now.

5) The line

import javax.swing.JApplet.*;

is wrong and unnecessary; remove it.

I can't say the whole class will work OK, but the double-buffering will then work fine.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Thanks for your help! It compiles perfect. But... when I run it, it says the applet is not initialized. With the changes that I made from your help help, here is what I have: (I hope you know why?)
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I got it! Its not just add Painter... its getContentPane().add......
Thanks Ernest for all your help!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Martin vanPutten:
I got it! Its not just add Painter... its getContentPane().add......
Thanks Ernest for all your help!!


In JDK 1.5, just "add()" is OK; but you're right, if you're using an older JDK, then the old-fashioned getContentPane().add() is required.

Glad we got it working!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: My Games Flashing!!
 
Similar Threads
applet flickering/blinking/flashing on Mac
double buffering
Refresh problem in applet
Repainting of applets
Images