import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.util.List;
/*
***************************************************************
* Sample program using update to do incremental painting.
*
* Incremental painting is useful if the component needs to
* render incremental output on top of a complex background.
***************************************************************
*/
public class UpdateDemo extends Frame{
private final DrawingArea drawingArea;
public UpdateDemo(){
super("Press key to draw ovals");
drawingArea = new DrawingArea(new ArrayList<Point>());
add(drawingArea, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
WindowListener l = new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
Runnable requestFocus = new Runnable() {
public void run() {
drawingArea.requestFocusInWindow();
}
};
EventQueue.invokeLater(requestFocus);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
@Override
public void run() {
new UpdateDemo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
EventQueue.invokeLater(gui);
}
}
/**
* A canvas which renders a semi-complex background layered
* with shapes in various locations on the canvas
* which are created as the user presses any key.
*
* The paint() method always paints the entire area.
*
* Additionally, this class uses update() to do
* incremental painting of only the vectors which have been
* created since the last time the canvas painted.
*/
class DrawingArea extends Canvas {
private Color colors[] = {Color.red, Color.yellow, Color.blue,
Color.green, Color.pink, Color.orange,
Color.white, Color.magenta, Color.cyan};
private List<Point> points;
private int shapesPainted;
private final Random r;
public DrawingArea(final List points) {
this.points = points;
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
DrawingArea c = (DrawingArea) e.getSource();
c.points.add(getRandomPoint());
c.repaint();
}
});
r = new Random();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
public void paint(final Graphics g) {
// paint entire canvas -> background and all shapes
paintBackground(g);
shapesPainted = 0;
for (int i = 0; i < points.size() - 1; i++) {
paintShape(g, i);
}
}
@Override
public void update(final Graphics g) {
// only paint new shapes since last update call
for (int i = shapesPainted; i < points.size(); i++) {
paintShape(g, i);
}
}
private void paintBackground(final Graphics g) {
Dimension size = getSize();
int thickness = 5;
for (int y = 0; y + thickness <= size.height; y += thickness) {
g.setColor(colors[y % colors.length]);
g.fillRect(0, y, size.width, thickness);
}
}
private void paintShape(final Graphics g, int index) {
Point p1 = points.get(index);
g.setColor(Color.BLUE);
g.fillOval(p1.x, p1.y, 15, 15);
shapesPainted++;
}
private Point getRandomPoint(){
return new Point(r.nextInt(getWidth()), r.nextInt(getHeight()));
}
}