1 18 package org.apache.batik.ext.awt.g2d; 19 20 import java.awt.AlphaComposite ; 21 import java.awt.Color ; 22 import java.awt.Composite ; 23 import java.awt.Font ; 24 import java.awt.Graphics2D ; 25 import java.awt.Image ; 26 import java.awt.Paint ; 27 import java.awt.Polygon ; 28 import java.awt.Rectangle ; 29 import java.awt.RenderingHints ; 30 import java.awt.Shape ; 31 import java.awt.Stroke ; 32 import java.awt.font.FontRenderContext ; 33 import java.awt.font.GlyphVector ; 34 import java.awt.geom.AffineTransform ; 35 import java.awt.geom.Arc2D ; 36 import java.awt.geom.Ellipse2D ; 37 import java.awt.geom.GeneralPath ; 38 import java.awt.geom.Line2D ; 39 import java.awt.geom.NoninvertibleTransformException ; 40 import java.awt.geom.RoundRectangle2D ; 41 import java.awt.image.BufferedImage ; 42 import java.awt.image.BufferedImageOp ; 43 import java.awt.image.ImageObserver ; 44 import java.text.AttributedCharacterIterator ; 45 import java.util.Map ; 46 47 66 public abstract class AbstractGraphics2D extends Graphics2D implements Cloneable { 67 72 protected GraphicContext gc; 73 74 77 protected boolean textAsShapes = false; 78 79 84 public AbstractGraphics2D(boolean textAsShapes){ 85 this.textAsShapes = textAsShapes; 86 } 87 88 93 public AbstractGraphics2D(AbstractGraphics2D g){ 94 this.gc = (GraphicContext)g.gc.clone(); 95 this.gc.validateTransformStack(); 96 this.textAsShapes = g.textAsShapes; 97 } 98 99 110 public void translate(int x, int y){ 111 gc.translate(x, y); 112 } 113 114 120 public Color getColor(){ 121 return gc.getColor(); 122 } 123 124 132 public void setColor(Color c){ 133 gc.setColor(c); 134 } 135 136 143 public void setPaintMode(){ 144 gc.setComposite(AlphaComposite.SrcOver); 145 } 146 147 153 public Font getFont(){ 154 return gc.getFont(); 155 } 156 157 164 public void setFont(Font font){ 165 gc.setFont(font); 166 } 167 168 185 public Rectangle getClipBounds(){ 186 return gc.getClipBounds(); 187 } 188 189 190 209 public void clipRect(int x, int y, int width, int height){ 210 gc.clipRect(x, y, width, height); 211 } 212 213 214 228 public void setClip(int x, int y, int width, int height){ 229 gc.setClip(x, y, width, height); 230 } 231 232 233 249 public Shape getClip(){ 250 return gc.getClip(); 251 } 252 253 254 270 public void setClip(Shape clip){ 271 gc.setClip(clip); 272 } 273 274 275 284 public void drawLine(int x1, int y1, int x2, int y2){ 285 Line2D line = new Line2D.Float (x1, y1, x2, y2); 286 draw(line); 287 } 288 289 290 309 public void fillRect(int x, int y, int width, int height){ 310 Rectangle rect = new Rectangle (x, y, width, height); 311 fill(rect); 312 } 313 314 public void drawRect(int x, int y, int width, int height){ 315 Rectangle rect = new Rectangle (x, y, width, height); 316 draw(rect); 317 } 318 319 320 321 340 public void clearRect(int x, int y, int width, int height){ 341 Paint paint = gc.getPaint(); 342 gc.setColor(gc.getBackground()); 343 fillRect(x, y, width, height); 344 gc.setPaint(paint); 345 } 346 347 363 public void drawRoundRect(int x, int y, int width, int height, 364 int arcWidth, int arcHeight){ 365 RoundRectangle2D rect = new RoundRectangle2D.Float (x, y, width, height, arcWidth, arcHeight); 366 draw(rect); 367 } 368 369 370 386 public void fillRoundRect(int x, int y, int width, int height, 387 int arcWidth, int arcHeight){ 388 RoundRectangle2D rect = new RoundRectangle2D.Float (x, y, width, height, arcWidth, arcHeight); 389 fill(rect); 390 } 391 392 393 410 public void drawOval(int x, int y, int width, int height){ 411 Ellipse2D oval = new Ellipse2D.Float (x, y, width, height); 412 draw(oval); 413 } 414 415 416 427 public void fillOval(int x, int y, int width, int height){ 428 Ellipse2D oval = new Ellipse2D.Float (x, y, width, height); 429 fill(oval); 430 } 431 432 433 470 public void drawArc(int x, int y, int width, int height, 471 int startAngle, int arcAngle){ 472 Arc2D arc = new Arc2D.Float (x, y, width, height, startAngle, arcAngle, Arc2D.OPEN); 473 draw(arc); 474 } 475 476 477 513 public void fillArc(int x, int y, int width, int height, 514 int startAngle, int arcAngle){ 515 Arc2D arc = new Arc2D.Float (x, y, width, height, startAngle, arcAngle, Arc2D.PIE); 516 fill(arc); 517 } 518 519 520 532 public void drawPolyline(int xPoints[], int yPoints[], 533 int nPoints){ 534 if(nPoints > 0){ 535 GeneralPath path = new GeneralPath (); 536 path.moveTo(xPoints[0], yPoints[0]); 537 for(int i=1; i<nPoints; i++) 538 path.lineTo(xPoints[i], yPoints[i]); 539 540 draw(path); 541 } 542 } 543 544 563 public void drawPolygon(int xPoints[], int yPoints[], 564 int nPoints){ 565 Polygon polygon = new Polygon (xPoints, yPoints, nPoints); 566 draw(polygon); 567 } 568 569 570 590 public void fillPolygon(int xPoints[], int yPoints[], 591 int nPoints){ 592 Polygon polygon = new Polygon (xPoints, yPoints, nPoints); 593 fill(polygon); 594 } 595 596 607 public void drawString(String str, int x, int y){ 608 drawString(str, (float)x, (float)y); 609 } 610 611 612 624 public void drawString(AttributedCharacterIterator iterator, 625 int x, int y){ 626 drawString(iterator, (float)x, (float)y); 627 } 628 629 659 public boolean drawImage(Image img, int x, int y, 660 Color bgcolor, 661 ImageObserver observer){ 662 return drawImage(img, x, y, img.getWidth(null), img.getHeight(null), 663 bgcolor, observer); 664 } 665 666 667 705 public boolean drawImage(Image img, int x, int y, 706 int width, int height, 707 Color bgcolor, 708 ImageObserver observer){ 709 Paint paint = gc.getPaint(); 710 gc.setPaint(bgcolor); 711 fillRect(x, y, width, height); 712 gc.setPaint(paint); 713 drawImage(img, x, y, width, height, observer); 714 715 return true; 716 } 717 718 765 public boolean drawImage(Image img, 766 int dx1, int dy1, int dx2, int dy2, 767 int sx1, int sy1, int sx2, int sy2, 768 ImageObserver observer){ 769 BufferedImage src = new BufferedImage (img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); 770 Graphics2D g = src.createGraphics(); 771 g.drawImage(img, 0, 0, null); 772 g.dispose(); 773 774 src = src.getSubimage(sx1, sy1, sx2-sx1, sy2-sy1); 775 776 return drawImage(src, dx1, dy1, dx2-dx1, dy2-dy1, observer); 777 } 778 779 780 833 public boolean drawImage(Image img, 834 int dx1, int dy1, int dx2, int dy2, 835 int sx1, int sy1, int sx2, int sy2, 836 Color bgcolor, 837 ImageObserver observer){ 838 Paint paint = gc.getPaint(); 839 gc.setPaint(bgcolor); 840 fillRect(dx1, dy1, dx2-dx1, dy2-dy1); 841 gc.setPaint(paint); 842 return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer); 843 } 844 845 846 871 public boolean drawImage(Image img, 872 AffineTransform xform, 873 ImageObserver obs){ 874 boolean retVal = true; 875 876 if(xform.getDeterminant() != 0){ 877 AffineTransform inverseTransform = null; 878 try{ 879 inverseTransform = xform.createInverse(); 880 } catch(NoninvertibleTransformException e){ 881 throw new Error (); 884 } 885 886 gc.transform(xform); 887 retVal = drawImage(img, 0, 0, null); 888 gc.transform(inverseTransform); 889 } 890 else{ 891 AffineTransform savTransform = new AffineTransform (gc.getTransform()); 892 gc.transform(xform); 893 retVal = drawImage(img, 0, 0, null); 894 gc.setTransform(savTransform); 895 } 896 897 return retVal; 898 899 } 900 901 902 923 public void drawImage(BufferedImage img, 924 BufferedImageOp op, 925 int x, 926 int y){ 927 img = op.filter(img, null); 928 drawImage(img, x, y, null); 929 } 930 931 932 933 959 public void drawGlyphVector(GlyphVector g, float x, float y){ 960 Shape glyphOutline = g.getOutline(x, y); 961 fill(glyphOutline); 962 } 963 964 993 public boolean hit(Rectangle rect, 994 Shape s, 995 boolean onStroke){ 996 if (onStroke) { 997 s = gc.getStroke().createStrokedShape(s); 998 } 999 1000 s = gc.getTransform().createTransformedShape(s); 1001 1002 return s.intersects(rect); 1003 } 1004 1005 1029 public void setComposite(Composite comp){ 1030 gc.setComposite(comp); 1031 } 1032 1033 1034 1044 public void setPaint(Paint paint) { 1045 gc.setPaint(paint); 1046 } 1047 1048 1049 1054 public void setStroke(Stroke s){ 1055 gc.setStroke(s); 1056 } 1057 1058 1059 1070 public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue){ 1071 gc.setRenderingHint(hintKey, hintValue); 1072 } 1073 1074 1075 1087 public Object getRenderingHint(RenderingHints.Key hintKey){ 1088 return gc.getRenderingHint(hintKey); 1089 } 1090 1091 1092 1105 public void setRenderingHints(Map hints){ 1106 gc.setRenderingHints(hints); 1107 } 1108 1109 1110 1124 public void addRenderingHints(Map hints){ 1125 gc.addRenderingHints(hints); 1126 } 1127 1128 1129 1141 public RenderingHints getRenderingHints(){ 1142 return gc.getRenderingHints(); 1143 } 1144 1145 1161 public void translate(double tx, double ty){ 1162 gc.translate(tx, ty); 1163 } 1164 1165 1166 1182 public void rotate(double theta){ 1183 gc.rotate(theta); 1184 } 1185 1186 1187 1206 public void rotate(double theta, double x, double y){ 1207 gc.rotate(theta, x, y); 1208 } 1209 1210 1211 1230 public void scale(double sx, double sy){ 1231 gc.scale(sx, sy); 1232 } 1233 1234 1252 public void shear(double shx, double shy){ 1253 gc.shear(shx, shy); 1254 } 1255 1256 1273 public void transform(AffineTransform Tx){ 1274 gc.transform(Tx); 1275 } 1276 1277 1285 public void setTransform(AffineTransform Tx){ 1286 gc.setTransform(Tx); 1287 } 1288 1289 1290 1298 public AffineTransform getTransform(){ 1299 return gc.getTransform(); 1300 } 1301 1302 1303 1311 public Paint getPaint(){ 1312 return gc.getPaint(); 1313 } 1314 1315 1316 1323 public Composite getComposite(){ 1324 return gc.getComposite(); 1325 } 1326 1327 1328 1344 public void setBackground(Color color){ 1345 gc.setBackground(color); 1346 } 1347 1348 1349 1355 public Color getBackground(){ 1356 return gc.getBackground(); 1357 } 1358 1359 1360 1367 public Stroke getStroke(){ 1368 return gc.getStroke(); 1369 } 1370 1371 1372 1391 public void clip(Shape s){ 1392 gc.clip(s); 1393 } 1394 1395 1396 1417 public FontRenderContext getFontRenderContext(){ 1418 return gc.getFontRenderContext(); 1419 } 1420 1421 1424 public GraphicContext getGraphicContext() { 1425 return gc; 1426 } 1427} 1428 | Popular Tags |