Example usage for javax.swing JComponent paint

List of usage examples for javax.swing JComponent paint

Introduction

In this page you can find the example usage for javax.swing JComponent paint.

Prototype

public void paint(Graphics g) 

Source Link

Document

Invoked by Swing to draw components.

Usage

From source file:Utils.java

/** 
 * Renders a component into an image, which is useful for playing with the component's 
 * resultant image in special effects or transitions
 *
 * @param component The component to render
 * @return A buffered image with the rendered component. 
 *//*from w w w  . j av  a 2s.c o m*/
public static BufferedImage renderComponentToImage(JComponent component) {
    //Create the image
    BufferedImage image = createCompatibleImage(component.getWidth(), component.getHeight());

    //Render the component onto the image
    Graphics graphics = image.createGraphics();
    //      component.update(graphics);
    component.paint(graphics);
    graphics.dispose();
    return image;
}

From source file:org.jfree.graphics2d.demo.SwingUIToSVGDemo.java

@Override
public void actionPerformed(ActionEvent e) {
    JComponent c = (JComponent) getContentPane().getComponent(0);
    SVGGraphics2D g2 = new SVGGraphics2D(c.getWidth(), c.getHeight());
    c.paint(g2);
    File f = new File("SwingUIToSVGDemo.svg");
    try {//from   www.  j  a  v  a 2 s .  c  o  m
        SVGUtils.writeToSVG(f, g2.getSVGElement());
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:probe.com.model.util.SwingToImageGenerator.java

public String generateHeatMap(JComponent component) {

    BufferedImage heatMapImg = new BufferedImage((component.getWidth()), (component.getHeight()),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = heatMapImg.getGraphics();
    component.paint(g);
    return generateEncodedImg(heatMapImg);
}

From source file:probe.com.model.util.vaadintoimageutil.Convertor.java

public String toImage(int height, int width, JComponent component) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setPaint(Color.WHITE);
    component.paint(graphics);
    //        super.paint(graphics);
    byte[] imageData = null;

    try {//w  ww.j  a  v a  2 s  . c om

        imageData = in.encode(image);
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
    }

    String base64 = Base64.encodeBytes(imageData);
    base64 = "data:image/png;base64," + base64;
    return base64;

}

From source file:es.emergya.ui.base.plugins.PluggableJTabbedPane.java

@Override
public void paint(Graphics g) {
    super.paint(g);
    for (JComponent bf : botones_flotantes) {
        try {//from  w w  w  .  ja va 2 s .  c  o  m
            g.translate(bf.getBounds().x, bf.getBounds().y);
            bf.paint(g);
            g.translate(-bf.getBounds().x, -bf.getBounds().y);
        } catch (Throwable t) {
        }
    }
}

From source file:jboost.visualization.HistogramFrame.java

private void toPDF() {

    File pdf = selectPDFFile();/*  ww w  . j  av a  2s . c o  m*/

    if (pdf != null) {

        JComponent toDraw = this.jSplitPane2;

        File[] tmpFiles = new File[infoParser.maxNumIter];

        for (int i = 0; i < infoParser.maxNumIter; i++) {

            post("Printing " + infoParser.iterNoList[i] + "...");

            jList1.setSelectedIndex(i);
            jList1.scrollRectToVisible(jList1.getCellBounds(i, i));
            loadIteration(i);

            BufferedImage bimg = new BufferedImage(toDraw.getWidth(), toDraw.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bimg.createGraphics();
            toDraw.paint(g);
            g.dispose();

            // add leading zeros
            String file_idx = Integer.toString(i);
            while (file_idx.length() < 3)
                file_idx = "0" + file_idx;

            try {
                tmpFiles[i] = new File("scorevistmp" + file_idx + ".png");
                javax.imageio.ImageIO.write(bimg, "png", tmpFiles[i]);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        // restore plots
        jList1.setSelectedIndex(iter);
        loadIteration(iter);

        boolean success = true;

        // call convert to make pdf
        try {
            Process p = Runtime.getRuntime().exec("convert scorevistmp*.png " + pdf.getAbsolutePath());
            p.waitFor();
        } catch (Exception e) {
            success = false;
        }

        if (!success) {
            System.out.println("'convert' is missing. You need to have ImageMagick installed.");
            System.out.println("PDF is not generated but you can find PNG files in the current directory.");
        } else {
            /*
             * for (int i=0;i<maxNumIter-1;i++) { tmpFiles[i].delete(); }
             */

            if (infoParser.maxNumIter > 0) {
                tmpFiles[infoParser.maxNumIter - 1].renameTo(new File(pdf.getAbsolutePath() + ".png"));
            }

            JOptionPane.showMessageDialog(this, "PDF file is generated!", "Done",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

From source file:org.mwc.cmap.xyplot.views.XYPlotView.java

protected void bitmapToClipBoard(JComponent component) {
    Point size = _plotControl.getSize();
    final BufferedImage img = new BufferedImage(size.x, size.y, BufferedImage.TYPE_INT_ARGB);
    Graphics g = img.getGraphics();
    g.setColor(component.getForeground());
    g.setFont(component.getFont());//w w w . j a va2 s. co  m
    component.setSize(size.x, size.y);
    component.paint(g);
    Transferable t = new Transferable() {

        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] { DataFlavor.imageFlavor };
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            if (flavor == DataFlavor.imageFlavor)
                return true;
            return false;
        }

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (isDataFlavorSupported(flavor)) {
                return img;
            }
            return null;
        }

    };

    ClipboardOwner co = new ClipboardOwner() {

        public void lostOwnership(java.awt.datatransfer.Clipboard clipboard, Transferable contents) {
        }

    };
    java.awt.datatransfer.Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    cb.setContents(t, co);

}

From source file:org.shaman.rpg.editor.dialog.DialogVisualElement.java

public DialogVisualElement(Lookup lkp) {
    obj = lkp.lookup(DialogDataObject.class);
    assert obj != null;

    undoRedo = new UndoRedo.Manager();
    undoRedo.addChangeListener(new ChangeListener() {

        @Override/*w  ww .jav  a2 s  .c o  m*/
        public void stateChanged(ChangeEvent e) {
            obj.setModified(true);
        }
    });
    initComponents();

    contentPanel = new JPanel() {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            for (JComponent c : overlayComponents) {
                c.setBounds(this.getBounds());
                c.paint(g);
            }
        }
    };
    contentPanel.setLayout(new VerticalFlowLayout(GAP_Y * 2));
    contentPanel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (!e.isConsumed()) {
                requestFocus();
                callback.requestActive();
            }
        }
    });

    personsList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            personsListSelectionEvent();
        }
    });
    removeButton.setEnabled(false);
    Action editNames = new EditListAction();
    ListAction ls = new ListAction(personsList, editNames);
    personsList.getModel().addListDataListener(new ListDataListener() {
        @Override
        public void intervalAdded(ListDataEvent e) {
            updatePersons();
        }

        @Override
        public void intervalRemoved(ListDataEvent e) {
            updatePersons();
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            updatePersons();
        }
    });

    setupVisuals();

    contentParent = new JPanel();
    contentParent.setLayout(new BorderLayout());
    //      contentPanel.setBounds(contentPanel.getBounds());
    //      contentParent.setBounds(contentPanel.getBounds());
    contentParent.add(contentPanel);
    scrollPane.setViewportView(contentParent);
    jumpListener = new JumpListener(this);
    dropListener = new DropListener(this);
    DropTarget dt = new DropTarget(contentParent, dropListener);
    contentParent.setDropTarget(dt);

    obj.getPrimaryFile().addFileChangeListener(new FileChangeAdapter() {

        @Override
        public void fileChanged(FileEvent fe) {
            refresh();
        }

    });
    obj.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName()) && callback != null) {
                if (obj.isModified()) {
                    callback.updateTitle("<html><b>" + obj.getName() + "</b></html>");
                } else {
                    callback.updateTitle("<html>" + obj.getName() + "</html>");
                }
            }
        }
    });
}