01: package gnu.kawa.models;
02:
03: import java.awt.*;
04: import java.awt.geom.*;
05:
06: public class WithComposite implements Paintable {
07: Paintable[] paintable;
08: Composite[] composite;
09:
10: public static WithComposite make(Paintable paintable,
11: Composite composite) {
12: WithComposite comp = new WithComposite();
13: comp.paintable = new Paintable[] { paintable };
14: comp.composite = new Composite[] { composite };
15: return comp;
16: }
17:
18: public static WithComposite make(Paintable[] paintable,
19: Composite[] composite) {
20: WithComposite comp = new WithComposite();
21: comp.paintable = paintable;
22: comp.composite = composite;
23: return comp;
24: }
25:
26: public static WithComposite make(Object[] arguments) {
27: int n = 0;
28: for (int i = arguments.length; --i >= 0;) {
29: if (arguments[i] instanceof Paintable)
30: n++;
31: }
32: Paintable[] paintable = new Paintable[n];
33: Composite[] composite = new Composite[n];
34: Composite comp = null;
35: int j = 0;
36: for (int i = 0; i < arguments.length; i++) {
37: Object arg = arguments[i];
38: if (arg instanceof Paintable) {
39: paintable[j] = (Paintable) arguments[i];
40: composite[j] = comp;
41: j++;
42: } else {
43: comp = (Composite) arg;
44: }
45: }
46: return make(paintable, composite);
47:
48: }
49:
50: public void paint(Graphics2D graphics) {
51: Composite saved = graphics.getComposite();
52: Composite prev = saved;
53: try {
54: int n = paintable.length;
55: for (int i = 0; i < n; i++) {
56: Composite cur = composite[i];
57: if (cur != null && cur != prev) {
58: graphics.setComposite(cur);
59: prev = cur;
60: }
61: paintable[i].paint(graphics);
62: }
63: } finally {
64: if (prev != saved)
65: graphics.setComposite(saved);
66: }
67: }
68:
69: public Rectangle2D getBounds2D() {
70: int n = paintable.length;
71: if (n == 0)
72: return null; // ???
73: Rectangle2D bounds = paintable[0].getBounds2D();
74: for (int i = 1; i < n; i++)
75: bounds = bounds.createUnion(paintable[i].getBounds2D());
76: return bounds;
77: }
78:
79: public Paintable transform(AffineTransform tr) {
80: int n = paintable.length;
81: Paintable[] transformed = new Paintable[n];
82: for (int i = 0; i < n; i++)
83: transformed[i] = paintable[i].transform(tr);
84: return WithComposite.make(transformed, composite);
85: }
86: }
|