Lecture 8 Leftover Demos: JPEGEncoder, Animation-demos. Translucency demos. Internationalization, Customization Accessibility History of Animation/Early Cinema Animation how-to Example: Dungeon Dregs Internationalization 2 System properties are typically used to customize program interface: user.language and user.region There is a standardized set of 2-letter ISO codes for these. Languages: en, fr, de, es, sw, etc. Regions: US, FR, CA, CH, DE, GB, etc. Also called Localization I18N How-To 1. Subclass ResourceBundle once for every supported Locale. All localityspecific resources go in here. 2. Load ResourceBundle corresponding to user’s preference. 3. Get all locale-specific Strings and other resources from the bundle. Use wherever appropriate. I18N Demo1 Notes Some redundancy is required and desirable. MyResources, MyResources_en and MyResources_en_US are identical. These provide defaults in case not all system properties are set. Watch for pitfalls: String resources may not be the expected size. Meanings may change in subtle ways. Formats To get accents and special characters, need to know some Unicode. \u#### NumberFormat, DateFormat. Constructing sentences from pieces is not so easy anymore. Languages differ in sentence structure and ordering. Move all sentence-constructing into bundles. Use MessageFormat class. Bundle format strings. Beyond Strings Can customize look and feel for different locales. ResourceBundles can contain filenames, such as image files, and can even contain customized classes such as dialogs or frames. ResourceBundles can inherit from one another. Might have a single resource file with common data for Europe. Customization User may want to change language preference for your program only, or alter other settings from the defaults. Easy to do for one session. Just modify the attributes. Would also like preferences to persist from one session to another. Need to create a preferences file. Preferences files Location Preferred location varies with system. Program directory for single-user PCs, user directory for multi-user systems. Format Serialized objects? Easy. Hard to edit. Custom format. Must deal with syntax. Preferences class Define a preferences class along the same lines as ResourceBundle. Basically a Map or Hashtable of keys and values. Write preference file using Serialize or entry by entry. On startup, load the preferences from the stored file. Defaults if this fails. What to customize A good topic for use case analysis and user surveys. What will the product be used for? What do users ask for? Better to offer users a limited set of ready-made preference sets than to make them choose everything individually. Options to turn “features” off. What not to customize Everything. Want to be able to explain to users how to do any given task, without having to worry about how they customized the application. Menus. Makes sense to have all commands accessible through menus in fixed locations. Customizable toolbars for commonly-used commands. (This isn’t a hard-and-fast rule) Accessibility Swing components have methods to support assistive technologies, which for example may allow vocal control and audio display of a GUI interface. Can play with this: download Monkey. To take advantage, you need to label components with informative text. Making components accessible 1. Label all JComponents using method. This also serves as help for new users. 2. When JLabels are meant to label something, use JLabel.setLabelFor() setToolTipText(String) 3. JComponent.getAccessibleContext().setA ccessible*() Description. where *=Name or History of the Magic Lantern Projected Images have been around in one form or another since at least 1420. By 1700’s, used for popular entertainment. Special effects such as rear-projection onto a translucent screen, projection onto cloud of smoke, dissolving views. 1895: Cinematographe invented. Pre-history of Cinema As early as 1674, idea of quick succession of glass slides. 1832: Fantoscope, Stroboscope. Illusion of motion created by “persistence of vision” 1870’s: combined with photography. 1890’s: celluloid film. Persistence of Vision Images remain on the retina for about 1/14 of a second. Small delay between bright images not visible. Frame rate of 30Hz is generally considered flicker-free. Typical monitor refresh rate is 70130Hz. (Hz = Hertz = * per second). Basic Animation Display image sequence at intervals. Images must line up exactly can detect 1-pixel shift on most monitors Intervals must be same length movement will appear jerky Intervals should be short. probably no benefit beyond 1/30 second User expectations vary with application Corollaries Must ensure adequate computational resources. Small delays lead to jerks. Example: scrolling in IE. Less computationally demanding to animate small images, or simple images such as rectangles filled by solid color. Rendering text especially slow in Java. Simple animation in Swing Use javax.swing.Timer to generate ticks every N milliseconds. On tick, display current frame, then compute next frame. Order matters: computing frame may take variable time; displaying should not. Speed-up tricks: downloads Downloading: slow, and speed varies. Track downloads with a MediaTracker. Start animations after downloads complete. Fewer HTTP connections saves time. Combine images into 1 file; extract with graphics operations. Better still: put all images and source into a jar file, and extract locally. Generate graphics locally if possible. Speed-up tricks: precompute Compute once, draw many times. Avoid Graphics.drawString (very slow) Precompute GlyphVectors or Images for strings which you draw more than once. Same for digits of numeric displays. Use antialias and transparency effects in precomputing. For actual display, just copy in the BufferedImage. More speed tricks Don’t repeat graphics ops wastefully e.g. Triple buffering (next slide). Minimize area to redraw. Small rectangles can be updated faster. Graphics.setRenderHints(…RENDER_SPEED) Ask user not to run other jobs. Case-study: “Dungeon Dregs” About the lamest of many cool games at J*va On The Brain by Karl Hörnell. In public domain. Lots of game applets on the site, together with notes about their creation, including design choices and implementation details. Go browse it! Welcome to the Dungeon Dungeon Graphics All the graphics for this game have been stored in a single .gif file. This is downloaded and stored as an Image. The individual pieces are copied out as needed. This method reduces download time. Triple-Buffering There are 2 buffers, farBuffer and nearBuffer (both class Image). farBuffer contains background elements, and is changed rarely. (e.g. crate smashed or prisoner freed). nearBuffer is redrawn frequently, with all the moving elements. nearBuffer drawn to Panel when done. Triple-Buffering farBuffer: static elements nearBuffer: farBuffer + mobile elements screen: nearBuffer, but drawn in 1 step. Animation Loop Since this is an Applet, it uses Thread.sleep instead of a Timer. Every 85ms, tick occurs. New positions are computed, farBuffer and nearBuffer are updated, and screen is refreshed. Finite state machine is simulated to minimize processing time. Slow tasks are spread over several ticks.