From: Subject: Graphics notes 2 Date: Thu, 12 May 2005 08:44:44 -0500 MIME-Version: 1.0 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Location: http://scitech.stisd.net/users/lois.kertesz/unit9/Morenotes.htm X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 =EF=BB=BF Graphics notes 2

Special methods for = Applets

delaying the action

public void delay(int p) =
{
  =20 = try
   {Thread.sleep(p);}
   catch=20 (InterruptedException e){}
}=20

And a call line = like this=20 =          delay(30);

Double Buffering

// add=20 these before init method

private=20 Image bufferImage;
private Graphics bufferG;=20

//Do not change anything in this method - = add before=20 the paint method

public void update (Graphics g) =
{=20
 // initialize buffer
 if (bufferImage = =3D=3D null)=20
  {
   bufferImage =3D = createImage=20 (this.getSize().width, this.getSize().height);=20
   bufferG =3D bufferImage.getGraphics = ();=20
   }
// clear screen in background=20
  bufferG.setColor (getBackground ());=20
  bufferG.fillRect (0, 0, = this.getSize().width,=20 this.getSize().height);
// draw = elements in=20 background
  bufferG.setColor = (getForeground());=20
  paint = (bufferG);
// draw image = on the screen=20
   g.drawImage (bufferImage, 0, 0, this); =
}=20
 =20

No call line is=20 necessary

Wrapping code

//this = goes at the=20 top of the applet- before all methods
int k=3D0;  // a=20 global variable that can be used in any=20 method

//this goes=20 inside the paint method after you draw the=20 = object
k+=3D5;        =20 //increase k for the next = position
if(=20 k > 300//the blue numbers may=20 = change

       &nb= sp;      =20 k =3D 0;

 

Bouncing code

//this goes at the top of the applet- before all=20 methods
  int m =3D=20 0;   // m is the position = variable=20
  int d =3D 0;  //d =3D 0 means=20 we go to the right(or=20 = down)
         &nb= sp;   =20 //d =3D 1 means we go to the left (or=20 = up)
          = ;    //starting value is direction it = starts
=20
// = goes inside=20 paint
  if(d =3D=3D 0)   //if=20 i am going to the right
  = { 
   m=20 +=3D 5;     //increase to=20 continue to the right
   if(m > 280) //change = blue number=20 to correct = number
       d =3D=20 1;    // change=20 direction
  }
  = else 
  =20 = {           =20 // d must be 1 to get=20 here
     m=20 -=3D5;      //decrease to=20 go left
    if(m < 0
)   // change=20 blue number to correct=20 number
      d =3D=20 0;    // change=20 directions
  =20 }