<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      好東西介紹,直接用 java 命令行動態生成jpg文件

      [摘要]/** * jeruGraphics v 1.0 ** 看到一些動態生成圖象的例子都是servlet完成的,* 而且程序很長,覺得不是無論從實用性還是可讀性來說都不是太好。* 這里給了段代碼,命令行...
      /**
      * jeruGraphics v 1.0
      *
      * 看到一些動態生成圖象的例子都是servlet完成的,
      * 而且程序很長,覺得不是無論從實用性還是可讀性來說都不是太好。
      * 這里給了段代碼,命令行生成圖象文件。這樣是不是簡單易用些呢?
      *
      * 創建一個 BufferedImage 對象,將你的“畫”放到這個緩沖里,
      * 再打開一個文件,將圖像流編碼后輸入這個文件,這樣就有一個
      * jpg文件出現了,試試吧。。。
      *
      * Mender :
      * Jeru Liu
      * Homepage :
      * http://javaren.126.com
      * Email: jeru@163.net
      *
      * 這僅僅是一個范例程序,沒什么實用,卻極具參考價值。
      *
      */


      import java.io.*;
      import java.util.*;
      import com.sun.image.codec.jpeg.*;
      import java.awt.image.*;
      import java.awt.*;

      public class jeruGraphics {
      BufferedImage image;

      // 創建 jpg 文件到指定路徑下
      public void createJpg(String path) {
      try {
      FileOutputStream fos = new FileOutputStream(path);
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
      encoder.encode(image);
      bos.close();
      } catch(FileNotFoundException fnfe) {
      System.out.println(fnfe);
      } catch(IOException ioe) {
      System.out.println(ioe);
      }
      }

      public static void main(String[] args) {
      int width=400, height=200;
      int xLength=300, yLength=150;
      int count=5;

      Vector data=new Vector();
      data.addElement(new Integer(100));
      data.addElement(new Integer(120));
      data.addElement(new Integer(150));
      data.addElement(new Integer(40));
      data.addElement(new Integer(5));

      jeruGraphics jg = new jeruGraphics();
      jg.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics g = jg.image.getGraphics();

      // 畫坐標
      g.setColor(Color.white);
      g.fillRect(0, 0, width, height);
      g.setColor(Color.blue);
      g.drawLine(10,height-10,10,height-10-yLength);
      g.drawLine(10,height-10,10+xLength,height-10);

      // 連線
      int yTo;
      int yFrom = ((Integer)(data.elementAt(0))).intValue();
      for (int i=1; i<count; i++) {
      yTo=((Integer)(data.elementAt(i))).intValue();
      g.drawLine(10+i*xLength/count,height-10,10+i*xLength/count,height-15);
      g.drawLine(10+(i-1)*xLength/count,yFrom,10+i*xLength/count,yTo);
      yFrom=yTo;
      }

      jg.createJpg("d:\\aaa.jpg");

      }
      }



      日韩精品一区二区三区高清