我试了一下,如果jcomponent是可见的就能保存上,如果不可见保存的图片就是黑的。
这应该是因为paintAll()方法中判断了组件是否显示了出来 public void paintAll(Graphics g) {
if (isShowing()) {
GraphicsCallback.PeerPaintCallback.getInstance().
runOneComponent(this, new Rectangle(0, 0, width, height),
g, g.getClip(),
GraphicsCallback.LIGHTWEIGHTS |
GraphicsCallback.HEAVYWEIGHTS);
}
}
我试了把paintAll(g); 改成 paint(g); 就可以在jcomponent不可见的情况下也保存成功。
不知道你遇到的情况是怎样的。
这应该是因为paintAll()方法中判断了组件是否显示了出来 public void paintAll(Graphics g) {
if (isShowing()) {
GraphicsCallback.PeerPaintCallback.getInstance().
runOneComponent(this, new Rectangle(0, 0, width, height),
g, g.getClip(),
GraphicsCallback.LIGHTWEIGHTS |
GraphicsCallback.HEAVYWEIGHTS);
}
}
我试了把paintAll(g); 改成 paint(g); 就可以在jcomponent不可见的情况下也保存成功。
不知道你遇到的情况是怎样的。
代码在下一贴,希望指导一下,谢谢
drawPanel.getGraphics().drawImage((Image)image,0,0,drawPanel);
//drawPanel类型为JPanel
}catch(Exception e){
}问题:JPanel中显示的图形,中间始终有个空白矩形区域 -_-
{
Image image = null;
public DrawPanel(Image image)
{
this.image = image;
} protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
} public Dimension getPreferredSize() {
return image == null ? new Dimension(0,0) :
new Dimension(image.getWidth(this), image.getHeight(this));
}
}BufferedImage image = ImageIO.read(file);
DrawPanel drawPanel = new DrawPanel(image);
然后把这个drawPanel 传给saveFile()方法。