Jim priest peter lundgren russell bennett
Download
1 / 8

Animation in java - PowerPoint PPT Presentation


  • 57 Views
  • Uploaded on

Jim Priest, Peter Lundgren, Russell Bennett. Animation in java. Background info. A sequence of fames You have some experience already Used for some GUI’s and Games. Example. /** * Draws the World and its Balls. * * @ param g the Graphics object onto which to draw. */ @Override

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Animation in java' - ozzie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Background info
Background info

  • A sequence of fames

  • You have some experience already

  • Used for some GUI’s and Games


Example
Example

/**

*DrawstheWorldanditsBalls.

*

*@paramgtheGraphicsobjectontowhichtodraw.

*/

@Override

publicvoid paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D graphics = (Graphics2D) g;

this.drawWorld(graphics);

this.drawBalls(graphics);

}

/**

*Repeatedlyrepaintsthispanel.

*/

publicvoid run() {

while (true) {

try {

Thread.sleep(this.timeToSleep);

this.repaint();

} catch (InterruptedException exception) {

// If you can't sleep, no problem -- just continue.

}

}

}


Important concepts
Important Concepts

  • Using threads to control animation behavior

  • Controlling the speed and smoothness using .sleep()

    • Have a frame rate field.

    • Pass 1000/frameRate to .sleep()

    • Can’t have different frame rates for different animation objects on screen at same time

    • After sleep, calls repaint.


Important concepts1
Important Concepts

  • Using the run method

    • Contains the animation thread

    • Can be used to draw on the Frame

  • Using the paintComponent method

    • Can be used to draw on the frame

    • Still needs a thread to handle updating the frame


Important concepts2
Important Concepts

  • “Flashing”

  • Overriding the update() method

  • Off-screen images

    • “Double buffering”


Example1
Example

classOptimizedDoubleBufferedCanvasextends Canvas {

publicvoid update(Graphics g) {

Graphics offgc;

Image offscreen = null;

Rectangle box = g.getClipRect();

// create the offscreen buffer and associated Graphics

offscreen = createImage(box.width, box.height);

offgc = offscreen.getGraphics();

// clear the exposed area

offgc.setColor(getBackground());

offgc.fillRect(0, 0, box.width, box.height);

offgc.setColor(getForeground());

// do normal redraw

offgc.translate(-box.x, -box.y);

paint(offgc);

// transfer offscreen to window

g.drawImage(offscreen, box.x, box.y, this);

}

}


Questions?

Next, Demo


ad