下面列出了javax.swing.JPanel#paint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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));
}
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());
}
}
/**
* 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;
}
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;
}
}
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);
}
}
static private BufferedImage getScreenShot(JPanel panel) {
BufferedImage bi = new BufferedImage(
panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
panel.paint(bi.getGraphics());
return bi;
}