在JPanel中绘图,并利用ImageIO将JPanel保存为图片时,往往只保存一个空白的JPanel,用户所绘制的图形往往不能正确保存,查了很多资料说是要调用paintComponent方法而不是paint方法,但是这里强调的事自由绘图,而不是画好图然后呈现出来。
下面一小段程序是将一个Jpanel保存为图片的正常思路,但在自由绘图时,这个程序只保存一个空白的JPanel。
public boolean savePanelAsJpg(File file, DrawPanel panel) {
Dimension imageSize = panel.getSize();
BufferedImage image = new BufferedImage(imageSize.width,imageSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
panel.paint(graphics);
graphics.dispose();
try {
ImageIO.write(image, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
//可以指定要抓取的区域
BufferedImage screencapture = new Robot().createScreenCapture(
new Rectangle( 15, 15, 150, 150));
//可以抓取指定控件:
BufferedImage image = new Robot().createScreenCapture(
new Rectangle( jFrame.getX(), jFrame.getY(), jFrame.getWidth(), jFrame.getHeight() ) );
下面是一个使用Jpanel绘图并保存为jpg文件的完整例子。
package cvts;
import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawSave {
private JFrame jFrame = null;
private JPanel jPanel = null;
private String path = System.getProperty("user.dir");
//下侧右侧面板画图时的参数
private int startX,startY,endX,endY;
public void showGui(){
jFrame = new JFrame();
jPanel = new JPanel();
//设置jPanel大小
jPanel.setPreferredSize(new Dimension(400,400));
jPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
jPanel.addMouseListener(new MouseAdapter(){
//鼠标按下事件,重置startX,startY
public void mousePressed(MouseEvent e){
startX = e.getX();
startY = e.getY();
}
//双击面板是保存绘图区
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2){
savePic(path+"/pic.jpg");
}
}
});
//鼠标拖动事件,自由画图
jPanel.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent e){
Graphics graphics = jPanel.getGraphics();
endX = e.getX();
endY = e.getY();
((Graphics2D) graphics).setColor(new Color(144,242,41));
((Graphics2D) graphics).setStroke(new BasicStroke(10));
((Graphics2D)graphics).drawLine(startX,startY,endX,endY);
startX=endX;
startY=endY;
}
});
//设置jFrame的位置
jFrame.setLocation(400, 200);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(jPanel);
jFrame.pack();
jFrame.setVisible(true);
}
public void savePic(String path){
BufferedImage myImage = null;
try {
myImage = new Robot().createScreenCapture(
new Rectangle(jFrame.getX()+8, jFrame.getY()+30, jPanel.getWidth(), jPanel.getHeight()));
ImageIO.write(myImage, "jpg", new File(path));
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DrawSave drawAndSave = new DrawSave();
drawAndSave.showGui();
}
}