Animation

This is my code:

import element.*;

import java.awt.Color;

public class jonesp_assign1

{

static DrawingWindow d;

public static void main(String[] args)

{

d = new DrawingWindow(800,800);

int i;

for(i=1; i < 4000; i = i + 20)

{

sky();

tree();

bush();

road();

sun(i);

car(i);

}

}

public static void sky()

{

Rect sky = new Rect(0,0,800,800);

d.setForeground(new Color(128,255,255));

d.fill(sky);

Rect bottom = new Rect(0,680,800,120);

d.setForeground(Color.white);

d.fill(bottom);

}

public static void tree()

{

Rect trunk = new Rect(640,520,40,136);

int XCentre = 660, YCentre = 450, Radius = 80;

Circle treeTop = new Circle(XCentre,YCentre,Radius);

d.setForeground(new Color(128,64,0));

d.fill(trunk);

d.setForeground(Color.green);

d.fill(treeTop);

}

public static void road()

{

Rect road = new Rect(0,656,800,24);

d.setForeground(Color.lightGray);

d.fill(road);

}

public static void bush()

{

Oval bush = new Oval(480,608,80,56);

d.setForeground(Color.green);

d.fill(bush);

}

public static void sun(int i)

{

int XCentre = 800 - i/5, YCentre = 0, Radius = 160;

Circle sun = new Circle(XCentre,YCentre,Radius);

d.setForeground(Color.yellow);

d.fill(sun);

int XCentre2 = 800 - i/5, YCentre2 = 0, Radius2 = 120;

Circle innerSun = new Circle(XCentre2,YCentre2,Radius2);

d.setForeground(Color.orange);

d.fill(innerSun);

int XCentre3 = 800 - i/5, YCentre3 = 0, Radius3 = 80;

Circle centreSun = new Circle(XCentre3,YCentre3,Radius3);

d.setForeground(Color.red);

d.fill(centreSun);

}

public static void car(int i)

{

int Start = 90, Angle = 180;

Rect arcRect = new Rect(i+48,528,192,112);

Arc boot = new Arc(arcRect,Start,Angle);

d.setForeground(new Color(97,25,215));

d.fill(boot);

int Start2 = 90, Angle2 = 90;

Rect arcRect2 = new Rect(i+56,536,192,112);

Arc bootWindow = new Arc(arcRect2,Start2,Angle2);

d.setForeground(new Color(193,255,255));

d.fill(bootWindow);

Rect backBumper = new Rect(i+48,584,96,48);

d.setForeground(new Color(97,25,215));

d.fill(backBumper);

int XCentre = i+240, YCentre = 580, Radius = 52;

Circle frontBody = new Circle(XCentre,YCentre,Radius);

d.setForeground(new Color(97,25,215));

d.fill(frontBody);

int XCentre1 = i+240, YCentre1 = 580, Radius1 = 45;

Circle windscreen = new Circle(XCentre1,YCentre1,Radius1);

d.setForeground(new Color(193,255,255));

d.fill(windscreen);

Rect body = new Rect(i+144,584,176,48);

d.setForeground(new Color(97,25,215));

d.fill(body);

int XCentre2 = i+120, YCentre2 = 632, Radius2 = 24;

Circle tyreLeft = new Circle(XCentre2,YCentre2,Radius2);

d.setForeground(Color.black);

d.fill(tyreLeft);

int XCentre3 = i+120, YCentre3 = 632, Radius3 = 12;

Circle hubcapLeft = new Circle(XCentre3,YCentre3,Radius3);

d.setForeground(Color.lightGray);

d.fill(hubcapLeft);

int XCentre4 = i+264, YCentre4 = 632, Radius4 = 24;

Circle tyreRight = new Circle(XCentre4,YCentre4,Radius4);

d.setForeground(Color.black);

d.fill(tyreRight);

int XCentre5 = i+264, YCentre5 = 632, Radius5 = 12;

Circle hubcapRight = new Circle(XCentre5,YCentre5,Radius5);

d.setForeground(Color.lightGray);

d.fill(hubcapRight);

int XCentre6 = i+320, YCentre6 = 608, Radius6 = 24;

Circle frontBumper = new Circle(XCentre6,YCentre6,Radius6);

d.setForeground(new Color(97,25,215));

d.fill(frontBumper);

Rect doors = new Rect(i+144,528,96,56);

d.setForeground(new Color(97,25,215));

d.fill(doors);

Rect rearWindow = new Rect(i+152,536,36,48);

d.setForeground(new Color(193,255,255));

d.fill(rearWindow);

Rect frontWindow = new Rect(i+196,536,36,48);

d.setForeground(new Color(193,255,255));

d.fill(frontWindow);

}

}

It makes a car drive across the screen and the sun go across the sky. What i need is to stop the flickering between frames. I know that i can use dWin.hold() and dWin.release() but im not sure exactly how to code it?

[4480 byte] By [fwabblya] at [2007-10-2 6:14:15]
# 1
Use double buffering
tjacobs01a at 2007-7-16 13:15:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
What would that be? Im new to this java stuff.
fwabblya at 2007-7-16 13:15:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3
Double buffering is where, instead of drawing onscreen whenever you update your screen, you draw offscreen to an image. Then when you update the screen you simply draw that 'offscreen' image. ( see this example http://javaboutique.internet.com/tutorials/Step/Chapter4/DoubleBuffering.html
sierratecha at 2007-7-16 13:15:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 4
hi,if u draw this in a panel then you can easily avoid flicking. because in panel the double buffering is true(by default). u need to add this panel to the frame.
mamun05a at 2007-7-16 13:15:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...