KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > models > WithComposite


1 package gnu.kawa.models;
2 import java.awt.*;
3 import java.awt.geom.*;
4
5 public class WithComposite implements Paintable
6 {
7   Paintable[] paintable;
8   Composite[] composite;
9
10   public static WithComposite make(Paintable paintable, Composite composite)
11   {
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   {
21     WithComposite comp = new WithComposite();
22     comp.paintable = paintable;
23     comp.composite = composite;
24     return comp;
25   }
26
27   public static WithComposite make(Object JavaDoc[] arguments)
28   {
29     int n = 0;
30     for (int i = arguments.length; --i >= 0; )
31       {
32     if (arguments[i] instanceof Paintable)
33       n++;
34       }
35     Paintable[] paintable = new Paintable[n];
36     Composite[] composite = new Composite[n];
37     Composite comp = null;
38     int j = 0;
39     for (int i = 0; i < arguments.length; i++)
40       {
41     Object JavaDoc arg = arguments[i];
42     if (arg instanceof Paintable)
43       {
44         paintable[j] = (Paintable) arguments[i];
45         composite[j] = comp;
46         j++;
47       }
48     else
49       {
50         comp = (Composite) arg;
51       }
52       }
53     return make(paintable, composite);
54       
55   }
56
57   public void paint (Graphics2D graphics)
58   {
59     Composite saved = graphics.getComposite();
60     Composite prev = saved;
61     try
62       {
63     int n = paintable.length;
64     for (int i = 0; i < n; i++)
65       {
66         Composite cur = composite[i];
67         if (cur != null && cur != prev)
68           {
69         graphics.setComposite(cur);
70         prev = cur;
71           }
72         paintable[i].paint(graphics);
73       }
74       }
75     finally
76       {
77     if (prev != saved)
78       graphics.setComposite(saved);
79       }
80   }
81
82   public Rectangle2D getBounds2D()
83   {
84     int n = paintable.length;
85     if (n == 0)
86       return null; // ???
87
Rectangle2D bounds = paintable[0].getBounds2D();
88     for (int i = 1; i < n; i++)
89       bounds = bounds.createUnion(paintable[i].getBounds2D());
90     return bounds;
91   }
92
93   public Paintable transform (AffineTransform tr)
94   {
95     int n = paintable.length;
96     Paintable[] transformed = new Paintable[n];
97     for (int i = 0; i < n; i++)
98       transformed[i] = paintable[i].transform(tr);
99     return WithComposite.make(transformed, composite);
100   }
101 }
102
Popular Tags