high performance java swing animation
Download
Skip this Video
Download Presentation
High Performance Java Swing Animation

Loading in 2 Seconds...

play fullscreen
1 / 21

high performance java swing animation - PowerPoint PPT Presentation


  • 416 Views
  • Uploaded 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.

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

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
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
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
Performance Issues
  • Clock resolution
  • Hardware acceleration
  • Repaint algorithm
clock resolution
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
Animation Loop
  • Phase 1: update sprite positions
  • Phase 2: repaint sprites at new positions
  • Phase 3: delay
loopgovernor
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
Hardware Acceleration
  • Hardware acceleration for images
  • Added to Java in version 1.4
  • Big difference in performance
definitions
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
Class Image
  • Package java.awt
  • Holds pixel (picture element) data
  • Abstract superclass
  • Subclasses
    • BufferedImage
    • VolatileImage
bufferedimage
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
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
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
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
Translucent Demo
  • SpriteDemo fog option
  • Performance drops
scaled blt
Scaled BLT
  • Graphics.drawImage ( x, y, width, height, null )
  • Slow
  • Solution = pre-scale
loadautomaticimage
loadAutomaticImage()
  • com.croftsoft.core.awt.image.ImageLib
  • Source code review
repaint algorithm
Repaint Algorithm
  • Default algorithm in Swing bad for animation
  • Creates union of dirty rectangles
  • Slows down when rectangles far apart
  • Demonstration using Sprite
repaintcollector
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
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
Questions?
  • Also ask via book discussion mailing list
ad