< prev index next >

application/org.openjdk.jmc.ui/src/main/java/org/openjdk/jmc/ui/misc/AwtCanvas.java

Print this page




  51 public class AwtCanvas {
  52         private ImageData imageDataSWT;
  53         private BufferedImage imageAWT;
  54 
  55         private static final double X_SCALE = (Display.getCurrent().getDPI().x) / Environment.getNormalDPI();
  56         private static final double Y_SCALE = (Display.getCurrent().getDPI().y) / Environment.getNormalDPI();
  57 
  58         public boolean hasImage(int width, int height) {
  59                 return (imageDataSWT != null) && (imageDataSWT.width == width) && (imageDataSWT.height == height);
  60         }
  61 
  62         public Graphics2D getGraphics(int width, int height) {
  63                 if ((imageDataSWT == null) || (imageDataSWT.width != width) || (imageDataSWT.height != height)) {
  64                         imageAWT = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  65                         PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
  66                         int scanlinePad = width * 3;
  67                         byte[] byteData = ((DataBufferByte) imageAWT.getRaster().getDataBuffer()).getData();
  68                         imageDataSWT = new ImageData(width, height, 24, vpPalette, scanlinePad, byteData);
  69                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  70                         setAntiAliasing(graphicsAWT);
  71                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12));
  72                         fixDPI(graphicsAWT);
  73                         return graphicsAWT;
  74                 } else {
  75                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  76                         setAntiAliasing(graphicsAWT);
  77                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12));
  78                         graphicsAWT.clearRect(0, 0, width, height);
  79                         fixDPI(graphicsAWT);
  80                         return graphicsAWT;
  81                 }
  82         }
  83         
  84         private void setAntiAliasing(Graphics2D ctx) {
  85                 Boolean antiAliasing = UIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_ANTI_ALIASING);
  86                 if (antiAliasing) {
  87                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  88                 } else {
  89                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  90                 }
  91         }
  92 
  93         private void fixDPI(Graphics2D ctx) {
  94                 AffineTransform defaultTransform = ctx.getDeviceConfiguration().getDefaultTransform();
  95                 defaultTransform.scale(X_SCALE, Y_SCALE);
  96                 ctx.setTransform(defaultTransform);
  97         }


  51 public class AwtCanvas {
  52         private ImageData imageDataSWT;
  53         private BufferedImage imageAWT;
  54 
  55         private static final double X_SCALE = (Display.getCurrent().getDPI().x) / Environment.getNormalDPI();
  56         private static final double Y_SCALE = (Display.getCurrent().getDPI().y) / Environment.getNormalDPI();
  57 
  58         public boolean hasImage(int width, int height) {
  59                 return (imageDataSWT != null) && (imageDataSWT.width == width) && (imageDataSWT.height == height);
  60         }
  61 
  62         public Graphics2D getGraphics(int width, int height) {
  63                 if ((imageDataSWT == null) || (imageDataSWT.width != width) || (imageDataSWT.height != height)) {
  64                         imageAWT = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  65                         PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
  66                         int scanlinePad = width * 3;
  67                         byte[] byteData = ((DataBufferByte) imageAWT.getRaster().getDataBuffer()).getData();
  68                         imageDataSWT = new ImageData(width, height, 24, vpPalette, scanlinePad, byteData);
  69                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  70                         setAntiAliasing(graphicsAWT);
  71                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12)); //$NON-NLS-1$
  72                         fixDPI(graphicsAWT);
  73                         return graphicsAWT;
  74                 } else {
  75                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  76                         setAntiAliasing(graphicsAWT);
  77                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12)); //$NON-NLS-1$
  78                         graphicsAWT.clearRect(0, 0, width, height);
  79                         fixDPI(graphicsAWT);
  80                         return graphicsAWT;
  81                 }
  82         }
  83         
  84         private void setAntiAliasing(Graphics2D ctx) {
  85                 Boolean antiAliasing = UIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_ANTI_ALIASING);
  86                 if (antiAliasing) {
  87                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  88                 } else {
  89                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  90                 }
  91         }
  92 
  93         private void fixDPI(Graphics2D ctx) {
  94                 AffineTransform defaultTransform = ctx.getDeviceConfiguration().getDefaultTransform();
  95                 defaultTransform.scale(X_SCALE, Y_SCALE);
  96                 ctx.setTransform(defaultTransform);
  97         }
< prev index next >