Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My Games Flashing!!

 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it! Its not just add Painter... its getContentPane().add......
Thanks Ernest for all your help!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic