1 16 17 package org.apache.poi.hssf.usermodel; 18 19 import org.apache.poi.util.POILogFactory; 20 import org.apache.poi.util.POILogger; 21 22 import java.awt.*; 23 import java.awt.font.FontRenderContext ; 24 import java.awt.font.GlyphVector ; 25 import java.awt.font.TextLayout ; 26 import java.awt.geom.AffineTransform ; 27 import java.awt.geom.Area ; 28 import java.awt.geom.GeneralPath ; 29 import java.awt.image.BufferedImage ; 30 import java.awt.image.BufferedImageOp ; 31 import java.awt.image.ImageObserver ; 32 import java.awt.image.RenderedImage ; 33 import java.awt.image.renderable.RenderableImage ; 34 import java.text.AttributedCharacterIterator ; 35 import java.util.Map ; 36 37 71 public class EscherGraphics2d extends Graphics2D 72 { 73 private EscherGraphics escherGraphics; 74 private BufferedImage img; 75 private AffineTransform trans; 76 private Stroke stroke; 77 private Paint paint; 78 private Shape deviceclip; 79 private POILogger logger = POILogFactory.getLogger(getClass()); 80 81 86 public EscherGraphics2d(EscherGraphics escherGraphics) 87 { 88 this.escherGraphics = escherGraphics; 89 setImg( new BufferedImage (1, 1, 2) ); 90 setColor(Color.black); 91 } 92 93 public void addRenderingHints(Map map) 94 { 95 getG2D().addRenderingHints(map); 96 } 97 98 public void clearRect(int i, int j, int k, int l) 99 { 100 Paint paint1 = getPaint(); 101 setColor(getBackground()); 102 fillRect(i, j, k, l); 103 setPaint(paint1); 104 } 105 106 public void clip(Shape shape) 107 { 108 if(getDeviceclip() != null) 109 { 110 Area area = new Area (getClip()); 111 if(shape != null) 112 area.intersect(new Area (shape)); 113 shape = area; 114 } 115 setClip(shape); 116 } 117 118 public void clipRect(int x, int y, int width, int height) 119 { 120 clip(new Rectangle(x,y,width,height)); 121 } 122 123 public void copyArea(int x, int y, int width, int height, 124 int dx, int dy) 125 { 126 getG2D().copyArea(x,y,width,height,dx,dy); 127 } 128 129 public Graphics create() 130 { 131 EscherGraphics2d g2d = new EscherGraphics2d(escherGraphics); 132 return g2d; 133 } 134 135 public void dispose() 136 { 137 getEscherGraphics().dispose(); 138 getG2D().dispose(); 139 getImg().flush(); 140 } 141 142 public void draw(Shape shape) 143 { 144 if (logger.check( POILogger.WARN )) 145 logger.log(POILogger.WARN,"copyArea not supported"); 146 } 147 148 public void drawArc(int x, int y, int width, int height, 149 int startAngle, int arcAngle) 150 { 151 draw(new java.awt.geom.Arc2D.Float(x, y, width, height, startAngle, arcAngle, 0)); 152 } 153 154 public void drawGlyphVector(GlyphVector g, float x, float y) 155 { 156 fill(g.getOutline(x, y)); 157 } 158 159 public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, 160 int sx2, int sy2, Color bgColor, ImageObserver imageobserver) 161 { 162 if (logger.check( POILogger.WARN )) 163 logger.log(POILogger.WARN,"drawImage() not supported"); 164 return true; 165 } 166 167 public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, 168 int sx2, int sy2, ImageObserver imageobserver) 169 { 170 if (logger.check( POILogger.WARN )) 171 logger.log(POILogger.WARN,"drawImage() not supported"); 172 return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, imageobserver); 173 } 174 public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, Color bgColor, ImageObserver imageobserver) 175 { 176 if (logger.check( POILogger.WARN )) 177 logger.log(POILogger.WARN,"drawImage() not supported"); 178 return true; 179 } 180 181 public boolean drawImage(Image img, int x, int y, 182 int width, int height, 183 ImageObserver observer) 184 { 185 return drawImage(img, x,y,width,height, null, observer); 186 } 187 188 public boolean drawImage(Image image, int x, int y, Color bgColor, ImageObserver imageobserver) 189 { 190 return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), bgColor, imageobserver); 191 } 192 193 public boolean drawImage(Image image, int x, int y, ImageObserver imageobserver) 194 { 195 return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver); 196 } 197 198 public boolean drawImage(Image image, AffineTransform affinetransform, ImageObserver imageobserver) 199 { 200 AffineTransform affinetransform1 = (AffineTransform )getTrans().clone(); 201 getTrans().concatenate(affinetransform); 202 drawImage(image, 0, 0, imageobserver); 203 setTrans( affinetransform1 ); 204 return true; 205 } 206 207 public void drawImage(BufferedImage bufferedimage, BufferedImageOp op, int x, int y) 208 { 209 BufferedImage img = op.filter(bufferedimage, null); 210 drawImage(((Image ) (img)), new AffineTransform (1.0F, 0.0F, 0.0F, 1.0F, x, y), null); 211 } 212 213 public void drawLine(int x1, int y1, int x2, int y2) 214 { 215 getEscherGraphics().drawLine(x1,y1,x2,y2); 216 } 218 219 public void drawOval(int x, int y, int width, int height) 220 { 221 getEscherGraphics().drawOval(x,y,width,height); 222 } 224 225 public void drawPolygon(int xPoints[], int yPoints[], 226 int nPoints) 227 { 228 getEscherGraphics().drawPolygon(xPoints, yPoints, nPoints); 229 } 230 231 public void drawPolyline(int xPoints[], int yPoints[], int nPoints) 232 { 233 if(nPoints > 0) 234 { 235 GeneralPath generalpath = new GeneralPath (); 236 generalpath.moveTo(xPoints[0], yPoints[0]); 237 for(int j = 1; j < nPoints; j++) 238 generalpath.lineTo(xPoints[j], yPoints[j]); 239 240 draw(generalpath); 241 } 242 } 243 244 public void drawRect(int x, int y, int width, int height) 245 { 246 escherGraphics.drawRect(x,y,width,height); 247 } 248 249 public void drawRenderableImage(RenderableImage renderableimage, AffineTransform affinetransform) 250 { 251 drawRenderedImage(renderableimage.createDefaultRendering(), affinetransform); 252 } 253 254 public void drawRenderedImage(RenderedImage renderedimage, AffineTransform affinetransform) 255 { 256 BufferedImage bufferedimage = new BufferedImage (renderedimage.getColorModel(), renderedimage.getData().createCompatibleWritableRaster(), false, null); 257 bufferedimage.setData(renderedimage.getData()); 258 drawImage(bufferedimage, affinetransform, null); 259 } 260 261 public void drawRoundRect(int i, int j, int k, int l, int i1, int j1) 262 { 263 draw(new java.awt.geom.RoundRectangle2D.Float(i, j, k, l, i1, j1)); 264 } 265 266 public void drawString(String string, float x, float y) 267 { 268 getEscherGraphics().drawString(string, (int)x, (int)y); 269 } 270 271 public void drawString(String string, int x, int y) 272 { 273 getEscherGraphics().drawString(string, x, y); 274 } 275 276 public void drawString(AttributedCharacterIterator attributedcharacteriterator, float x, float y) 277 { 278 TextLayout textlayout = new TextLayout (attributedcharacteriterator, getFontRenderContext()); 279 Paint paint1 = getPaint(); 280 setColor(getColor()); 281 fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y))); 282 setPaint(paint1); 283 } 284 285 public void drawString(AttributedCharacterIterator attributedcharacteriterator, int x, int y) 286 { 287 drawString(attributedcharacteriterator, x, y); 288 } 289 290 public void fill(Shape shape) 291 { 292 if (logger.check( POILogger.WARN )) 293 logger.log(POILogger.WARN,"fill(Shape) not supported"); 294 } 295 296 public void fillArc(int i, int j, int k, int l, int i1, int j1) 297 { 298 fill(new java.awt.geom.Arc2D.Float(i, j, k, l, i1, j1, 2)); 299 } 300 301 public void fillOval(int x, int y, int width, int height) 302 { 303 escherGraphics.fillOval(x,y,width,height); 304 } 305 306 326 public void fillPolygon(int xPoints[], int yPoints[], int nPoints) 327 { 328 escherGraphics.fillPolygon(xPoints, yPoints, nPoints); 329 } 330 331 public void fillRect(int x, int y, int width, int height) 332 { 333 getEscherGraphics().fillRect(x,y,width,height); 334 } 335 336 public void fillRoundRect(int x, int y, int width, int height, 337 int arcWidth, int arcHeight) 338 { 339 fill(new java.awt.geom.RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight)); 340 } 341 342 public Color getBackground() 343 { 344 return getEscherGraphics().getBackground(); 345 } 346 347 public Shape getClip() 348 { 349 try 350 { 351 return getTrans().createInverse().createTransformedShape(getDeviceclip()); 352 } 353 catch(Exception _ex) 354 { 355 return null; 356 } 357 } 358 359 public Rectangle getClipBounds() 360 { 361 if(getDeviceclip() != null) 362 return getClip().getBounds(); 363 else 364 return null; 365 } 366 367 public Color getColor() 368 { 369 return escherGraphics.getColor(); 370 } 371 372 public Composite getComposite() 373 { 374 return getG2D().getComposite(); 375 } 376 377 public GraphicsConfiguration getDeviceConfiguration() 378 { 379 return getG2D().getDeviceConfiguration(); 380 } 381 382 public Font getFont() 383 { 384 return getEscherGraphics().getFont(); 385 } 386 387 public FontMetrics getFontMetrics(Font font) 388 { 389 return getEscherGraphics().getFontMetrics(font); 390 } 391 392 public FontRenderContext getFontRenderContext() 393 { 394 getG2D().setTransform(getTrans()); 395 return getG2D().getFontRenderContext(); 396 } 397 398 public Paint getPaint() 399 { 400 return paint; 401 } 402 403 public Object getRenderingHint(java.awt.RenderingHints.Key key) 404 { 405 return getG2D().getRenderingHint(key); 406 } 407 408 public RenderingHints getRenderingHints() 409 { 410 return getG2D().getRenderingHints(); 411 } 412 413 public Stroke getStroke() 414 { 415 return stroke; 416 } 417 418 public AffineTransform getTransform() 419 { 420 return (AffineTransform )getTrans().clone(); 421 } 422 423 public boolean hit(Rectangle rectangle, Shape shape, boolean flag) 424 { 425 getG2D().setTransform(getTrans()); 426 getG2D().setStroke(getStroke()); 427 getG2D().setClip(getClip()); 428 return getG2D().hit(rectangle, shape, flag); 429 } 430 431 public void rotate(double d) 432 { 433 getTrans().rotate(d); 434 } 435 436 public void rotate(double d, double d1, double d2) 437 { 438 getTrans().rotate(d, d1, d2); 439 } 440 441 public void scale(double d, double d1) 442 { 443 getTrans().scale(d, d1); 444 } 445 446 public void setBackground(Color c) 447 { 448 getEscherGraphics().setBackground(c); 449 } 450 451 public void setClip(int i, int j, int k, int l) 452 { 453 setClip(((Shape) (new Rectangle(i, j, k, l)))); 454 } 455 456 public void setClip(Shape shape) 457 { 458 setDeviceclip( getTrans().createTransformedShape(shape) ); 459 } 460 461 public void setColor(Color c) 462 { 463 escherGraphics.setColor(c); 464 } 465 466 public void setComposite(Composite composite) 467 { 468 getG2D().setComposite(composite); 469 } 470 471 public void setFont(Font font) 472 { 473 getEscherGraphics().setFont(font); 474 } 475 476 public void setPaint(Paint paint1) 477 { 478 if(paint1 != null) 479 { 480 paint = paint1; 481 if(paint1 instanceof Color) 482 setColor( (Color)paint1 ); 483 } 484 } 485 486 public void setPaintMode() 487 { 488 getEscherGraphics().setPaintMode(); 489 } 490 491 public void setRenderingHint(java.awt.RenderingHints.Key key, Object obj) 492 { 493 getG2D().setRenderingHint(key, obj); 494 } 495 496 public void setRenderingHints(Map map) 497 { 498 getG2D().setRenderingHints(map); 499 } 500 501 public void setStroke(Stroke s) 502 { 503 stroke = s; 504 } 505 506 public void setTransform(AffineTransform affinetransform) 507 { 508 setTrans( (AffineTransform )affinetransform.clone() ); 509 } 510 511 public void setXORMode(Color color1) 512 { 513 getEscherGraphics().setXORMode(color1); 514 } 515 516 public void shear(double d, double d1) 517 { 518 getTrans().shear(d, d1); 519 } 520 521 public void transform(AffineTransform affinetransform) 522 { 523 getTrans().concatenate(affinetransform); 524 } 525 526 538 public void translate(double d, double d1) 539 { 540 getTrans().translate(d, d1); 541 } 542 543 public void translate(int i, int j) 544 { 545 getTrans().translate(i, j); 546 } 547 548 private EscherGraphics getEscherGraphics() 549 { 550 return escherGraphics; 551 } 552 553 private BufferedImage getImg() 554 { 555 return img; 556 } 557 558 private void setImg( BufferedImage img ) 559 { 560 this.img = img; 561 } 562 563 private Graphics2D getG2D() 564 { 565 return (Graphics2D) img.getGraphics(); 566 } 567 568 private AffineTransform getTrans() 569 { 570 return trans; 571 } 572 573 private void setTrans( AffineTransform trans ) 574 { 575 this.trans = trans; 576 } 577 578 private Shape getDeviceclip() 579 { 580 return deviceclip; 581 } 582 583 private void setDeviceclip( Shape deviceclip ) 584 { 585 this.deviceclip = deviceclip; 586 } 587 588 } 589 | Popular Tags |