001/* ============ 002 * FXGraphics2D 003 * ============ 004 * 005 * (C)opyright 2014-2021, by Object Refinery Limited. 006 * 007 * http://www.jfree.org/fxgraphics2d/index.html 008 * 009 * The FXGraphics2D class has been developed by Object Refinery Limited for 010 * use in Orson Charts (http://www.object-refinery.com/orsoncharts) and 011 * JFreeChart (http://www.jfree.org/jfreechart). It may be useful for other 012 * code that uses the Graphics2D API provided by Java2D. 013 * 014 * Redistribution and use in source and binary forms, with or without 015 * modification, are permitted provided that the following conditions are met: 016 * - Redistributions of source code must retain the above copyright 017 * notice, this list of conditions and the following disclaimer. 018 * - Redistributions in binary form must reproduce the above copyright 019 * notice, this list of conditions and the following disclaimer in the 020 * documentation and/or other materials provided with the distribution. 021 * - Neither the name of the Object Refinery Limited nor the 022 * names of its contributors may be used to endorse or promote products 023 * derived from this software without specific prior written permission. 024 * 025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 028 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY 029 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 030 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 031 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 032 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 033 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 034 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 035 * 036 */ 037 038package org.jfree.fx; 039 040import java.awt.*; 041import java.awt.geom.AffineTransform; 042import java.awt.image.BufferedImage; 043import java.awt.image.ColorModel; 044import java.awt.image.DirectColorModel; 045import java.awt.image.VolatileImage; 046 047/** 048 * A graphics configuration for the {@link FXGraphics2D} class. 049 */ 050public class FXGraphicsConfiguration extends GraphicsConfiguration { 051 052 private GraphicsDevice device; 053 054 private final int width, height; 055 056 /** 057 * Creates a new instance. 058 * 059 * @param width the width of the bounds. 060 * @param height the height of the bounds. 061 */ 062 public FXGraphicsConfiguration(int width, int height) { 063 super(); 064 this.width = width; 065 this.height = height; 066 } 067 068 /** 069 * Returns the graphics device that this configuration is associated with. 070 * 071 * @return The graphics device (never {@code null}). 072 */ 073 @Override 074 public GraphicsDevice getDevice() { 075 if (this.device == null) { 076 this.device = new FXGraphicsDevice("FXGraphicsDevice", this); 077 } 078 return this.device; 079 } 080 081 /** 082 * Returns the color model for this configuration. 083 * 084 * @return The color model. 085 */ 086 @Override 087 public ColorModel getColorModel() { 088 return getColorModel(Transparency.TRANSLUCENT); 089 } 090 091 /** 092 * Returns the color model for the specified transparency type, or 093 * {@code null}. 094 * 095 * @param transparency the transparency type. 096 * 097 * @return A color model (possibly {@code null}). 098 */ 099 @Override 100 public ColorModel getColorModel(int transparency) { 101 if (transparency == Transparency.TRANSLUCENT) { 102 return ColorModel.getRGBdefault(); 103 } else if (transparency == Transparency.OPAQUE) { 104 return new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff); 105 } else { 106 return null; 107 } 108 } 109 110 /** 111 * Returns the default transform. 112 * 113 * @return The default transform. 114 */ 115 @Override 116 public AffineTransform getDefaultTransform() { 117 return new AffineTransform(); 118 } 119 120 /** 121 * Returns the normalizing transform. 122 * 123 * @return The normalizing transform. 124 */ 125 @Override 126 public AffineTransform getNormalizingTransform() { 127 return new AffineTransform(); 128 } 129 130 /** 131 * Returns the bounds for this configuration. 132 * 133 * @return The bounds. 134 */ 135 @Override 136 public Rectangle getBounds() { 137 return new Rectangle(this.width, this.height); 138 } 139 140 private BufferedImage img; 141 private GraphicsConfiguration gc; 142 143 /** 144 * Returns a volatile image. This method is a workaround for a 145 * ClassCastException that occurs on MacOSX when exporting a Swing UI 146 * that uses the Nimbus Look and Feel. 147 * 148 * @param width the image width. 149 * @param height the image height. 150 * @param caps the image capabilities. 151 * @param transparency the transparency. 152 * 153 * @return The volatile image. 154 * 155 * @throws AWTException if there is a problem creating the image. 156 */ 157 @Override 158 public VolatileImage createCompatibleVolatileImage(int width, int height, 159 ImageCapabilities caps, int transparency) throws AWTException { 160 if (img == null) { 161 img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 162 gc = img.createGraphics().getDeviceConfiguration(); 163 } 164 return gc.createCompatibleVolatileImage(width, height, caps, 165 transparency); 166 } 167 168}