1 / 21

High Performance Java Swing Animation

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.

benjamin
Download Presentation

High Performance Java Swing Animation

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

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

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

  4. Performance Issues • Clock resolution • Hardware acceleration • Repaint algorithm

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

  6. Animation Loop • Phase 1: update sprite positions • Phase 2: repaint sprites at new positions • Phase 3: delay

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

  8. Hardware Acceleration • Hardware acceleration for images • Added to Java in version 1.4 • Big difference in performance

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

  10. Class Image • Package java.awt • Holds pixel (picture element) data • Abstract superclass • Subclasses • BufferedImage • VolatileImage

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

  12. 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()

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

  14. Transparency • GraphicsConfiguration.createCompatibleImage ( width, height, transparency ) • Transparency.OPAQUE • Transparency.BITMASK • Transparency.TRANSLUCENT • TRANSLUCENT not accelerated so slow • Transparency lost when copying to VolatileImage

  15. Translucent Demo • SpriteDemo fog option • Performance drops

  16. Scaled BLT • Graphics.drawImage ( x, y, width, height, null ) • Slow • Solution = pre-scale

  17. loadAutomaticImage() • com.croftsoft.core.awt.image.ImageLib • Source code review

  18. Repaint Algorithm • Default algorithm in Swing bad for animation • Creates union of dirty rectangles • Slows down when rectangles far apart • Demonstration using Sprite

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

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

  21. Questions? • Also ask via book discussion mailing list

More Related