Java Code Examples for javax.swing.JPanel#paint()

The following examples show how to use javax.swing.JPanel#paint() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Returns button with resource amounts and given text. If resources is empty then returns button
 * with just the text.
 */
public JButton getResourcesButton(final ResourceCollection resources, final String text) {
  if (resources.isEmpty()) {
    return new JButton(text);
  }
  final JPanel panel = getResourcesPanel(resources);
  panel.setOpaque(false);
  panel.add(new JLabel(text));
  panel.setSize(panel.getPreferredSize());
  panel.doLayout();
  final BufferedImage image =
      new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
  final Graphics2D g = image.createGraphics();
  panel.paint(g);
  g.dispose();
  return new JButton(new ImageIcon(image));
}
 
Example 2
Source Project: Any-Angle-Pathfinding   File: KeyToggler.java    License: The Unlicense 6 votes vote down vote up
private void takeSnapShot() {
   imgCount++;
   JPanel panel = drawCanvas;
   
   (new File("snapshots/")).mkdirs();
   BufferedImage bufImage = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);
   panel.paint(bufImage.createGraphics());
   File imageFile = new File("snapshots/"+imgCount+".png");
   try {
       imageFile.createNewFile();
       ImageIO.write(bufImage, "png", imageFile);
       System.out.println("Snapshot " + imgCount);
   } catch(Exception ex) {
       System.out.println("Unable to take snapshot: " + ex.getMessage());
   }
   
}
 
Example 3
Source Project: pumpernickel   File: PumpernickelShowcaseApp.java    License: MIT License 5 votes vote down vote up
/**
 * Create the application icon.
 * 
 * This should be called on the EDT, because it creates/renders JComponents.
 */
private static BufferedImage createAppImage() {
	BufferedImage bi = new BufferedImage(100, 100,
			BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = bi.createGraphics();

	JPanel p = new JPanel();
	p.setOpaque(false);
	QPanelUI qui = new QPanelUI();
	qui.setCalloutSize(10);
	qui.setCornerSize(10);
	qui.setCalloutType(CalloutType.BOTTOM_CENTER);
	qui.setFillColor1(Color.white);
	qui.setFillColor2(new Color(0xececec));
	qui.setShadowSize(5);
	qui.setStrokeColor(new Color(0x787878));
	p.setUI(qui);
	JSwitchButton switchButton1 = new JSwitchButton(true);
	JSwitchButton switchButton2 = new JSwitchButton(false);
	p.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();
	c.gridx = 0;
	c.gridy = 0;
	c.weightx = 1;
	c.weighty = 1;
	c.fill = GridBagConstraints.BOTH;
	c.insets = new Insets(4, 4, 4, 4);
	p.add(switchButton1, c);
	c.gridy++;
	p.add(switchButton2, c);
	Dimension d = p.getPreferredSize();
	d.width = Math.max(d.width, d.height);
	d.height = Math.max(d.width, d.height);
	p.setSize(d);
	p.getLayout().layoutContainer(p);
	p.paint(g);
	g.dispose();
	return bi;
}
 
Example 4
Source Project: OkapiBarcode   File: SaveImage.java    License: Apache License 2.0 5 votes vote down vote up
public void save(File file, JPanel panel) throws IOException {
    String extension;
    int i = file.getName().lastIndexOf('.');
    if (i > 0) {
        extension = file.getName().substring(i + 1).toLowerCase();
    } else {
        extension = "png";
    }

    switch (extension) {
        case "png":
        case "gif":
        case "jpg":
        case "bmp":
            BufferedImage bi = new BufferedImage(OkapiUI.symbol.getWidth(), OkapiUI.symbol.getHeight(), BufferedImage.TYPE_INT_ARGB);
            panel.paint(bi.getGraphics());
            ImageIO.write(bi, extension, file);
            break;
        case "svg":
            SvgRenderer svg = new SvgRenderer(new FileOutputStream(file), OkapiUI.factor, OkapiUI.paperColour, OkapiUI.inkColour);
            svg.render(OkapiUI.symbol);
            break;
        case "eps":
            PostScriptRenderer eps = new PostScriptRenderer(new FileOutputStream(file), OkapiUI.factor, OkapiUI.paperColour, OkapiUI.inkColour);
            eps.render(OkapiUI.symbol);
            break;
        default:
            System.out.println("Unsupported output format");
            break;
    }
}
 
Example 5
Source Project: swcv   File: WordCloudMenuBar.java    License: MIT License 5 votes vote down vote up
private void exportSVG(final JPanel panel, String selectedFile)
{
    // Create an instance of org.w3c.dom.Document.
    DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);

    // Create an instance of the SVG Generator.
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // Ask to render into the SVG Graphics2D implementation.
    if (panel instanceof FlexWordlePanel)
        ((FlexWordlePanel)panel).draw(svgGenerator, panel.getWidth(), panel.getHeight());
    else
        panel.paint(svgGenerator);

    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    boolean useCSS = true; // we want to use CSS style attributes
    Writer out;
    try
    {
        out = new OutputStreamWriter(new FileOutputStream(selectedFile), "UTF-8");
        svgGenerator.stream(out, useCSS);
        out.close();
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source Project: jdk8u_jdk   File: DrawTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
static private BufferedImage getScreenShot(JPanel panel) {
    BufferedImage bi = new BufferedImage(
            panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    panel.paint(bi.getGraphics());
    return bi;
}