关于Java Swing panel中 图片保存(某区域)

Green__Tea 2014-02-12 03:12:15
保存panel中一块区域 保存生图片
代码:
BufferedImage image = new BufferedImage(Main.mp.getWidth(),Main.mp.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 =(Graphics2D) image.getGraphics();
Main.mp.paint(g2);
save(image);
public void save(BufferedImage bImage)
{
//把图像保存为文件
JFileChooser chooser=new JFileChooser();//文件保存对话框
chooser.setCurrentDirectory(new File("."));
if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
{
File oFile=chooser.getSelectedFile();
try
{
ImageIO.write(bImage, "jpeg", oFile);//保存图像文件
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}
但是这样保存后 生成的图片是整个panel的大小 ,不是我想要的结果,我想要只保存panel中框的大小 (框的x,y,width,height都知道)不想截图 ,怎么完成

...全文
372 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Green__Tea 2014-02-13
  • 打赏
  • 举报
回复
引用 6 楼 huntor 的回复:
BufferedImage getSubimage(int x, int y, int w, int h)
Graphics2D g2 =  image.createGraphics(); 
Main.mp.paint(g2); 
save(image.getSubimage(x,y,w,h));
嗯 可以了 谢谢哦
Green__Tea 2014-02-13
  • 打赏
  • 举报
回复
别沉啊(...)
Green__Tea 2014-02-13
  • 打赏
  • 举报
回复
引用 6 楼 huntor 的回复:
BufferedImage getSubimage(int x, int y, int w, int h)
Graphics2D g2 =  image.createGraphics(); 
Main.mp.paint(g2); 
save(image.getSubimage(x,y,w,h));
不是很明白,你能多写几行代码不?
huntor 2014-02-12
  • 打赏
  • 举报
回复
BufferedImage getSubimage(int x, int y, int w, int h)
Graphics2D g2 =  image.createGraphics(); 
Main.mp.paint(g2); 
save(image.getSubimage(x,y,w,h));
Green__Tea 2014-02-12
  • 打赏
  • 举报
回复
而且 开始位置总是0,0 而不是框的开始位置
Green__Tea 2014-02-12
  • 打赏
  • 举报
回复
Main.mp.getWidth(),Main.mp.getHeight() 这个就是获取宽和高的 出来 整个panel 都被生成图片了
Green__Tea 2014-02-12
  • 打赏
  • 举报
回复
引用 1 楼 Green__Tea 的回复:
来人啊。。。
??不会啊
  • 打赏
  • 举报
回复
你先获取panel的宽高,然后再创建这样一个大小的图片,把你里面的图片写到这个图片内。
Green__Tea 2014-02-12
  • 打赏
  • 举报
回复
来人啊。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧