use of java.awt.GraphicsEnvironment in project knime-core by knime.
the class DecTreeToImageNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
exec.setMessage("Decision Tree To Image: Loading model...");
PMMLPortObject port = (PMMLPortObject) inPorts[0];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
if (models.isEmpty()) {
String msg = "Decision Tree evaluation failed: " + "No tree model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
port.initializeModelTranslator(trans);
m_decTree = trans.getDecisionTree();
m_decTree.resetColorInformation();
String colorColumn = null;
if (null != inPorts[1]) {
BufferedDataTable inData = (BufferedDataTable) inPorts[1];
// get column with color information
for (DataColumnSpec s : inData.getDataTableSpec()) {
if (s.getColorHandler() != null) {
colorColumn = s.getName();
break;
}
}
m_decTree.setColorColumn(colorColumn);
for (DataRow thisRow : inData) {
m_decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
}
}
// create PNG via streamed string
ByteArrayOutputStream os = new ByteArrayOutputStream();
int width = m_settings.getWidth();
int height = m_settings.getHeight();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
BufferedImage image = null;
if (env.isHeadlessInstance()) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
// create compatible image for better performance
GraphicsConfiguration gfxConf = env.getDefaultScreenDevice().getDefaultConfiguration();
// image = gfxConf.createCompatibleImage(width, height);
// with binary transparency
image = gfxConf.createCompatibleImage(width, height, Transparency.BITMASK);
// with transparency
// image = gfxConf.createCompatibleImage(width, height,
// Transparency.TRANSLUCENT);
}
Graphics2D g = (Graphics2D) image.getGraphics();
DecisionTreeNode root = null != getDecisionTree() ? getDecisionTree().getRootNode() : null;
DecTreeGraphView graph = new DecTreeToImageGraphView(root, colorColumn, m_settings);
// draw graph
graph.getView().paint(g);
// write png
ImageIO.write(image, "png", os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
m_imageContent = new PNGImageContent(is);
ImagePortObjectSpec outSpec = new ImagePortObjectSpec(PNGImageContent.TYPE);
// return image object
PortObject po = new ImagePortObject(m_imageContent, outSpec);
return new PortObject[] { po };
}
use of java.awt.GraphicsEnvironment in project Lucee by lucee.
the class Image method toBufferedImage.
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(java.awt.Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
use of java.awt.GraphicsEnvironment in project Lucee by lucee.
the class FontUtil method getAvailableFonts.
private static Array getAvailableFonts(boolean duplicate) {
synchronized (sync) {
if (fonts == null) {
fonts = new ArrayImpl();
GraphicsEnvironment graphicsEvn = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] availableFonts = graphicsEvn.getAllFonts();
for (int i = 0; i < availableFonts.length; i++) {
fonts.appendEL(availableFonts[i]);
}
}
if (!duplicate)
return fonts;
return (Array) Duplicator.duplicate(fonts, false);
}
}
Aggregations