JPanel dPanel; ... public void save() { BufferedImage bImg = new BufferedImage(dPanel.getWidth(), dPanel.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D cg = bImg.createGraphics(); dPanel.paintAll(cg); try { if (ImageIO.write(bImg, "png", new File("./output_image.png"))) { System.out.println("-- saved"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
JButton button = createButton(); JPanel panel = createPanel(); public TestContentPane() { paintButtonCheck = createRadioButton("paint button", true); paintPanelCheck = createRadioButton("paint panel", false); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(paintButtonCheck); buttonGroup.add(paintPanelCheck); add(paintButtonCheck); add(paintPanelCheck); //Hack, just prove something (realize both components) add(panel); add(button); } ... @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.translate(100, 100); if (paintButtonCheck.isSelected()) { button.paintAll(g); } else { panel.paintAll(g); } g.translate(-100, -100); }
@Override public void paintAll(Graphics g) { if ((getInvokeCondition() & INVOKE_ON_PAINT) != 0) { initialize(); } super.paintAll(g); }
panel.paintAll ( g2d ); g2d.dispose ();
public static BufferedImage componentToImageWithSwing(Component component, Rectangle region) { BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.createGraphics(); // Real render if (component.getPreferredSize().height == 0 && component.getPreferredSize().width == 0) { component.setPreferredSize(component.getSize()); } JDialog f = new JDialog(); JPanel p = new JPanel(); p.add(component); f.add(p); f.pack(); f.setLocation(-f.getWidth() - 10, -f.getHeight() -10); f.setVisible(true); p.paintAll(g); f.dispose(); // --- g.dispose(); if (region == null) { return img; } return img.getSubimage(region.x, region.y, region.width, region.height); }
protected void saveWindowAsImage(String filename) { screenshotCounter++; BufferedImage img = new BufferedImage(getContainer().getWidth(), getContainer().getHeight(), BufferedImage.TYPE_INT_ARGB); getContainer().paintAll(img.getGraphics()); File f = new File(FileUtil.getMavenTargetDir(), "screenshots" + File.separator + getClass().getName() + "_" + screenshotCounter + (filename != null ? "_" + filename : "") + ".png"); File parent = f.getParentFile(); if (!parent.exists()) { assertTrue(parent.mkdirs()); } log.info("saving window contents to " + f.getPath()); try { assertTrue(ImageIO.write(img, "png", f)); } catch (IOException ex) { throw new RuntimeException(ex); } }
protected void saveWindowAsImage(String filename) { screenshotCounter++; BufferedImage img = new BufferedImage(getContainer().getWidth(), getContainer().getHeight(), BufferedImage.TYPE_INT_ARGB); getContainer().paintAll(img.getGraphics()); File f = new File(Maven.getMavenTargetDir(), "screenshots" + File.separator + getClass().getName() + "_" + screenshotCounter + (filename != null ? "_" + filename : "") + ".png"); File parent = f.getParentFile(); if (!parent.exists()) { assertTrue(parent.mkdirs()); } log.info("saving window contents to " + f.getPath()); try { assertTrue(ImageIO.write(img, "png", f)); } catch (IOException ex) { throw new RuntimeException(ex); } }
this.selectedCollectionJPanel.add(this.rcCollectionJPanel); this.selectedCollectionJPanel.paintAll(selectedCollectionJPanel.getGraphics()); this.hideWaitCursor();
this.rcObjMdJPanel.getMetadataContentJScrollPane().getVerticalScrollBar().setValue(yScrollPos); this.selectedObjectMetadataJPanel.paintAll(selectedObjectMetadataJPanel.getGraphics());
this.rcObjMdJPanel.getMetadataContentJScrollPane().getVerticalScrollBar().setValue(yScrollPos); this.selectedObjectJPanel.paintAll(selectedObjectJPanel.getGraphics());