1 / 8

Animation in java

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

ozzie
Télécharger la présentation

Animation in java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Jim Priest, Peter Lundgren, Russell Bennett Animation in java

  2. Background info • A sequence of fames • You have some experience already • Used for some GUI’s and Games

  3. 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. } } }

  4. 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.

  5. 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

  6. Important Concepts • “Flashing” • Overriding the update() method • Off-screen images • “Double buffering”

  7. 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); } }

  8. Questions? Next, Demo

More Related