1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.awt.*;
7    import java.util.*;
8    import java.awt.geom.*;
9    import java.awt.image.*;
10   
11   public class Fill implements java.io.Serializable{
12   
13     private static final long serialVersionUID = Version.getSUID();
14   
15     Symbol sym;
16     transient Paint paint = null;
17     public boolean isVector = false;
18     public static boolean allVector = false;
19     public static boolean overideVector = true;
20   
21   //  public Size period = null;
22   //  public Size scale = null;
23   
24     public Fill(Symbol sym) {
25       this.sym=sym;
26     }
27   
28     public Paint getPaint(View view, Shape shape) {
29   //    System.out.println("vect = "+allVector+" over = "+overideVector);
30       if (((!isVector  && ! overideVector) ||
31            (!allVector &&   overideVector)) &&
32           (paint!=null)) {
33   //      System.out.println("Using old");
34         return(paint);
35       }
36   
37       Paint background = Color.white;
38       Vector items = new Vector();
39       Symbol box = null;
40   
41       if (sym instanceof Comp) {
42         int memcnt = ((Comp)sym).members.size();
43         if (memcnt==0) return(null);
44         box = (Symbol)((Comp)sym).members.elementAt(0);
45         if (memcnt==1) items.add(((Comp)sym).members.elementAt(0));
46         else {
47           if ((box instanceof Curve) && ((Curve)box).paint!=null)
48             background = ((Curve)box).paint;
49           for (int i=1;i<memcnt;i++)
50             items.add(((Comp)sym).members.elementAt(i));
51         }
52       } else {
53         box = sym;
54         items.add(sym);
55       }
56   
57       if ((!isVector  && ! overideVector) ||
58            (!allVector &&   overideVector)) {
59         Rectangle2D cell = box.getBounds(new AffineTransform());
60   
61   //      System.out.println("making paint");
62   
63         BufferedImage image = new BufferedImage((int)cell.getWidth(),(int)cell.getHeight(),
64           				      BufferedImage.TYPE_INT_RGB);
65         View newview = new View();
66         newview.trans.translate(-cell.getX(),-cell.getY()-cell.getHeight());
67         newview.draw = image.createGraphics();
68         newview.draw.setPaint(background);
69   
70         cell.setRect(0,0,cell.getWidth(),cell.getHeight());
71         newview.draw.fill(cell);
72         newview.draw.setPaint(Color.black);
73   
74         for (Iterator it = items.iterator();it.hasNext();)
75           ((Symbol)it.next()).paint(newview);
76   
77         paint = new TexturePaint(image,cell);
78         return(paint);
79       }
80   
81       Shape clip = view.getClip();
82       view.clip(shape);
83       Rectangle2D zone = view.getClipBounds();
84       if (background!=null) {
85         Paint pa = view.draw.getPaint();
86         view.draw.setPaint(background);
87         view.draw.fill(zone);
88         view.draw.setPaint(pa);
89       }
90   //    Rectangle2D zone = shape.getBounds();
91       Rectangle2D cell = box.getBounds(view.trans);
92       ViewTransform subzone = new ViewTransform(ViewTransform.POSTMULTIPLY);
93       double x0 = zone.getX() - cell.getX();
94       double y0 = zone.getY() - cell.getY();
95   //    System.out.println("drawing vects");// zone ="+zone+" cell = "+cell+" base = "+base);
96       for (double x=x0; x<x0+zone.getWidth(); x += cell.getWidth()) {
97         for (double y=y0; y<y0+zone.getHeight(); y += cell.getHeight()) {
98           subzone.setTransform(1,0,0,1,x,y);
99           view.push(subzone);
100          for (Iterator it = items.iterator();it.hasNext();)
101            ((Symbol)it.next()).paint(view);
102          view.pop();
103        }
104      }
105      view.setClip(clip);
106      paint = null;
107      return(null);
108    }
109  
110  }
111