1 18 package org.apache.batik.gvt; 19 20 import java.awt.Paint ; 21 import java.awt.PaintContext ; 22 import java.awt.Rectangle ; 23 import java.awt.RenderingHints ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.image.ColorModel ; 27 import java.awt.image.Raster ; 28 29 import org.apache.batik.ext.awt.image.PadMode; 30 import org.apache.batik.ext.awt.image.renderable.Filter; 31 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 32 33 40 public class PatternPaint implements Paint { 41 42 46 private GraphicsNode node; 47 48 51 private Rectangle2D patternRegion; 52 53 58 private AffineTransform patternTransform; 59 60 64 private Filter tile; 65 66 70 private boolean overflow; 71 72 private PatternPaintContext lastContext; 73 74 84 public PatternPaint(GraphicsNode node, 85 Rectangle2D patternRegion, 86 boolean overflow, 87 AffineTransform patternTransform){ 88 89 if (node == null) { 90 throw new IllegalArgumentException (); 91 } 92 93 if (patternRegion == null) { 94 throw new IllegalArgumentException (); 95 } 96 97 this.node = node; 98 this.patternRegion = patternRegion; 99 this.overflow = overflow; 100 this.patternTransform = patternTransform; 101 102 CompositeGraphicsNode comp = new CompositeGraphicsNode(); 106 comp.getChildren().add(node); 107 Filter gnr = comp.getGraphicsNodeRable(true); 108 109 Rectangle2D padBounds = (Rectangle2D )patternRegion.clone(); 110 111 if (overflow) { 114 Rectangle2D nodeBounds = comp.getBounds(); 115 padBounds.add(nodeBounds); 118 } 119 120 tile = new PadRable8Bit(gnr, padBounds, PadMode.ZERO_PAD); 123 } 124 125 128 public GraphicsNode getGraphicsNode(){ 129 return node; 130 } 131 132 135 public Rectangle2D getPatternRect(){ 136 return (Rectangle2D )patternRegion.clone(); 137 } 138 139 142 public AffineTransform getPatternTransform(){ 143 return patternTransform; 144 } 145 146 149 public PaintContext createContext(ColorModel cm, 150 Rectangle deviceBounds, 151 Rectangle2D userBounds, 152 AffineTransform xform, 153 RenderingHints hints) { 154 if (patternTransform != null) { 156 xform = new AffineTransform (xform); 157 xform.concatenate(patternTransform); 158 } 159 160 if ((lastContext!= null) && 161 lastContext.getColorModel().equals(cm)) { 162 163 double p[] = new double[6]; 164 double q[] = new double[6]; 165 xform.getMatrix(p); 166 lastContext.getUsr2Dev().getMatrix(q); 167 if ((p[0] == q[0]) && (p[1] == q[1]) && 168 (p[2] == q[2]) && (p[3] == q[3])) { 169 if ((p[4] == q[4]) && (p[5] == q[5])) 170 return lastContext; 171 else 172 return new PatternPaintContextWrapper 173 (lastContext, 174 (int)(q[4]-p[4]+0.5), 175 (int)(q[5]-p[5]+0.5)); 176 } 177 } 178 182 lastContext = new PatternPaintContext(cm, xform, 183 hints, tile, 184 patternRegion, 185 overflow); 186 return lastContext; 187 } 188 189 192 public int getTransparency(){ 193 return TRANSLUCENT; 194 } 195 196 static class PatternPaintContextWrapper implements PaintContext { 197 PatternPaintContext ppc; 198 int xShift, yShift; 199 PatternPaintContextWrapper(PatternPaintContext ppc, 200 int xShift, int yShift) { 201 this.ppc = ppc; 202 this.xShift = xShift; 203 this.yShift = yShift; 204 } 205 206 public void dispose(){ } 207 208 public ColorModel getColorModel(){ 209 return ppc.getColorModel(); 210 } 211 public Raster getRaster(int x, int y, int width, int height){ 212 return ppc.getRaster(x+xShift, y+yShift, width, height); 213 } 214 } 215 } 216 | Popular Tags |