下面列出了java.awt.Graphics2D#getDeviceConfiguration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static CachedImageKey create(Graphics2D g) {
final AffineTransform tx = g.getTransform();
final int txType = tx.getType();
final double scale;
if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
{
scale = tx.getScaleX();
} else {
scale = 1.0;
}
GraphicsConfiguration gconf = g.getDeviceConfiguration();
/* Always use the same transparency mode for the cached images, so we don't end up with
one set of cached images for each encountered mode. The TRANSLUCENT mode is the most
generic one; presumably it should paint OK onto less capable surfaces. */
ColorModel colorModel = gconf.getColorModel(Transparency.TRANSLUCENT);
return new CachedImageKey(colorModel, scale);
}
private void drawBackingStoreImage(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
GraphicsConfiguration gc = g2d.getDeviceConfiguration();
if (vImg == null ||
vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
/* Create a new volatile image */
vImg = createVolatileImage(PANEL_WIDTH, PANEL_HEIGHT / 3);
}
Graphics vImgGraphics = vImg.createGraphics();
vImgGraphics.setColor(Color.WHITE);
vImgGraphics.fillRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT / 3);
drawInfo(vImgGraphics,
PANEL_X,
PANEL_Y,
"Backbuffer",
Color.MAGENTA);
g.drawImage(vImg, 0, PANEL_Y * 2, this);
}
private boolean isBitonal(Graphics2D graphics)
{
GraphicsConfiguration deviceConfiguration = graphics.getDeviceConfiguration();
if (deviceConfiguration == null)
{
return false;
}
GraphicsDevice device = deviceConfiguration.getDevice();
if (device == null)
{
return false;
}
DisplayMode displayMode = device.getDisplayMode();
if (displayMode == null)
{
return false;
}
return displayMode.getBitDepth() == 1;
}
private boolean isBitonal(Graphics2D graphics)
{
GraphicsConfiguration deviceConfiguration = graphics.getDeviceConfiguration();
if (deviceConfiguration == null)
{
return false;
}
GraphicsDevice device = deviceConfiguration.getDevice();
if (device == null)
{
return false;
}
DisplayMode displayMode = device.getDisplayMode();
if (displayMode == null)
{
return false;
}
return displayMode.getBitDepth() == 1;
}
/**
* @param width The width to create.
* @param height The height to create.
* @param transparency A constant from {@link Transparency}.
* @return A new {@link Img} of the given size.
*/
public static Img create(int width, int height, int transparency) {
Graphics2D g2d = GraphicsUtilities.getGraphics();
GraphicsConfiguration gc = g2d.getDeviceConfiguration();
Img img = create(gc, width, height, transparency);
g2d.dispose();
return img;
}
final public static BufferedImage resizeImage(BufferedImage img, int width, int height) {
Graphics2D gin = img.createGraphics();
GraphicsConfiguration gc = gin.getDeviceConfiguration();
gin.dispose();
BufferedImage dst = gc.createCompatibleImage(width, height, BufferedImage.BITMASK);
Graphics2D gr = dst.createGraphics();
gr.setComposite(AlphaComposite.Src);
AffineTransform at = AffineTransform.getScaleInstance((double)width/img.getWidth(), (double)height/img.getHeight());
gr.drawRenderedImage(img,at);
return dst;
}
@Override
public void paint(Graphics2D g, Object o, int width, int height) {
GraphicsConfiguration gc = g.getDeviceConfiguration();
if (cache == null) { // called for the first time or was invalidated
createAndUseCachedImage(g, gc, width, height);
return;
}
VolatileImage vi = cache.get();
if (vi == null) { // soft reference collected
createAndUseCachedImage(g, gc, width, height);
return;
}
if (vi.getWidth() != width || vi.getHeight() != height) { // size changed
vi.flush();
createAndUseCachedImage(g, gc, width, height);
return;
}
// at this point we have a cached image with the right size
int safetyCounter = 0; // to be 100% sure that this is not an infinite loop
do {
int valCode = vi.validate(gc); // check before rendering
if (valCode == VolatileImage.IMAGE_OK) {
// simplest case, just use the image
g.drawImage(vi, 0, 0, null);
} else if (valCode == VolatileImage.IMAGE_RESTORED) {
// memory loss, but the the image object can be reused
renderAndUseCachedImage(vi, g, width, height);
} else if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
// surface incompatibility: the image has to be recreated
vi.flush();
vi = createAndUseCachedImage(g, gc, width, height);
}
} while (vi.contentsLost() && safetyCounter++ < 3); // check after rendering
}