< prev index next >

src/java.desktop/share/classes/sun/awt/image/BufferedImageGraphicsConfig.java

Print this page




  28 import java.awt.AWTException;
  29 import java.awt.Component;
  30 import java.awt.Graphics2D;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.ImageCapabilities;
  34 import java.awt.Rectangle;
  35 import java.awt.Transparency;
  36 import java.awt.geom.AffineTransform;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.ColorModel;
  39 import java.awt.image.DirectColorModel;
  40 import java.awt.image.Raster;
  41 import java.awt.image.VolatileImage;
  42 import java.awt.image.WritableRaster;
  43 
  44 public class BufferedImageGraphicsConfig
  45     extends GraphicsConfiguration
  46 {
  47     private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
  48     private static BufferedImageGraphicsConfig configs[] =
  49         new BufferedImageGraphicsConfig[numconfigs];
  50 
  51     public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {







  52         BufferedImageGraphicsConfig ret;
  53         int type = bImg.getType();

  54         if (type > 0 && type < numconfigs) {
  55             ret = configs[type];
  56             if (ret != null) {
  57                 return ret;
  58             }
  59         }
  60         ret = new BufferedImageGraphicsConfig(bImg, null);
  61         if (type > 0 && type < numconfigs) {
  62             configs[type] = ret;
  63         }
  64         return ret;
  65     }
  66 
  67     GraphicsDevice gd;
  68     ColorModel model;
  69     Raster raster;
  70     int width, height;


  71 
  72     public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {






  73         if (comp == null) {
  74             this.gd = new BufferedImageDevice(this);
  75         } else {
  76             Graphics2D g2d = (Graphics2D)comp.getGraphics();
  77             this.gd = g2d.getDeviceConfiguration().getDevice();
  78         }
  79         this.model = bufImg.getColorModel();
  80         this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
  81         this.width = bufImg.getWidth();
  82         this.height = bufImg.getHeight();


  83     }
  84 
  85     /**
  86      * Return the graphics device associated with this configuration.
  87      */
  88     public GraphicsDevice getDevice() {
  89         return gd;
  90     }
  91 
  92     /**
  93      * Returns a BufferedImage with channel layout and color model
  94      * compatible with this graphics configuration.  This method
  95      * has nothing to do with memory-mapping
  96      * a device.  This BufferedImage has
  97      * a layout and color model
  98      * that is closest to this native device configuration and thus
  99      * can be optimally blitted to this device.
 100      */
 101     public BufferedImage createCompatibleImage(int width, int height) {
 102         WritableRaster wr = raster.createCompatibleWritableRaster(width, height);


 124             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 125         case Transparency.BITMASK:
 126             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 127         case Transparency.TRANSLUCENT:
 128             return ColorModel.getRGBdefault();
 129         default:
 130             return null;
 131         }
 132     }
 133 
 134     /**
 135      * Returns the default Transform for this configuration.  This
 136      * Transform is typically the Identity transform for most normal
 137      * screens.  Device coordinates for screen and printer devices will
 138      * have the origin in the upper left-hand corner of the target region of
 139      * the device, with X coordinates
 140      * increasing to the right and Y coordinates increasing downwards.
 141      * For image buffers, this Transform will be the Identity transform.
 142      */
 143     public AffineTransform getDefaultTransform() {
 144         return new AffineTransform();
 145     }
 146 
 147     /**
 148      *
 149      * Returns a Transform that can be composed with the default Transform
 150      * of a Graphics2D so that 72 units in user space will equal 1 inch
 151      * in device space.
 152      * Given a Graphics2D, g, one can reset the transformation to create
 153      * such a mapping by using the following pseudocode:
 154      * <pre>
 155      *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
 156      *
 157      *      g.setTransform(gc.getDefaultTransform());
 158      *      g.transform(gc.getNormalizingTransform());
 159      * </pre>
 160      * Note that sometimes this Transform will be identity (e.g. for
 161      * printers or metafile output) and that this Transform is only
 162      * as accurate as the information supplied by the underlying system.
 163      * For image buffers, this Transform will be the Identity transform,
 164      * since there is no valid distance measurement.


  28 import java.awt.AWTException;
  29 import java.awt.Component;
  30 import java.awt.Graphics2D;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.ImageCapabilities;
  34 import java.awt.Rectangle;
  35 import java.awt.Transparency;
  36 import java.awt.geom.AffineTransform;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.ColorModel;
  39 import java.awt.image.DirectColorModel;
  40 import java.awt.image.Raster;
  41 import java.awt.image.VolatileImage;
  42 import java.awt.image.WritableRaster;
  43 
  44 public class BufferedImageGraphicsConfig
  45     extends GraphicsConfiguration
  46 {
  47     private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
  48     private static BufferedImageGraphicsConfig configs[][] =
  49         new BufferedImageGraphicsConfig[numconfigs][2];
  50 
  51     public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {
  52         return getConfig(bImg, 1, 1);
  53     }
  54 
  55     public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg,
  56                                                         double scaleX,
  57                                                         double scaleY)
  58     {
  59         BufferedImageGraphicsConfig ret;
  60         int type = bImg.getType();
  61         int index = (scaleX == 1 && scaleY == 1) ? 0 : 1;
  62         if (type > 0 && type < numconfigs) {
  63             ret = configs[type][index];
  64             if (ret != null && ret.scaleX == scaleX && ret.scaleY == scaleY) {
  65                 return ret;
  66             }
  67         }
  68         ret = new BufferedImageGraphicsConfig(bImg, null, scaleX, scaleY);
  69         if (type > 0 && type < numconfigs) {
  70             configs[type][index] = ret;
  71         }
  72         return ret;
  73     }
  74 
  75     GraphicsDevice gd;
  76     ColorModel model;
  77     Raster raster;
  78     int width, height;
  79     private final double scaleX;
  80     private final double scaleY;
  81 
  82     public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {
  83         this(bufImg, comp, 1, 1);
  84     }
  85 
  86     public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp,
  87                                        double scaleX, double scaleY)
  88     {
  89         if (comp == null) {
  90             this.gd = new BufferedImageDevice(this);
  91         } else {
  92             Graphics2D g2d = (Graphics2D)comp.getGraphics();
  93             this.gd = g2d.getDeviceConfiguration().getDevice();
  94         }
  95         this.model = bufImg.getColorModel();
  96         this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
  97         this.width = bufImg.getWidth();
  98         this.height = bufImg.getHeight();
  99         this.scaleX = scaleX;
 100         this.scaleY = scaleY;
 101     }
 102 
 103     /**
 104      * Return the graphics device associated with this configuration.
 105      */
 106     public GraphicsDevice getDevice() {
 107         return gd;
 108     }
 109 
 110     /**
 111      * Returns a BufferedImage with channel layout and color model
 112      * compatible with this graphics configuration.  This method
 113      * has nothing to do with memory-mapping
 114      * a device.  This BufferedImage has
 115      * a layout and color model
 116      * that is closest to this native device configuration and thus
 117      * can be optimally blitted to this device.
 118      */
 119     public BufferedImage createCompatibleImage(int width, int height) {
 120         WritableRaster wr = raster.createCompatibleWritableRaster(width, height);


 142             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 143         case Transparency.BITMASK:
 144             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 145         case Transparency.TRANSLUCENT:
 146             return ColorModel.getRGBdefault();
 147         default:
 148             return null;
 149         }
 150     }
 151 
 152     /**
 153      * Returns the default Transform for this configuration.  This
 154      * Transform is typically the Identity transform for most normal
 155      * screens.  Device coordinates for screen and printer devices will
 156      * have the origin in the upper left-hand corner of the target region of
 157      * the device, with X coordinates
 158      * increasing to the right and Y coordinates increasing downwards.
 159      * For image buffers, this Transform will be the Identity transform.
 160      */
 161     public AffineTransform getDefaultTransform() {
 162         return AffineTransform.getScaleInstance(scaleX, scaleY);
 163     }
 164 
 165     /**
 166      *
 167      * Returns a Transform that can be composed with the default Transform
 168      * of a Graphics2D so that 72 units in user space will equal 1 inch
 169      * in device space.
 170      * Given a Graphics2D, g, one can reset the transformation to create
 171      * such a mapping by using the following pseudocode:
 172      * <pre>
 173      *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
 174      *
 175      *      g.setTransform(gc.getDefaultTransform());
 176      *      g.transform(gc.getNormalizingTransform());
 177      * </pre>
 178      * Note that sometimes this Transform will be identity (e.g. for
 179      * printers or metafile output) and that this Transform is only
 180      * as accurate as the information supplied by the underlying system.
 181      * For image buffers, this Transform will be the Identity transform,
 182      * since there is no valid distance measurement.
< prev index next >