Search in sources :

Example 31 with BufferedImage

use of java.awt.image.BufferedImage in project antlr4 by antlr.

the class TreeViewer method generatePNGFile.

private static void generatePNGFile(TreeViewer viewer, JFrame dialog) {
    BufferedImage bi = new BufferedImage(viewer.getSize().width, viewer.getSize().height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    viewer.paint(g);
    g.dispose();
    try {
        File suggestedFile = generateNonExistingPngFile();
        JFileChooser fileChooser = new JFileChooserConfirmOverwrite();
        fileChooser.setCurrentDirectory(suggestedFile.getParentFile());
        fileChooser.setSelectedFile(suggestedFile);
        FileFilter pngFilter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    return pathname.getName().toLowerCase().endsWith(".png");
                }
                return true;
            }

            @Override
            public String getDescription() {
                return "PNG Files (*.png)";
            }
        };
        fileChooser.addChoosableFileFilter(pngFilter);
        fileChooser.setFileFilter(pngFilter);
        int returnValue = fileChooser.showSaveDialog(dialog);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File pngFile = fileChooser.getSelectedFile();
            ImageIO.write(bi, "png", pngFile);
            try {
                // Try to open the parent folder using the OS' native file manager.
                Desktop.getDesktop().open(pngFile.getParentFile());
            } catch (Exception ex) {
                // We could not launch the file manager: just show a popup that we
                // succeeded in saving the PNG file.
                JOptionPane.showMessageDialog(dialog, "Saved PNG to: " + pngFile.getAbsolutePath());
                ex.printStackTrace();
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(dialog, "Could not export to PNG: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}
Also used : FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) PrintException(javax.print.PrintException)

Example 32 with BufferedImage

use of java.awt.image.BufferedImage in project javatari by ppeccin.

the class JComboBoxNim method loadImages.

private void loadImages() {
    try {
        BufferedImage image = SwingHelper.loadAsCompatibleTranslucentImage("org/javatari/pc/room/settings/images/Joystick.png");
        joystickIcon = new ImageIcon(image);
        joystickIconGrayed = new ImageIcon(GrayFilter.createDisabledImage(image));
        image = SwingHelper.loadAsCompatibleTranslucentImage("org/javatari/pc/room/settings/images/Paddle.png");
        paddleIcon = new ImageIcon(image);
        paddleIconGrayed = new ImageIcon(GrayFilter.createDisabledImage(image));
    } catch (IOException ex) {
        System.out.println("SettingsDialog: unable to load images\n" + ex);
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 33 with BufferedImage

use of java.awt.image.BufferedImage in project javatari by ppeccin.

the class SwingHelper method asCompatibleImage.

public static BufferedImage asCompatibleImage(Image img) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null), img.getHeight(null));
    Graphics2D gc = ret.createGraphics();
    gc.drawImage(img, 0, 0, null);
    gc.dispose();
    return ret;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 34 with BufferedImage

use of java.awt.image.BufferedImage in project javatari by ppeccin.

the class SwingHelper method asCompatibleImage.

public static BufferedImage asCompatibleImage(Image img, int transparency) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null), img.getHeight(null), transparency);
    Graphics2D gc = ret.createGraphics();
    gc.setComposite(AlphaComposite.Src);
    gc.drawImage(img, 0, 0, null);
    gc.dispose();
    return ret;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 35 with BufferedImage

use of java.awt.image.BufferedImage in project antlrworks by antlr.

the class SDGenerator method renderRuleToBitmapFile.

public void renderRuleToBitmapFile(String ruleName, String imageFormat, String file) throws Exception {
    GGraph graph = createGraph(ruleName);
    int width = (int) (graph.getWidth() + 1);
    int height = (int) (graph.getHeight() + 1);
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    context.setEngine(new GEngineGraphics());
    context.setGraphics2D(g2d);
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, width, height);
    graph.draw();
    g2d.dispose();
    ImageIO.write(image, imageFormat, new File(file));
}
Also used : GGraph(org.antlr.works.visualization.graphics.graph.GGraph) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1702 Graphics2D (java.awt.Graphics2D)376 IOException (java.io.IOException)220 File (java.io.File)195 FunctionException (lucee.runtime.exp.FunctionException)122 Graphics (java.awt.Graphics)104 ByteArrayOutputStream (java.io.ByteArrayOutputStream)89 Color (java.awt.Color)88 ByteArrayInputStream (java.io.ByteArrayInputStream)86 Test (org.junit.Test)81 WritableRaster (java.awt.image.WritableRaster)75 Point (java.awt.Point)71 Rectangle (java.awt.Rectangle)68 AffineTransform (java.awt.geom.AffineTransform)57 Image (java.awt.Image)56 InputStream (java.io.InputStream)51 Dimension (java.awt.Dimension)50 ImageIcon (javax.swing.ImageIcon)50 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)46 ImageWriter (javax.imageio.ImageWriter)45