SlideServe
Menu
Préférences
© SlideServe · v2026

Animation in java

ozzie avatar
8 diapositives · 205 Views · Jul 17, 2014

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

Animation in java - slide 1
01 / 8Animation in java
1 / 17

Télécharger la présentation

Animation in java
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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