1 40 41 package org.jfree.experimental.swt; 42 43 import java.awt.BasicStroke ; 44 import java.awt.Color ; 45 import java.awt.Composite ; 46 import java.awt.Font ; 47 import java.awt.FontMetrics ; 48 import java.awt.Graphics ; 49 import java.awt.Graphics2D ; 50 import java.awt.GraphicsConfiguration ; 51 import java.awt.Image ; 52 import java.awt.Paint ; 53 import java.awt.Rectangle ; 54 import java.awt.RenderingHints ; 55 import java.awt.Shape ; 56 import java.awt.Stroke ; 57 import java.awt.RenderingHints.Key; 58 import java.awt.font.FontRenderContext ; 59 import java.awt.font.GlyphVector ; 60 import java.awt.geom.AffineTransform ; 61 import java.awt.geom.PathIterator ; 62 import java.awt.image.BufferedImage ; 63 import java.awt.image.BufferedImageOp ; 64 import java.awt.image.DirectColorModel ; 65 import java.awt.image.ImageObserver ; 66 import java.awt.image.IndexColorModel ; 67 import java.awt.image.RenderedImage ; 68 import java.awt.image.WritableRaster ; 69 import java.awt.image.renderable.RenderableImage ; 70 import java.text.AttributedCharacterIterator ; 71 import java.util.Map ; 72 73 import javax.swing.JPanel ; 74 75 import org.eclipse.swt.SWT; 76 import org.eclipse.swt.graphics.FontData; 77 import org.eclipse.swt.graphics.GC; 78 import org.eclipse.swt.graphics.ImageData; 79 import org.eclipse.swt.graphics.PaletteData; 80 import org.eclipse.swt.graphics.Path; 81 import org.eclipse.swt.graphics.RGB; 82 import org.eclipse.swt.graphics.Transform; 83 84 89 public class SWTGraphics2D extends Graphics2D { 90 91 92 private GC gc; 93 94 95 private static final JPanel DUMMY_PANEL = new JPanel (); 96 97 102 public SWTGraphics2D(GC gc) { 103 super(); 104 this.gc = gc; 105 } 106 107 113 private void switchColors() { 114 org.eclipse.swt.graphics.Color bg = gc.getBackground(); 115 org.eclipse.swt.graphics.Color fg = gc.getForeground(); 116 gc.setBackground(fg); 117 gc.setForeground(bg); 118 } 119 120 127 private Path toSwtPath(Shape shape) { 128 int type; 129 float[] coords = new float[6]; 130 Path path = new Path(this.gc.getDevice()); 131 PathIterator pit = shape.getPathIterator(null); 132 while (!pit.isDone()) { 133 type = pit.currentSegment(coords); 134 switch (type) { 135 case (PathIterator.SEG_MOVETO): 136 path.moveTo(coords[0], coords[1]); 137 break; 138 case (PathIterator.SEG_LINETO): 139 path.lineTo(coords[0], coords[1]); 140 break; 141 case (PathIterator.SEG_QUADTO): 142 path.quadTo(coords[0], coords[1], coords[2], coords[3]); 143 break; 144 case (PathIterator.SEG_CUBICTO): 145 path.cubicTo(coords[0], coords[1], coords[2], 146 coords[3], coords[4], coords[5]); 147 break; 148 case (PathIterator.SEG_CLOSE): 149 path.close(); 150 break; 151 default: 152 break; 153 } 154 pit.next(); 155 } 156 return path; 157 } 158 159 166 private Transform toSwtTransform(AffineTransform awtTransform) { 167 Transform t = new Transform (gc.getDevice()); 168 double[] matrix = new double[6]; 169 awtTransform.getMatrix(matrix); 170 t.setElements((float) matrix[0], (float) matrix[1], 171 (float) matrix[2], (float) matrix[3], 172 (float) matrix[4], (float) matrix[5]); 173 return t; 174 } 175 176 183 private AffineTransform toAwtTransform(Transform swtTransform) { 184 float[] elements = new float[6]; 185 swtTransform.getElements(elements); 186 AffineTransform awtTransform = new AffineTransform (elements); 187 return awtTransform; 188 } 189 190 193 public void draw(Shape shape) { 194 Path path = toSwtPath(shape); 195 gc.drawPath(path); 196 path.dispose(); 197 } 198 199 203 public boolean drawImage(Image image, AffineTransform xform, 204 ImageObserver obs) { 205 return false; 207 } 208 209 213 public void drawImage(BufferedImage image, BufferedImageOp op, int x, 214 int y) { 215 org.eclipse.swt.graphics.Image im 216 = new org.eclipse.swt.graphics.Image(gc.getDevice(), 217 convertToSWT(image)); 218 gc.drawImage(im, x, y); 219 im.dispose(); 220 } 221 222 229 public void drawImage(org.eclipse.swt.graphics.Image image, int x, int y) { 230 gc.drawImage(image, x, y); 231 } 232 233 237 public void drawRenderedImage(RenderedImage image, AffineTransform xform) { 238 } 240 241 245 public void drawRenderableImage(RenderableImage image, 246 AffineTransform xform) { 247 249 } 250 251 258 public void drawString(String text, int x, int y) { 259 float fm = gc.getFontMetrics().getAscent(); 260 gc.drawString(text, x, (int) (y - fm), true); 261 } 262 263 266 public void drawString(String text, float x, float y) { 267 float fm = gc.getFontMetrics().getAscent(); 268 gc.drawString(text, (int) x, (int) ( y - fm ), true); 269 } 270 271 275 public void drawString(AttributedCharacterIterator iterator, int x, int y) { 276 278 } 279 280 284 public void drawString(AttributedCharacterIterator iterator, float x, 285 float y) { 286 288 } 289 290 296 public void fill(Shape shape) { 297 Path path = toSwtPath(shape); 298 switchColors(); 299 this.gc.fillPath(path); 300 switchColors(); 301 path.dispose(); 302 } 303 304 307 public boolean hit(Rectangle rect, Shape text, boolean onStroke) { 308 return false; 310 } 311 312 315 public GraphicsConfiguration getDeviceConfiguration() { 316 return null; 318 } 319 320 323 public void setComposite(Composite comp) { 324 326 } 327 328 332 public void setPaint(Paint paint) { 333 if (paint instanceof Color ) { 334 setColor((Color ) paint); 335 } 336 else { 337 throw new RuntimeException ("Can only handle 'Color' at present."); 338 } 339 } 340 341 344 public void setStroke(Stroke stroke) { 345 if (stroke instanceof BasicStroke ) { 346 BasicStroke bs = (BasicStroke ) stroke; 347 gc.setLineWidth((int) bs.getLineWidth()); 349 350 switch (bs.getLineJoin()) { 352 case BasicStroke.JOIN_BEVEL : 353 gc.setLineJoin(SWT.JOIN_BEVEL); 354 break; 355 case BasicStroke.JOIN_MITER : 356 gc.setLineJoin(SWT.JOIN_MITER); 357 break; 358 case BasicStroke.JOIN_ROUND : 359 gc.setLineJoin(SWT.JOIN_ROUND); 360 break; 361 } 362 363 switch (bs.getEndCap()) { 365 case BasicStroke.CAP_BUTT : 366 gc.setLineCap(SWT.CAP_FLAT); 367 break; 368 case BasicStroke.CAP_ROUND : 369 gc.setLineCap(SWT.CAP_ROUND); 370 break; 371 case BasicStroke.CAP_SQUARE : 372 gc.setLineCap(SWT.CAP_SQUARE); 373 break; 374 } 375 376 gc.setLineStyle(SWT.LINE_SOLID); 378 379 float[] dashes = bs.getDashArray(); 381 if (dashes != null) { 382 int[] swtDashes = new int[dashes.length]; 383 for (int i = 0; i < swtDashes.length; i++) { 384 swtDashes[i] = (int) dashes[i]; 385 } 386 gc.setLineDash(swtDashes); 387 } 388 } 389 else { 390 throw new RuntimeException ( 391 "Can only handle 'Basic Stroke' at present."); 392 } 393 } 394 395 399 public void setRenderingHint(Key hintKey, Object hintValue) { 400 402 } 403 404 407 public Object getRenderingHint(Key hintKey) { 408 return null; 410 } 411 412 415 public void setRenderingHints(Map hints) { 416 418 } 419 420 423 public void addRenderingHints(Map hints) { 424 426 } 427 428 431 public RenderingHints getRenderingHints() { 432 return null; 434 } 435 436 439 public void translate(int x, int y) { 440 Transform swtTransform = new Transform (gc.getDevice()); 441 gc.getTransform(swtTransform); 442 swtTransform.translate(x, y); 443 gc.setTransform(swtTransform); 444 swtTransform.dispose(); 445 } 446 447 450 public void translate(double tx, double ty) { 451 translate((int) tx, (int) ty); 452 } 453 454 457 public void rotate(double theta) { 458 Transform swtTransform = new Transform (gc.getDevice()); 459 gc.getTransform(swtTransform); 460 swtTransform.rotate( (float) (theta * 180 / Math.PI)); 461 gc.setTransform(swtTransform); 462 swtTransform.dispose(); 463 } 464 465 468 public void rotate(double theta, double x, double y) { 469 471 } 472 473 476 public void scale(double scaleX, double scaleY) { 477 Transform swtTransform = new Transform (gc.getDevice()); 478 gc.getTransform(swtTransform); 479 swtTransform.scale((float) scaleX, (float) scaleY); 480 gc.setTransform(swtTransform); 481 swtTransform.dispose(); 482 } 483 484 487 public void shear(double shearX, double shearY) { 488 Transform swtTransform = new Transform (gc.getDevice()); 489 gc.getTransform(swtTransform); 490 Transform shear = new Transform (gc.getDevice(), 1f, (float) shearX, 491 (float) shearY, 1f, 0, 0); 492 swtTransform.multiply(shear); 493 gc.setTransform(swtTransform); 494 swtTransform.dispose(); 495 } 496 497 500 public void transform(AffineTransform Tx) { 501 Transform swtTransform = new Transform (gc.getDevice()); 502 gc.getTransform(swtTransform); 503 swtTransform.multiply(toSwtTransform(Tx)); 504 gc.setTransform(swtTransform); 505 swtTransform.dispose(); 506 } 507 508 511 public void setTransform(AffineTransform Tx) { 512 gc.setTransform(toSwtTransform(Tx)); 513 } 514 515 518 public AffineTransform getTransform() { 519 Transform swtTransform = new Transform (gc.getDevice()); 520 gc.getTransform(swtTransform); 521 return toAwtTransform(swtTransform); 522 } 523 524 527 public Paint getPaint() { 528 return SWTUtils.toAwtColor(gc.getForeground()); 529 } 530 531 534 public Composite getComposite() { 535 return null; 537 } 538 539 542 public void setBackground(Color color) { 543 gc.setBackground(SWTUtils.toSwtColor(gc.getDevice(), color)); 545 } 546 547 550 public Color getBackground() { 551 return SWTUtils.toAwtColor(gc.getBackground()); 552 } 553 554 557 public Stroke getStroke() { 558 return new BasicStroke (gc.getLineWidth(), gc.getLineCap(), 559 gc.getLineJoin()); 560 } 561 562 565 public void clip(Shape s) { 566 Path path = toSwtPath(s); 567 gc.setClipping(path); 568 path.dispose(); 569 } 570 571 574 public FontRenderContext getFontRenderContext() { 575 FontRenderContext fontRenderContext 576 = new FontRenderContext (new AffineTransform (), true, true); 577 return fontRenderContext; 578 } 579 580 584 public void drawGlyphVector(GlyphVector g, float x, float y) { 585 587 } 588 589 592 public Graphics create() { 593 return null; 595 } 596 597 600 public Color getColor() { 601 return SWTUtils.toAwtColor(gc.getForeground()); 602 } 603 604 607 public void setColor(Color color) { 608 gc.setForeground(SWTUtils.toSwtColor( gc.getDevice(), color)); 610 } 611 612 615 public void setPaintMode() { 616 } 618 619 622 public void setXORMode(Color color) { 623 625 } 626 627 633 public Font getFont() { 634 FontData[] fontData = gc.getFont().getFontData(); 636 return SWTUtils.toAwtFont(gc.getDevice(), fontData[0], true); 638 } 639 640 646 public void setFont(Font font) { 647 org.eclipse.swt.graphics.Font swtFont 649 = new org.eclipse.swt.graphics.Font( 650 gc.getDevice(), 651 SWTUtils.toSwtFontData(gc.getDevice(), font, true)); 652 gc.setFont( swtFont ); 653 } 654 655 658 public FontMetrics getFontMetrics(Font font) { 659 return DUMMY_PANEL.getFontMetrics(font); 660 } 661 662 665 public Rectangle getClipBounds() { 666 org.eclipse.swt.graphics.Rectangle clip = gc.getClipping(); 667 return new Rectangle (clip.x, clip.y, clip.width, clip.height); 668 } 669 670 673 public void clipRect(int x, int y, int width, int height) { 674 org.eclipse.swt.graphics.Rectangle clip = gc.getClipping(); 675 clip.intersects(x, y, width, height); 676 gc.setClipping(clip); 677 } 678 679 682 public void setClip(int x, int y, int width, int height) { 683 gc.setClipping(x, y, width, height); 684 } 685 686 689 public Shape getClip() { 690 return null; 692 } 693 694 697 public void setClip(Shape clip) { 698 if (clip == null) 699 return; 700 Path clipPath = toSwtPath(clip); 701 gc.setClipping(clipPath); 702 clipPath.dispose(); 703 } 704 705 708 public void copyArea(int x, int y, int width, int height, int dx, int dy) { 709 711 } 712 713 717 public void drawLine(int x1, int y1, int x2, int y2) { 718 gc.drawLine(x1, y1, x2, y2); 719 } 720 721 725 public void fillRect(int x, int y, int width, int height) { 726 gc.fillRectangle(x, y, width, height); 727 } 728 729 732 public void clearRect(int x, int y, int width, int height) { 733 735 } 736 737 740 public void drawRoundRect(int x, int y, int width, int height, 741 int arcWidth, int arcHeight) { 742 744 } 745 746 749 public void fillRoundRect(int x, int y, int width, int height, 750 int arcWidth, int arcHeight) { 751 753 } 754 755 758 public void drawOval(int x, int y, int width, int height) { 759 761 } 762 763 766 public void fillOval(int x, int y, int width, int height) { 767 769 } 770 771 774 public void drawArc(int x, int y, int width, int height, int arcStart, 775 int arcAngle) { 776 778 } 779 780 783 public void fillArc(int x, int y, int width, int height, int arcStart, 784 int arcAngle) { 785 787 } 788 789 792 public void drawPolyline(int [] xPoints, int [] yPoints, int npoints) { 793 795 } 796 797 800 public void drawPolygon(int [] xPoints, int [] yPoints, int npoints) { 801 803 } 804 805 808 public void fillPolygon(int [] xPoints, int [] yPoints, int npoints) { 809 811 } 812 813 817 public boolean drawImage(Image image, int x, int y, 818 ImageObserver observer) { 819 return false; 821 } 822 823 827 public boolean drawImage(Image image, int x, int y, int width, int height, 828 ImageObserver observer) { 829 return false; 831 } 832 833 837 public boolean drawImage(Image image, int x, int y, Color bgcolor, 838 ImageObserver observer) { 839 return false; 841 } 842 843 847 public boolean drawImage(Image image, int x, int y, int width, int height, 848 Color bgcolor, ImageObserver observer) { 849 return false; 851 } 852 853 857 public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, 858 int sx1, int sy1, int sx2, int sy2, ImageObserver observer) { 859 return false; 861 } 862 863 867 public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, 868 int sx1, int sy1, int sx2, int sy2, Color bgcolor, 869 ImageObserver observer) { 870 return false; 872 } 873 874 877 public void dispose() { 878 gc.dispose(); 879 } 880 881 static ImageData convertToSWT(BufferedImage bufferedImage) { 882 if (bufferedImage.getColorModel() instanceof DirectColorModel ) { 883 DirectColorModel colorModel 884 = (DirectColorModel ) bufferedImage.getColorModel(); 885 PaletteData palette = new PaletteData(colorModel.getRedMask(), 886 colorModel.getGreenMask(), colorModel.getBlueMask()); 887 ImageData data = new ImageData(bufferedImage.getWidth(), 888 bufferedImage.getHeight(), colorModel.getPixelSize(), 889 palette); 890 WritableRaster raster = bufferedImage.getRaster(); 891 int[] pixelArray = new int[3]; 892 for (int y = 0; y < data.height; y++) { 893 for (int x = 0; x < data.width; x++) { 894 raster.getPixel(x, y, pixelArray); 895 int pixel = palette.getPixel(new RGB(pixelArray[0], 896 pixelArray[1], pixelArray[2])); 897 data.setPixel(x, y, pixel); 898 } 899 } 900 return data; 901 } 902 else if (bufferedImage.getColorModel() instanceof IndexColorModel ) { 903 IndexColorModel colorModel 904 = (IndexColorModel ) bufferedImage.getColorModel(); 905 int size = colorModel.getMapSize(); 906 byte[] reds = new byte[size]; 907 byte[] greens = new byte[size]; 908 byte[] blues = new byte[size]; 909 colorModel.getReds(reds); 910 colorModel.getGreens(greens); 911 colorModel.getBlues(blues); 912 RGB[] rgbs = new RGB[size]; 913 for (int i = 0; i < rgbs.length; i++) { 914 rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, 915 blues[i] & 0xFF); 916 } 917 PaletteData palette = new PaletteData(rgbs); 918 ImageData data = new ImageData(bufferedImage.getWidth(), 919 bufferedImage.getHeight(), colorModel.getPixelSize(), 920 palette); 921 data.transparentPixel = colorModel.getTransparentPixel(); 922 WritableRaster raster = bufferedImage.getRaster(); 923 int[] pixelArray = new int[1]; 924 for (int y = 0; y < data.height; y++) { 925 for (int x = 0; x < data.width; x++) { 926 raster.getPixel(x, y, pixelArray); 927 data.setPixel(x, y, pixelArray[0]); 928 } 929 } 930 return data; 931 } 932 return null; 933 } 934 } 935 | Popular Tags |