| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation; |
| 2 | |
| 3 | import java.awt.Graphics2D; |
| 4 | import java.awt.image.BufferedImage; |
| 5 | |
| 6 | import org.eclipse.swt.graphics.GC; |
| 7 | import org.eclipse.swt.graphics.Image; |
| 8 | import org.eclipse.swt.graphics.ImageData; |
| 9 | import org.eclipse.swt.graphics.PaletteData; |
| 10 | import org.eclipse.swt.widgets.Display; |
| 11 | |
| 12 | /** |
| 13 | * Helper class allowing the use of Java 2D on SWT or Draw2D graphical |
| 14 | * context. |
| 15 | * @author Yannick Saillet |
| 16 | */ |
| 17 | public class Graphics2DRenderer { |
| 18 | private static final PaletteData PALETTE_DATA = |
| 19 | new PaletteData(0xFF0000, 0xFF00, 0xFF); |
| 20 | |
| 21 | private BufferedImage awtImage; |
| 22 | private Image swtImage; |
| 23 | private ImageData swtImageData; |
| 24 | private int[] awtPixels; |
| 25 | |
| 26 | /** RGB value to use as transparent color */ |
| 27 | private static final int TRANSPARENT_COLOR = 0x123456; |
| 28 | |
| 29 | /** |
| 30 | * Prepare to render on a SWT graphics context. |
| 31 | */ |
| 32 | public void prepareRendering(GC gc) { |
| 33 | org.eclipse.swt.graphics.Rectangle clip = gc.getClipping(); |
| 34 | prepareRendering(clip.x, clip.y, clip.width, clip.height); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Prepare to render on a Draw2D graphics context. |
| 39 | */ |
| 40 | public void prepareRendering(org.eclipse.draw2d.Graphics graphics) { |
| 41 | org.eclipse.draw2d.geometry.Rectangle clip = |
| 42 | graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle()); |
| 43 | prepareRendering(clip.x, clip.y, clip.width, clip.height); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Prepare the AWT offscreen image for the rendering of the rectangular |
| 48 | * region given as parameter. |
| 49 | */ |
| 50 | private void prepareRendering(int clipX, int clipY, int clipW, int clipH) { |
| 51 | // check that the offscreen images are initialized and large enough |
| 52 | checkOffScreenImages(clipW, clipH); |
| 53 | // fill the region in the AWT image with the transparent color |
| 54 | java.awt.Graphics awtGraphics = awtImage.getGraphics(); |
| 55 | awtGraphics.setColor(new java.awt.Color(TRANSPARENT_COLOR)); |
| 56 | awtGraphics.fillRect(clipX, clipY, clipW, clipH); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the Graphics2D context to use. |
| 61 | */ |
| 62 | public Graphics2D getGraphics2D() { |
| 63 | if (awtImage == null) return null; |
| 64 | return (Graphics2D) awtImage.getGraphics(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Complete the rendering by flushing the 2D renderer on a SWT graphical |
| 69 | * context. |
| 70 | */ |
| 71 | public void render(GC gc) { |
| 72 | if (awtImage == null) return; |
| 73 | |
| 74 | org.eclipse.swt.graphics.Rectangle clip = gc.getClipping(); |
| 75 | transferPixels(clip.x, clip.y, clip.width, clip.height); |
| 76 | gc.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height, |
| 77 | clip.x, clip.y, clip.width, clip.height); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Complete the rendering by flushing the 2D renderer on a Draw2D |
| 82 | * graphical context. |
| 83 | */ |
| 84 | public void render(org.eclipse.draw2d.Graphics graphics) { |
| 85 | if (awtImage == null) return; |
| 86 | |
| 87 | org.eclipse.draw2d.geometry.Rectangle clip = |
| 88 | graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle()); |
| 89 | transferPixels(clip.x, clip.y, clip.width, clip.height); |
| 90 | graphics.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height, |
| 91 | clip.x, clip.y, clip.width, clip.height); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Transfer a rectangular region from the AWT image to the SWT image. |
| 96 | */ |
| 97 | private void transferPixels(int clipX, int clipY, int clipW, int clipH) { |
| 98 | int step = swtImageData.depth / 8; |
| 99 | byte[] data = swtImageData.data; |
| 100 | awtImage.getRGB(clipX, clipY, clipW, clipH, awtPixels, 0, clipW); |
| 101 | for (int i = 0; i < clipH; i++) { |
| 102 | int idx = (clipY + i) * swtImageData.bytesPerLine + clipX * step; |
| 103 | for (int j = 0; j < clipW; j++) { |
| 104 | int rgb = awtPixels[j + i * clipW]; |
| 105 | for (int k = swtImageData.depth - 8; k >= 0; k -= 8) { |
| 106 | data[idx++] = (byte) ((rgb >> k) & 0xFF); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | if (swtImage != null) swtImage.dispose(); |
| 111 | swtImage = new Image(Display.getDefault(), swtImageData); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Dispose the resources attached to this 2D renderer. |
| 116 | */ |
| 117 | public void dispose() { |
| 118 | if (awtImage != null) awtImage.flush(); |
| 119 | if (swtImage != null) swtImage.dispose(); |
| 120 | awtImage = null; |
| 121 | swtImageData = null; |
| 122 | awtPixels = null; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Ensure that the offscreen images are initialized and are at least |
| 127 | * as large as the size given as parameter. |
| 128 | */ |
| 129 | private void checkOffScreenImages(int width, int height) { |
| 130 | int currentImageWidth = 0; |
| 131 | int currentImageHeight = 0; |
| 132 | if (swtImage != null) { |
| 133 | currentImageWidth = swtImage.getImageData().width; |
| 134 | currentImageHeight = swtImage.getImageData().height; |
| 135 | } |
| 136 | |
| 137 | // if the offscreen images are too small, recreate them |
| 138 | if (width > currentImageWidth || height > currentImageHeight) { |
| 139 | dispose(); |
| 140 | width = Math.max(width, currentImageWidth); |
| 141 | height = Math.max(height, currentImageHeight); |
| 142 | awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 143 | swtImageData = new ImageData(width, height, 24, PALETTE_DATA); |
| 144 | swtImageData.transparentPixel = TRANSPARENT_COLOR; |
| 145 | awtPixels = new int[width * height]; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } |