High performance java swing animation
Download
1 / 21

high performance java swing animation - PowerPoint PPT Presentation


  • 412 Views
  • Updated On :

David Wallace Croft 2004-05-22 Presented to the Plano Java Users Group Plano, TX Copyright 2004 David Wallace Croft. This work is licensed under the Creative Commons Attribution License. High Performance Java Swing Animation. David Wallace Croft.

Related searches for high performance java swing animation

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 'high performance java swing animation' - benjamin


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
High performance java swing animation l.jpg

David Wallace Croft

2004-05-22

Presented to the

Plano Java Users Group

Plano, TX

Copyright 2004 David Wallace Croft.

This work is licensed under the Creative Commons Attribution License.

High Performance Java Swing Animation


David wallace croft l.jpg
David Wallace Croft

  • Founder and VP, Game Developers Java Users Group (www.GameJUG.org)

  • Taught Java game programming at the Institute of Interactive Arts & Engineering at UTD

  • Author of "Advanced Java Game Programming"(www.CroftSoft.com/library/books/ajgp/)

  • Software Developer, NIST ATP "Peer-to-Peer Virtual Reality Learning Environments" research grant project, Whoola Inc.


Open source l.jpg
Open Source

  • All code presented today is Open Source

  • Code documented in detail in the book"Advanced Java Game Programming"

  • Source code available for download from www.CroftSoft.com/library/books/ajgp/

  • Public Domain multimedia files also available

  • This presentation archived on CroftSoft website


Performance issues l.jpg
Performance Issues

  • Clock resolution

  • Hardware acceleration

  • Repaint algorithm


Clock resolution l.jpg
Clock Resolution

  • Need at least 24 fps for smooth animation

  • Period of less than 42 ms

  • System.currentTimeMillis()

  • Clock resolution 50 to 60 ms on some Windows

  • Swing Timer class limited to 20 fps

  • Possibly due to clock resolution?

  • Thread.sleep() within loop finer control


Animation loop l.jpg
Animation Loop

  • Phase 1: update sprite positions

  • Phase 2: repaint sprites at new positions

  • Phase 3: delay


Loopgovernor l.jpg
LoopGovernor

  • Slows animation loop down to desired frame rate

  • Too slow = jerky animation

  • Too fast = faster than monitor refresh, 100% CPU

    Do not squander time, for that is the stuff life is made of. -- Benjamin Franklin

  • FixedDelayLoopGovernor

  • SamplerLoopGovernor

  • WindowedLoopGovernor


Hardware acceleration l.jpg
Hardware Acceleration

  • Hardware acceleration for images

  • Added to Java in version 1.4

  • Big difference in performance


Definitions l.jpg
Definitions

  • VRAM = Video Random Access Memory

  • Blit = BLT = Block Line Transfer

  • Buffer = block of memory for image data

  • Pixel = picture element

  • Render = calculate and store pixel data

  • Primary surface = screen surface

  • Back buffer = offscreen image


Class image l.jpg
Class Image

  • Package java.awt

  • Holds pixel (picture element) data

  • Abstract superclass

  • Subclasses

    • BufferedImage

    • VolatileImage


Bufferedimage l.jpg
BufferedImage

  • Quick and easy to modify pixel data

  • Pixel data not in video memory

  • Slow to transfer to video memory

  • Blitting

  • No hardware acceleration for drawing ops


Volatileimage l.jpg
VolatileImage

  • Component.createVolatileImage(width,height)

  • Pixel data in video memory (VRAM)

  • Hardware acceleration for drawing ops

  • Video memory is volatile

  • Screen saver will invalidate

  • Must check and reload

  • boolean VolatileImage.contentsLost()


Compatibleimage l.jpg
CompatibleImage

  • GraphicsConfiguration.createCompatibleImage()

  • Pixel format conversion not needed

  • BufferedImage for pixel manipulation

  • VolatileImage cache for fast blitting

  • Caching mechanism validates volatile memory

  • Best for static images since cached

  • Caching algorithm out of your control


Transparency l.jpg
Transparency

  • GraphicsConfiguration.createCompatibleImage (

    width, height, transparency )

  • Transparency.OPAQUE

  • Transparency.BITMASK

  • Transparency.TRANSLUCENT

  • TRANSLUCENT not accelerated so slow

  • Transparency lost when copying to VolatileImage


Translucent demo l.jpg
Translucent Demo

  • SpriteDemo fog option

  • Performance drops


Scaled blt l.jpg
Scaled BLT

  • Graphics.drawImage ( x, y, width, height, null )

  • Slow

  • Solution = pre-scale


Loadautomaticimage l.jpg
loadAutomaticImage()

  • com.croftsoft.core.awt.image.ImageLib

  • Source code review


Repaint algorithm l.jpg
Repaint Algorithm

  • Default algorithm in Swing bad for animation

  • Creates union of dirty rectangles

  • Slows down when rectangles far apart

  • Demonstration using Sprite


Repaintcollector l.jpg
RepaintCollector

  • Phase 1: Collects repaint requests

  • Phase 2: Delivers repaint requests

  • Coalesces requests for efficiency

  • Default algorithm in Swing bad for animation

  • SimpleRepaintCollector

  • BooleanRepaintCollector

  • CoalescingRepaintCollector


References l.jpg
References

  • Jeffrey Kesselman, Understanding the AWT Image Types

  • Michael Martak, Full-Screen Exclusive Mode API

  • Sun, High Performance Graphics

  • Sun, The VolatileImage API User Guide


Questions l.jpg
Questions?

  • Also ask via book discussion mailing list


ad