15 Multithreaded Graphics
15 Multithreaded Graphics
Multithreaded
Graphics
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/java.html
Multithreaded Graphics:
Alternative Approaches
• Redraw everything in paint
– Simple and easy, but if things change quickly it is slow and can
result in a flickering display
• Have routines other than paint directly do drawing
operations
– Easy, efficient, and flicker-free, but results in “transient” drawing
that is lost next time the screen is redrawn
• Override update and have paint do incremental
updating
– Eliminates the flicker and improves efficiency somewhat, but
requires the graphics to be non-overlapping
• Double buffering
– Most efficient option and has no problem with overlapping graphics.
– More complex and requires additional memory resources
6
Redraw Everything in paint
• Idea
– Have user actions change non-graphical data structures,
then call repaint.
– The repaint method sets a flag that tells the event-
handling process to call update.
– The standard update method clears the screen and then
calls paint.
– The paint method completely redraws everything.
• Advantage
– Easy
• Disadvantages
– Flickers, slow.
7
8
Redrawing Everything in paint:
Example (Continued)
/** When you click the mouse, create a SimpleCircle,
* put it in the list of circles, and tell the system
* to repaint (which calls update, which clears
* the screen and calls paint).
*/
10
Redrawing Everything in paint:
Example (Continued)
public class SimpleCircle {
private int x, y, radius;
By storing results in a permanent data structure and redrawing the whole structure every
time paint is invoked, you cause the drawing to persist even after the window is covered
up and reexposed
12
Have Other Routines Draw
Directly on Window
• Idea
– Arbitrary methods (i.e., other than paint) can call
getGraphics to obtain the window’s Graphics object
– Use that Graphics object to draw
– Drawing lost if
• Window covered up and reexposed
• The update method called (e.g., via repaint)
• Advantage
– Fast
• Disadvantage
– Temporary
13
16
Override update and Have
paint do Incremental Updating
• Idea
– Have repaint (which triggers update) avoid clearing the
screen each time by overriding update as follows:
public void update(Graphics g) {
paint(g);
}
– Then, assuming objects don’t overlap, erase each object
at its old location by drawing over it in the background
color then drawing it at the new location
• Advantages
– No flicker, faster
• Disadvantage
17
– Fails for overlapping images
Incremental Updating:
Bounce Applet
public class Bounce extends Applet implements Runnable,
ActionListener{
private ExecutorService taskList;
private volatile boolean running = false;
private ArrayList<MovingCircle> circles;
private int width, height;
private Button startButton, stopButton;
20
Bounce Applet (Continued)
/** Skip the usual screen-clearing step of update so that
* there is no flicker between each drawing step.
*/
/** Erase each circle's old position, move it, then draw it
* in new location.
*/
Incremental Updating:
MovingCircle Class
public class MovingCircle extends SimpleCircle {
private int deltaX, deltaY;
...
public void move(int windowWidth, int windowHeight) {
setX(getX() + getDeltaX());
setY(getY() + getDeltaY());
bounce(windowWidth, windowHeight);
}
25
27
At the expense of memory and some complexity, double buffering allows fast,
28 flicker-free updating of possibly overlapping images
Array-Based Animation
• Idea
– Load a sequence of images into an array
– Start a thread to cycle through the images and draw to the
graphics object
• Each time the thread loops through the while loop, the
array index is incremented and repaint (which triggers
update) is called to update the images on the screen
– Stop the animation by setting a flag
• In an applet, end the animation from the applet’s stop
method
29
Array-Based Animation:
Example
public class ImageAnimation extends Applet {
private static final int NUMDUKES = 2;
private Duke[] dukes; // Duke has array of images
private int i;
...
30
Animation Example (Continued)
public void update(Graphics g) {
paint(g);
}
parent.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break; // Break while loop.
}
}
}
}
33
Animation: Result
34
Summary
• Options for fast-changing graphics
– Redraw everything in paint
– Have routines other than paint directly do drawing
operations
– Override update and have paint do incremental updating
– Double buffering
• Animation can be achieved by cycling through
a sequence of images
– Usually in conjunction with double buffering
42
Questions?
JSF 2, PrimeFaces, Java 7, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training.