Example usage for javax.swing JPanel getGraphics

List of usage examples for javax.swing JPanel getGraphics

Introduction

In this page you can find the example usage for javax.swing JPanel getGraphics.

Prototype

@BeanProperty(bound = false)
public Graphics getGraphics() 

Source Link

Document

Returns this component's graphics context, which lets you draw on a component.

Usage

From source file:Main.java

public static void main(String args[]) {
    JPanel JMainPanel = new JPanel(new BorderLayout());
    JPanel jp = new JPanel();
    JComboBox combo = new JComboBox(new String[] { "Item1", "Item2", "Item3" });
    JPanel jImage = new JPanel();
    JFrame jf = new JFrame();

    jp.add(combo);//from w w  w  . j a  v  a  2s  . c o  m
    JMainPanel.add(jp, BorderLayout.WEST);
    JMainPanel.add(jImage, BorderLayout.CENTER);
    jp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.ALT_DOWN_MASK), "screenshot");
    jp.getActionMap().put("screenshot", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            final BufferedImage bf = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    jf.getRootPane().paint(bf.getGraphics());
                    jImage.getGraphics().drawImage(bf, 0, 0, jImage);
                }
            });
        }
    });
    jf.getContentPane().add(JMainPanel);
    jf.setSize(500, 500);
    jf.setVisible(true);
}