Example usage for java.awt Component paint

List of usage examples for java.awt Component paint

Introduction

In this page you can find the example usage for java.awt Component paint.

Prototype

public void paint(Graphics g) 

Source Link

Document

Paints this component.

Usage

From source file:Main.java

public static Image getImage(Component c) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    // Create an image that supports transparent pixels
    BufferedImage bImage = gc.createCompatibleImage(c.getWidth(), c.getHeight(), Transparency.BITMASK);

    /*/*w ww  .  j av a 2s .  co m*/
     * And now this is how we get an image of the component
     */
    Graphics2D g = bImage.createGraphics();

    // Then use the current component we're in and call paint on this
    // graphics object
    c.paint(g);
    return bImage;
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

public static void generatePDF(Component component, File file) {
    int width = component.getWidth();
    int height = component.getHeight();
    try {/*from  ww  w . jav  a  2 s  .c o  m*/
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        component.paint(g2);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.unc.LCCC.caBIG.DWD.javaCode.visualization.SetUpPlotWindow.java

public static void saveComponentAsJPEG(Component myComponent, String filename) {
    Dimension size = myComponent.getSize();
    BufferedImage myImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = myImage.createGraphics();
    myComponent.paint(g2);
    try {//  w  ww . java 2  s  .c o  m
        OutputStream out = new FileOutputStream(filename);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(myImage);
        out.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

/**
 * Takes a snapshot of the target component.
 *
 * @param component the component to draw
 * @param usePrint  whether <tt>print()</tt> or <tt>paint()</tt> is used to grab the snapshot
 * @return a Graphics compatible image of the component
 *///from  ww w  .ja  va 2 s.c o  m
public static Image takeSnapshot(Component component, boolean usePrint) {
    BufferedImage image = null;
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = genv.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    if (gc.getColorModel().hasAlpha()) {
        image = gc.createCompatibleImage((int) component.getSize().getWidth(),
                (int) component.getSize().getHeight());
    } else {
        image = new BufferedImage((int) component.getSize().getWidth(), (int) component.getSize().getHeight(),
                BufferedImage.TYPE_INT_ARGB);
    }

    Graphics g = image.getGraphics();
    if (usePrint) {
        component.print(g);
    } else {
        component.paint(g);
    }
    g.dispose();

    return image;
}

From source file:UserInterface.GarbageCollectorRole.GarbageCollectorWorkAreaJPanel.java

public static BufferedImage getScreenShot(Component component) {
    BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    component.paint(image.getGraphics());
    return image;
}

From source file:edu.ku.brc.specify.utilapps.sp5utils.Sp5Forms.java

public void generate() {
    BufferedImage bufImage = null;
    if (formFrame != null) {
        Component topComp = formFrame.getContentPane();

        Rectangle rect = topComp.getBounds();
        if (rect.width < 1 || rect.height < 1) {
            System.err.println("Can't create image. " + selectedForm.getTitle());
        } else {/*w w  w  .j  a v  a  2s.  c  om*/
            bufImage = new BufferedImage(rect.width, rect.height, (BufferedImage.TYPE_INT_ARGB));
            Graphics2D g2 = bufImage.createGraphics();
            topComp.paint(g2);
            g2.dispose();
        }
    }

    if (formIndex < forms.size()) {
        selectedForm = forms.get(formIndex);

        showForm();

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        formFrame.pack();
                        formFrame.repaint();
                    }
                });
            }
        });
    }

    File dir = new File("conversions" + File.separator + namePair.second + "_6" + File.separator + "forms");
    if (!dir.exists()) {
        dir.mkdirs();
    }

    if (bufImage != null) {
        String fName = dir.getAbsolutePath() + File.separator + formName + ".png";
        try {
            File imgFile = new File(fName);
            ImageIO.write(bufImage, "PNG", imgFile);

        } catch (Exception e) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ERDVisualizer.class, e);
            e.printStackTrace();
        }
    }

    if (formIndex >= forms.size()) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if (formFrame != null)
                            formFrame.setVisible(false);
                        createIndex();
                        UIRegistry.showLocalizedMsg("Done");
                    }
                });
            }
        });
    }

    if (selectedForm != null) {
        formName = selectedForm.getFileName();
    }
}

From source file:FirstStatMain.java

private static BufferedImage getScreenShot(Component com) {
    BufferedImage image = new BufferedImage(com.getWidth(), com.getHeight(), BufferedImage.TYPE_INT_RGB);
    com.paint(image.getGraphics());
    return image;
}

From source file:edu.ku.brc.ui.UIRegistry.java

/**
 * Writes a string message into the BufferedImage on GlassPane and sets the main component's visibility to false and
 * shows the GlassPane./* w  w  w.j  a  v a  2  s .co m*/
 * @param msg the message
 * @param pointSize the Font point size for the message to be writen in
 */
public static GhostGlassPane writeGlassPaneMsg(final String msg, final int pointSize) {
    GhostGlassPane glassPane = getGlassPane();
    if (glassPane != null) {
        glassPane.finishDnD();
    }

    glassPane.setMaskingEvents(true);

    Component mainComp = get(MAINPANE);
    if (mainComp != null && glassPane != null) {
        JFrame frame = (JFrame) get(FRAME);
        frameRect = frame.getBounds();

        int y = 0;
        JMenuBar menuBar = null;
        Dimension size = mainComp.getSize();
        if (UIHelper.getOSType() != UIHelper.OSTYPE.MacOSX) {
            menuBar = frame.getJMenuBar();
            size.height += menuBar.getSize().height;
            y += menuBar.getSize().height;
        }
        BufferedImage buffer = getGlassPaneBufferedImage(size.width, size.height);
        Graphics2D g2 = buffer.createGraphics();
        if (menuBar != null) {
            menuBar.paint(g2);
        }
        g2.translate(0, y);
        mainComp.paint(g2);
        g2.translate(0, -y);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(new Color(255, 255, 255, 128));
        g2.fillRect(0, 0, size.width, size.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(msg);
        int th = fm.getHeight();
        int tx = (size.width - tw) / 2;
        int ty = (size.height - th) / 2;

        int expand = 20;
        int arc = expand * 2;
        g2.setColor(Color.WHITE);
        g2.fillRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.DARK_GRAY);
        g2.drawRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.BLACK);
        g2.drawString(msg, tx, ty);
        g2.dispose();

        glassPane.setImage(buffer);
        glassPane.setPoint(new Point(0, 0), GhostGlassPane.ImagePaintMode.ABSOLUTE);
        glassPane.setOffset(new Point(0, 0));

        glassPane.setVisible(true);
        mainComp.setVisible(false);

        //Using paintImmediately fixes problems with glass pane not showing, such as for workbench saves initialed
        //during workbench or app shutdown. Don't know if there is a better way to fix it.
        //glassPane.repaint();
        glassPane.paintImmediately(glassPane.getBounds());
        showingGlassPane = true;
    }

    return glassPane;
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private static void paintComponents(Container c, Graphics g) {
    if (!c.isShowing())
        return;//from  ww w .  ja  va2 s  .c  o  m

    final int ncomponents = c.getComponentCount();
    final Rectangle clip = g.getClipBounds();

    int i = ncomponents - 1;
    while (i >= 0) {
        final Component component[] = c.getComponents();
        final Component comp = component[i];
        if (comp == null || !comp.isVisible())
            continue;
        final Rectangle bounds = comp.getBounds();
        Rectangle cr;
        if (clip == null)
            cr = new Rectangle(bounds);
        else
            cr = bounds.intersection(clip);
        if (cr.isEmpty())
            continue;

        final Graphics cg = g.create();
        cg.setClip(cr);
        cg.translate(bounds.x, bounds.y);
        try {
            comp.paint(cg);
        } catch (Throwable e) {
            //
        }

        cg.dispose();
        i--;
    }
}

From source file:org.jcurl.core.swing.TrajectoryDisplayTest.java

public void _testThroughPut() {
    final int dt = 5000;
    final Graphics g = new BufferedImage(1024 * 2, 768 * 2, BufferedImage.TYPE_INT_ARGB).getGraphics();
    final TrajectorySet p = initHammy(null);
    final int frames = showTrajectoryDisplay(p, FixpointZoomer.HOG2HACK, dt, new TimeRunnable() {
        @Override/*from w ww.j av a 2s.  c o  m*/
        public void run(final double t) throws InterruptedException {
            throw new UnsupportedOperationException();
        }

        @Override
        public void run(final double t, final Component jp) throws InterruptedException {
            jp.paint(g);
        }
    });
    if (frame != null)
        System.out.println(
                getClass().getName() + " frequency: " + frames * 1000L / (double) dt + " frames per second");
    // System.out.println(frames + " computations took " + dt
    // + " millis, i.e. " + frames * 1000L / dt + " per second.");
}