posts - 61,  comments - 2033,  trackbacks - 0

           

          package demo;

          import java.io.File;
          import org.jfree.chart.JFreeChart;
          import java.io.IOException;
          import java.io.OutputStream;
          import java.io.BufferedOutputStream;
          import java.io.FileOutputStream;
          import java.awt.image.BufferedImage;
          import org.jfree.chart.encoders.ImageFormat;
          import java.awt.Graphics2D;
          import java.awt.*;
          import java.awt.*;
          import org.jfree.chart.encoders.ImageEncoder;
          import org.jfree.chart.encoders.ImageEncoderFactory;
          import javax.swing.JFrame;
          import javax.imageio.ImageIO;

          public class ToImg {
            
          public ToImg() {
            }



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

            
          public static void saveChartAsJPEG(File file,
                                               
          float quality,
                                               JFreeChart chart,
                                               
          int width,
                                               
          int height) throws IOException {

              
          if (file == null{
                  
          throw new IllegalArgumentException("Null 'file' argument.");
              }

              
          if (chart == null{
                  
          throw new IllegalArgumentException("Null 'chart' argument.");
              }

              OutputStream out 
          = new BufferedOutputStream(new FileOutputStream(file));

              BufferedImage images 
          = chart.createBufferedImage(width, height, null);
              BufferedImage image 
          = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
              Graphics2D g2 
          = image.createGraphics();
          //    draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
              g2.dispose();

              writeBufferedImage(image, ImageFormat.JPEG, out, quality);
              out.close();
            }


            
          public static void writeBufferedImage(BufferedImage image, String format,
                OutputStream outputStream, 
          float quality)
                
          throws IOException {
              ImageEncoder imageEncoder
                  
          = ImageEncoderFactory.newInstance(format, quality);
              imageEncoder.encode(image, outputStream);

            }


            
          public static void main(String[] args) {
              Robot robot 
          = null;
              
          try {
                
                JFrame f 
          = new Frame1(); 
                f.setSize(
          500,600);
                f.setVisible(
          true);
                f.setDefaultCloseOperation(
          2);
                BufferedImage image 
          = new BufferedImage(500600, BufferedImage.TYPE_INT_RGB);
                f.getContentPane().paint(image.getGraphics());
                FileOutputStream out 
          = new FileOutputStream("d:\\b.jpg");
                ImageIO.write(image, 
          "jpeg" , out);
                out.close();

          /**    
               抓圖  
                Robot robot = new java.awt.Robot();
                BufferedImage image = robot.createScreenCapture(new Rectangle(100, 100));
                ImageEncoder imageEncoder
                    = ImageEncoderFactory.newInstance(ImageFormat.JPEG, 100);
                FileOutputStream out = new FileOutputStream("d:\\a.jpg");
                imageEncoder.encode(taget, out);
           }
          */


             }

              
          catch (Exception ex) {
                ex.printStackTrace();
              }


            }


          }

          posted on 2005-12-20 17:16 魚上游 閱讀(759) 評論(0)  編輯  收藏 所屬分類: 爪哇風景欣賞爪哇世界探險
          <2005年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(82)

          隨筆分類(59)

          文章分類(21)

          相冊

          收藏夾(40)

          GoodSites

          搜索

          •  

          積分與排名

          • 積分 - 1267790
          • 排名 - 22

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 南开区| 增城市| 大港区| 阿拉善左旗| 上杭县| 仁怀市| 云和县| 桑植县| 扎囊县| 巴马| 新和县| 栾城县| 陵川县| 怀化市| 正阳县| 綦江县| 老河口市| 宿州市| 乐平市| 都安| 安龙县| 昌邑市| 台东市| 项城市| 徐水县| 巨野县| 手游| 温州市| 右玉县| 金门县| 措美县| 丰原市| 海林市| 乐都县| 克拉玛依市| 南丰县| 长汀县| 黄浦区| 长葛市| 治县。| 云浮市|