Practical tech info. But seems to be sorely needed by artists. (At
least it always is with me, so maybe other people too)
Talking to somebody about "double buffering" (for animation)
specifically in Java. When trying to learn about it, I never could
find just a straight answer. It's really not nearly as complicated
as the name makes it sound.
Here's what happens. a program draws (in the order of the commands).
If no updates are required, that works fine. But for "animation"
it's gotta happen over and over ("threading"). Thing is, if you just
call the paint() method continuously, it makes this really extreme
flicker effect. It draws everything right on the screen, erases
everything, then draws the next pass.
So, the idea here is you "draw" the whole thing off screen, so when
it has to display it it can do the whole rectangular shape all at
once (faster), erases the off screen version and "redraws", then
plops the new one down right over the old one. So nothing disappears
and no flicker.
Here's how. (if anybody wants it)
judson
this SHOULD look pretty familiar but if it's completely alien, you
probably just have no use for this. God forbid anyone would think
this was code with no context.
before anything, where you put your global variables
add this:
ImagemyOffScreenImg;
GraphicsmyOffScreenGfx;
In the init() function
add this:
myOffScreenImg = createImage(this.size().width, this.size().height);
myOffScreenGfx = myOffScreenImg.getGraphics();
In the run() function (gotta "thread" to create "animation")
call repaint() every pass instead of paint()
public void run()
{
while (true)
{
repaint(); // i think it just calls update(),
// which you then tell to call paint()
try
{
Thread.sleep(someNumber);
}
catch (InterruptedException e) {}
}
}
Add this anywhere
public void update(Graphics g)
{
paint(g);
}
you always have to have a paint function but you can play around with
repaint() and update() for different effects (like "trails" where the
update is pasted down but the old version isn't erased). Call em,
don't call em, substitute them, re-define them… Another trick is
having some things paint() directly to the screen but some buffered.
in the paint(Graphics g) function (given that parameter)
anything you want to draw off screen, use myOffScreenGfx. in stead of
g. as in myOffScreenGfx.fillOval(50,120,30,30); vs.
g.fillOval(50,120,30,30); (By the way, you will be happier not using
the variable name "myOffScreenGfx" as you'll end up having to type it
all day.)
Finally, add this line at the end of paint(Graphics g), it puts that
whole thing you drew off screen onto the screen at once.
g.drawImage(osImg, 0, 0, this);
~~~~~~~~~~~~~~~~~~~~~~~~~~
PLASMA STUDII
http://plasmastudii.org
223 E 10th Street
PMB 130
New York, NY 10003