package jetbrains.mps.editor.contextActionsTool.pluginSolution.plugin; /*Generated by MPS */ import java.awt.Image; import javax.swing.Icon; import javax.swing.ImageIcon; import java.awt.GraphicsEnvironment; import java.awt.GraphicsDevice; import java.awt.GraphicsConfiguration; import java.awt.image.BufferedImage; import java.awt.Graphics2D; import java.awt.Component; import java.lang.reflect.Method; public class ImageUtil { public static Image iconToImage(Icon icon) { if (icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); } else { int w = icon.getIconWidth(); int h = icon.getIconHeight(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage(w, h); Graphics2D g = image.createGraphics(); icon.paintIcon(null, g, 0, 0); g.dispose(); return image; } } public static Image component2image(Component comp) { int w = comp.getWidth(); int h = comp.getHeight(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage(w, h); Graphics2D g = image.createGraphics(); comp.paintAll(g); g.dispose(); return image; } public static ImageIcon icon2imageIcon(Icon icon) { if (icon instanceof ImageIcon) { return ((ImageIcon) icon); } if (eq_9b5dri_a0b0f(icon.getClass().getName(), "com.intellij.openapi.util.IconLoader$CachedImageIcon")) { try { Method getRealIcon = icon.getClass().getDeclaredMethod("getRealIcon"); getRealIcon.setAccessible(true); Object realIcon = getRealIcon.invoke(icon); if (realIcon instanceof ImageIcon) { return ((ImageIcon) realIcon); } } catch (Exception ex) { } } return new ImageIcon(iconToImage(icon)); } private static boolean eq_9b5dri_a0b0f(Object a, Object b) { return (a != null ? a.equals(b) : a == b); } }