Reporting on the JavaOneSM Conference session "High-Performance GUIs
with the Java Foundation Classes (JFC) API," Dana Nourie notes that GUI
programs written using the JavaTM programming language have a
reputation for rendering slowly or incorrectly. Nourie says developers
can resolve these problems with coding techniques, however, by proper
use of threads, and the new JavaTM 2 Platform, Standard Edition
(J2SETM) 1.4 acceleration capabilities.
Panelist Steve Wilson, engineering manager at Sun, said "The cause
behind slow rendering applications is not the Java programming language
itself, but instead may include other causes such as programming
techniques not taking advantage of the many benefits of the Java
platform."
Among the remedies that Wilson proposes in his book "JavaTM Platform
Performance Strategies and Tactics" are the following:
- Code with proper building techniques
- Make proper use of threads
- Take advantage of the new EventHandler, RepaintManager and the
VolatileImage classes
The full source includes an example of code that, while it works, does
not run efficiently. The proposed solution saves programming time and
cuts down on both event object creation and runtime. Wilson recommends
performing operations in bulk to reduce the number of events that are
posted and initializing or totally replacing the contents of a model by
constructing a new one instead of reusing the existing one.
The panelists also recommended using the event-dispatching thread
instead of single-threaded event processing to exploit the savings
available from using the paint and actionPerformed methods and the AWT
EventQueue.invokeLater method. Any program that uses separate worker
threads to perform GUI-related processing can use invokeLater and
invokeAndWait methods to cause a Runnable object to be run on the
event-dispatching thread.
Rendering complex shapes and patterns requires iterating through pixels
and calculating values for each of them. Loading an image and
performing a filter operation on it sometimes requires allocating a
large block of memory. The new VolatileImage class in J2SE 1.4 allows
programmers to create a hardware-accelerated offscreen image and manage
the contents of that image. The new hardware acceleration has improved
the performance of Swing applications because double-buffering now uses
VolatileImage.
Applications can also be sped up by the use of methods and new classes.
For example, repainting small regions instead of entire sections or
screens can make a significant difference in the performance of an
application. In addition to using paint methods correctly, putting the
new EventHandler class from J2SE 1.4 to work will provide support for
dynamically generating event listeners that have a small footprint and
can be saved automatically by the persistence scheme. This can improve
start-up as well.
[...read more...]