如何把awt或者swing的UI生成一个图形文件

博客涉及Java、Swing、UI开发以及jfreechart等内容。Java是后端开发常用语言,Swing用于构建Java的图形用户界面,jfreechart可进行图表绘制,这些信息技术相关元素是博客核心。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

None.gif package  demo;
None.gif
None.gif
import  java.io.File;
None.gif
import  org.jfree.chart.JFreeChart;
None.gif
import  java.io.IOException;
None.gif
import  java.io.OutputStream;
None.gif
import  java.io.BufferedOutputStream;
None.gif
import  java.io.FileOutputStream;
None.gif
import  java.awt.image.BufferedImage;
None.gif
import  org.jfree.chart.encoders.ImageFormat;
None.gif
import  java.awt.Graphics2D;
None.gif
import  java.awt. * ;
None.gif
import  java.awt. * ;
None.gif
import  org.jfree.chart.encoders.ImageEncoder;
None.gif
import  org.jfree.chart.encoders.ImageEncoderFactory;
None.gif
import  javax.swing.JFrame;
None.gif
import  javax.imageio.ImageIO;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  ToImg  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public ToImg() dot.gif{
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   * Saves a chart to a file in JPEG format.
InBlock.gif   *
InBlock.gif   * 
@param file  the file (<code>null</code> not permitted).
InBlock.gif   * 
@param quality  the JPEG quality setting.
InBlock.gif   * 
@param chart  the chart (<code>null</code> not permitted).
InBlock.gif   * 
@param width  the image width.
InBlock.gif   * 
@param height  the image height.
InBlock.gif   *
InBlock.gif   * 
@throws IOException if there are any I/O errors.
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  
public static void saveChartAsJPEG(File file,
InBlock.gif                                     
float quality,
InBlock.gif                                     JFreeChart chart,
InBlock.gif                                     
int width,
ExpandedSubBlockStart.gifContractedSubBlock.gif                                     
int height) throws IOException dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (file == nulldot.gif{
InBlock.gif        
throw new IllegalArgumentException("Null 'file' argument.");
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (chart == nulldot.gif{
InBlock.gif        
throw new IllegalArgumentException("Null 'chart' argument.");
ExpandedSubBlockEnd.gif    }

InBlock.gif    OutputStream out 
= new BufferedOutputStream(new FileOutputStream(file));
InBlock.gif
InBlock.gif    BufferedImage images 
= chart.createBufferedImage(width, height, null);
InBlock.gif    BufferedImage image 
= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
InBlock.gif    Graphics2D g2 
= image.createGraphics();
InBlock.gif
//    draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
InBlock.gif
    g2.dispose();
InBlock.gif
InBlock.gif    writeBufferedImage(image, ImageFormat.JPEG, out, quality);
InBlock.gif    out.close();
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public static void writeBufferedImage(BufferedImage image, String format,
InBlock.gif      OutputStream outputStream, 
float quality)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
throws IOException dot.gif{
InBlock.gif    ImageEncoder imageEncoder
InBlock.gif        
= ImageEncoderFactory.newInstance(format, quality);
InBlock.gif    imageEncoder.encode(image, outputStream);
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public static void main(String[] args) dot.gif{
InBlock.gif    Robot robot 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif      
InBlock.gif      JFrame f 
= new Frame1(); 
InBlock.gif      f.setSize(
500,600);
InBlock.gif      f.setVisible(
true);
InBlock.gif      f.setDefaultCloseOperation(
2);
InBlock.gif      BufferedImage image 
= new BufferedImage(500600, BufferedImage.TYPE_INT_RGB);
InBlock.gif      f.getContentPane().paint(image.getGraphics());
InBlock.gif      FileOutputStream out 
= new FileOutputStream("d:\\b.jpg");
InBlock.gif      ImageIO.write(image, 
"jpeg" , out);
InBlock.gif      out.close();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/** *//**    
InBlock.gif     抓图  
InBlock.gif      Robot robot = new java.awt.Robot();
InBlock.gif      BufferedImage image = robot.createScreenCapture(new Rectangle(100, 100));
InBlock.gif      ImageEncoder imageEncoder
InBlock.gif          = ImageEncoderFactory.newInstance(ImageFormat.JPEG, 100);
InBlock.gif      FileOutputStream out = new FileOutputStream("d:\\a.jpg");
InBlock.gif      imageEncoder.encode(taget, out);
ExpandedSubBlockEnd.gif }
*/

InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch (Exception ex) dot.gif{
InBlock.gif      ex.printStackTrace();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值