下面列出了javax.swing.SwingUtilities#paintComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
JFrame window = new JFrame();
HtmlPanel panel = new HtmlPanel();
window.getContentPane().add(panel);
window.setSize(600, 400);
window.setVisible(true);
new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
.navigate("http://jobs.zhaopin.com/377931819252715.htm?ssidkey=y&ss=201&ff=03");
System.out.println("10");
Thread.sleep(10000);
BufferedImage image = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
// paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(), panel,
new JPanel(), 0, 0, image.getWidth(), image.getHeight());
// save the image to file
ImageIO.write((RenderedImage) image, "png", new File("html3.png"));
System.out.println("www");
}
public static void main(String[] args) throws Exception {
//load the webpage into the editor
//JEditorPane ed = new JEditorPane(new URL("http://www.google.com"));
JEditorPane ed = new JEditorPane(new URL("http://www.baidu.com"));
System.out.println("10");
Thread.sleep(10000);
ed.setSize(1000,1000);
//create a new image
BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
BufferedImage.TYPE_INT_ARGB);
//paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(),
ed,
new JPanel(),
0, 0, image.getWidth(), image.getHeight());
//save the image to file
ImageIO.write((RenderedImage)image, "png", new File("html1.png"));
System.out.println("ok");
}
/**
* Set the cell renderer we will proxy.
*/
public void setComponent (Component jc, JComponent owner) {
Dimension dd = jc.getPreferredSize();
Rectangle currentScreenBounds = Utilities.getUsableScreenBounds();
// get some reasonable limit for the width
int width = Math.min(dd.width, 2 * currentScreenBounds.width);
int height = Math.min(dd.height + 2, 2 * currentScreenBounds.height);
Image nue = !Utilities.isMac() ? owner.createVolatileImage(width, height) :
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = nue.getGraphics();
g.setColor (bg);
g.fillRect (0, 0, width, dd.height + 2);
if( jc instanceof Container && !jc.isValid() ) {
//#214739
jc.setSize( width, dd.height );
jc.doLayout();
}
SwingUtilities.paintComponent(g, jc, this, 0, 0, width, dd.height + 2);
g.dispose();
setImage (nue);
}
public void paintComponent(Graphics g) {
//Hack for issue 38132 - Beans are set via TTVBridge as a package
//private property on the parent PropertyPanel (if there is one).
//FindBeans() will locate the beans either in the model or as a
//property of the PropertyPanel (if an esoteric undocumented client property
//is set on the PropertyPanel). RendererFactory will set the env
//value (if there is an env) to the value of ReusablePropertyEnv.NODE
//(a performance hack to avoid creating 1 property env for each property
//painted each time we paint). Cool, huh?
reusableEnv.setNode(EditorPropertyDisplayer.findBeans(this));
JComponent comp = getRenderer(this);
prepareRenderer(comp);
comp.setBounds(0, 0, getWidth(), getHeight());
if (comp instanceof InplaceEditor) {
Component inner = findInnermostRenderer(comp);
SwingUtilities.paintComponent(g, comp, this, 0, 0, getWidth(), getHeight());
removeAll();
return;
}
comp.paint(g);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Insets borderInsets = border.getBorderInsets(c);
Insets insets = getBorderInsets(c);
int temp = (insets.top - borderInsets.top) / 2;
border.paintBorder(c, g, x, y + temp, width, height - temp);
Dimension size = comp.getPreferredSize();
rect = new Rectangle(offset, 0, size.width, size.height);
SwingUtilities.paintComponent(g, comp, (Container) c, rect);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Insets borderInsets = border.getBorderInsets(c);
Insets insets = getBorderInsets(c);
int temp = (insets.top - borderInsets.top) / 2;
border.paintBorder(c, g, x, y + temp, width, height - temp);
Dimension size = comp.getPreferredSize();
rect = new Rectangle(offset, 0, size.width, size.height);
SwingUtilities.paintComponent(g, comp, (Container)c, rect);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
SwingUtilities.paintComponent(
g, check, (Container) c, x, y, getIconWidth(), getIconHeight());
}