Or... you can just draw the objects you want, in the paint() method. Draw them in order, from back to front. You can create a simple framework for this... something like:
PHP Code:
public abstract class Component {
private int x, y, w, h;
protected abstract void paint(Graphics g);
void parentPaint(Graphics g) {
// record current state
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
int tx = g.getTranslateX();
int ty = g.getTranslateY();
// set up for component paint
g.translate(x, y);
g.setClip(0, 0, w, h);
paint(g);
// put everything back how it was
g.translate(tx - g.getTranslateX(), ty - g.getTranslateY());
g.setClip(cx, cy, cw, ch);
}
}
PHP Code:
public class Container extends Canvas {
private Vector components = new Vector();
private int backgroundColor;
public void paint(Graphics g) {
// paint background
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
// paint components
synchronized (components) {
int size = components.size();
for (int i = 0; i < size; i++) {
Component c = (Component) components.elementAt(i);
c.parentPaint(g);
}
}
}
}