1 18 package org.apache.batik.ext.awt.g2d; 19 20 import java.awt.AlphaComposite ; 21 import java.awt.BasicStroke ; 22 import java.awt.Color ; 23 import java.awt.Composite ; 24 import java.awt.Font ; 25 import java.awt.Paint ; 26 import java.awt.Rectangle ; 27 import java.awt.RenderingHints ; 28 import java.awt.Shape ; 29 import java.awt.Stroke ; 30 import java.awt.font.FontRenderContext ; 31 import java.awt.geom.AffineTransform ; 32 import java.awt.geom.Area ; 33 import java.awt.geom.GeneralPath ; 34 import java.awt.geom.NoninvertibleTransformException ; 35 import java.util.Map ; 36 import java.util.Vector ; 37 38 52 public class GraphicContext implements Cloneable { 53 56 protected AffineTransform defaultTransform = new AffineTransform (); 57 58 64 protected AffineTransform transform = new AffineTransform (); 65 66 69 protected Vector transformStack = new Vector (); 70 71 80 protected boolean transformStackValid = true; 81 82 85 protected Paint paint = Color.black; 86 87 90 protected Stroke stroke = new BasicStroke (); 91 92 95 protected Composite composite = AlphaComposite.SrcOver; 96 97 100 protected Shape clip = null; 101 102 105 protected RenderingHints hints = new RenderingHints (null); 106 107 110 protected Font font = new Font ("sanserif", Font.PLAIN, 12); 111 112 115 protected Color background = new Color (0, 0, 0, 0); 116 117 120 protected Color foreground = Color.black; 121 122 125 public GraphicContext() { 126 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT); 128 } 129 130 134 public GraphicContext(AffineTransform defaultDeviceTransform) { 135 this(); 136 defaultTransform = new AffineTransform (defaultDeviceTransform); 137 transform = new AffineTransform (defaultTransform); 138 if (!defaultTransform.isIdentity()) 139 transformStack.addElement(TransformStackElement.createGeneralTransformElement(defaultTransform)); 140 } 141 142 145 public Object clone(){ 146 GraphicContext copyGc = new GraphicContext(defaultTransform); 147 148 152 154 155 copyGc.transform = new AffineTransform (this.transform); 157 158 copyGc.transformStack = new Vector (); 160 for(int i=0; i<this.transformStack.size(); i++){ 161 TransformStackElement stackElement = 162 (TransformStackElement)this.transformStack.elementAt(i); 163 copyGc.transformStack.addElement(stackElement.clone()); 164 } 165 166 copyGc.transformStackValid = this.transformStackValid; 168 169 copyGc.paint = this.paint; 171 172 copyGc.stroke = this.stroke; 174 175 copyGc.composite = this.composite; 177 178 if(clip != null) 180 copyGc.clip = new GeneralPath (clip); 181 else 182 copyGc.clip = null; 183 184 copyGc.hints = (RenderingHints )this.hints.clone(); 186 187 copyGc.font = this.font; 189 190 copyGc.background = this.background; 192 copyGc.foreground = this.foreground; 193 194 return copyGc; 195 } 196 197 203 public Color getColor(){ 204 return foreground; 205 } 206 207 215 public void setColor(Color c){ 216 if(c == null) 217 return; 218 219 if(paint != c) 220 setPaint(c); 221 } 222 223 229 public Font getFont(){ 230 return font; 231 } 232 233 240 public void setFont(Font font){ 241 if(font != null) 242 this.font = font; 243 } 244 245 262 public Rectangle getClipBounds(){ 263 Shape c = getClip(); 264 if(c==null) 265 return null; 266 else 267 return c.getBounds(); 268 } 269 270 271 290 public void clipRect(int x, int y, int width, int height){ 291 clip(new Rectangle (x, y, width, height)); 292 } 293 294 295 309 public void setClip(int x, int y, int width, int height){ 310 setClip(new Rectangle (x, y, width, height)); 311 } 312 313 314 330 public Shape getClip(){ 331 try{ 332 return transform.createInverse().createTransformedShape(clip); 333 }catch(NoninvertibleTransformException e){ 334 return null; 335 } 336 } 337 338 339 355 public void setClip(Shape clip) { 356 if (clip != null) 357 this.clip = transform.createTransformedShape(clip); 358 else 359 this.clip = null; 360 } 361 362 386 public void setComposite(Composite comp){ 387 this.composite = comp; 388 } 389 390 391 403 public void setPaint( Paint paint ){ 404 if(paint == null) 405 return; 406 407 this.paint = paint; 408 if(paint instanceof Color ) 409 foreground = (Color )paint; 410 } 411 412 413 419 public void setStroke(Stroke s){ 420 stroke = s; 421 } 422 423 434 public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue){ 435 hints.put(hintKey, hintValue); 436 } 437 438 439 451 public Object getRenderingHint(RenderingHints.Key hintKey){ 452 return hints.get(hintKey); 453 } 454 455 456 469 public void setRenderingHints(Map hints){ 470 this.hints = new RenderingHints (hints); 471 } 472 473 474 488 public void addRenderingHints(Map hints){ 489 this.hints.putAll(hints); 490 } 491 492 493 505 public RenderingHints getRenderingHints(){ 506 return hints; 507 } 508 509 520 public void translate(int x, int y){ 521 if(x!=0 || y!=0){ 522 transform.translate(x, y); 523 transformStack.addElement(TransformStackElement.createTranslateElement(x, y)); 524 } 525 } 526 527 528 544 public void translate(double tx, double ty){ 545 transform.translate(tx, ty); 546 transformStack.addElement(TransformStackElement.createTranslateElement(tx, ty)); 547 } 548 549 565 public void rotate(double theta){ 566 transform.rotate(theta); 567 transformStack.addElement(TransformStackElement.createRotateElement(theta)); 568 } 569 570 589 public void rotate(double theta, double x, double y){ 590 transform.rotate(theta, x, y); 591 transformStack.addElement(TransformStackElement.createTranslateElement(x, y)); 592 transformStack.addElement(TransformStackElement.createRotateElement(theta)); 593 transformStack.addElement(TransformStackElement.createTranslateElement(-x, -y)); 594 } 595 596 615 public void scale(double sx, double sy){ 616 transform.scale(sx, sy); 617 transformStack.addElement(TransformStackElement.createScaleElement(sx, sy)); 618 } 619 620 638 public void shear(double shx, double shy){ 639 transform.shear(shx, shy); 640 transformStack.addElement(TransformStackElement.createShearElement(shx, shy)); 641 } 642 643 660 public void transform(AffineTransform Tx){ 661 transform.concatenate(Tx); 662 transformStack.addElement(TransformStackElement.createGeneralTransformElement(Tx)); 663 } 664 665 673 public void setTransform(AffineTransform Tx){ 674 transform = new AffineTransform (Tx); 675 invalidateTransformStack(); 676 if(!Tx.isIdentity()) 677 transformStack.addElement(TransformStackElement.createGeneralTransformElement(Tx)); 678 } 679 680 686 public void validateTransformStack(){ 687 transformStackValid = true; 688 } 689 690 693 public boolean isTransformStackValid(){ 694 return transformStackValid; 695 } 696 697 701 public TransformStackElement[] getTransformStack(){ 702 TransformStackElement stack[] = new TransformStackElement[transformStack.size()]; 703 transformStack.copyInto(stack); 704 return stack; 705 } 706 707 713 protected void invalidateTransformStack(){ 714 transformStack.removeAllElements(); 715 transformStackValid = false; 716 } 717 718 726 public AffineTransform getTransform(){ 727 return new AffineTransform (transform); 728 } 729 730 738 public Paint getPaint(){ 739 return paint; 740 } 741 742 743 750 public Composite getComposite(){ 751 return composite; 752 } 753 754 770 public void setBackground(Color color){ 771 if(color == null) 772 return; 773 774 background = color; 775 } 776 777 778 784 public Color getBackground(){ 785 return background; 786 } 787 788 795 public Stroke getStroke(){ 796 return stroke; 797 } 798 799 800 819 public void clip(Shape s){ 820 if (s != null) 821 s = transform.createTransformedShape(s); 822 823 if (clip != null) { 824 Area newClip = new Area (clip); 825 newClip.intersect(new Area (s)); 826 clip = new GeneralPath (newClip); 827 } else { 828 clip = s; 829 } 830 } 831 832 853 public FontRenderContext getFontRenderContext(){ 854 Object antialiasingHint = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING); 858 boolean isAntialiased = true; 859 if(antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON && 860 antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT){ 861 862 if(antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF){ 865 antialiasingHint = hints.get(RenderingHints.KEY_ANTIALIASING); 866 867 if(antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON && 869 antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT){ 870 if(antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF) 873 isAntialiased = false; 874 } 875 } 876 else 877 isAntialiased = false; 878 879 } 880 881 boolean useFractionalMetrics = true; 885 if(hints.get(RenderingHints.KEY_FRACTIONALMETRICS) 886 == RenderingHints.VALUE_FRACTIONALMETRICS_OFF) 887 useFractionalMetrics = false; 888 889 FontRenderContext frc = new FontRenderContext (defaultTransform, 890 isAntialiased, 891 useFractionalMetrics); 892 return frc; 893 } 894 } 895 | Popular Tags |