helloworld

来源:互联网 发布:手机抠图用什么软件 编辑:程序博客网 时间:2024/05/01 02:56

package USTC;

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

/**
 *
 *<br>Title:生成chart 的类
 *<br>Description:
 *<br>Copyright: Copyright (c) 2005
 * <br>作者:汪征
 * @version 1.0
 */
public class ChartGraphics {
  public ChartGraphics() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  BufferedImage image;

  private void createImage(String fileLocation) {

    try {

      FileOutputStream fos = new FileOutputStream(fileLocation);
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
      encoder.encode(image);
      bos.close();
    }
    catch (Exception e) {
      System.out.println(e);
    }
  }

  /**
   * 该方法用于绘出图片的内容。
   *  使用方法: cg.graphicsGeneration(6,"d://yang.txt","d://chart.jpg");
   * @param num int:为统计的图的个数,这个个数与fliename文本中的行树对应
   * @param filename String:读取数据的文件全路径,文件格式如下:yang.txt
   *<br>45
   *<br>chian
   *<br>43
   *<br>jepae
   *<br>23
   *<br>mericant
   *<br>453
   *<br>frach
   *<br>60
   *<br>england
   *<br>24
   *<br>hello
   *注意:柱的最大高度200,不能超过8个柱
   * @param newfile String:显示而生成的图的文件全路径
   */
  public void graphicsGeneration(int num, String filename, String newfile) {

    final int X = 10;
    int imageWidth = 300; //图片的宽度
    int imageHeight = 300; //图片的高度
    int columnWidth = 30; //柱的宽度
    int columnHeight = 200; //柱的最大高度
    int heightArray[] = new int[num];
    String title[] = new String[num];
    try {
      RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "r");
      for (int i = 0; i < 2 * num; i++) {
        if ( (i % 2) == 0) {
          heightArray[i / 2] = Integer.parseInt(randomAccessFile.readLine());//读取高度
        }
        else {
          String tmpstr=new String(randomAccessFile.readLine().toString().getBytes("8859_1"),"GB2312");

          title[i / 2] = tmpstr;//读取标题
        }
      }
    }
    catch (Exception e) {
      System.out.println(e);
    }

    ChartGraphics chartGraphics = new ChartGraphics();
    chartGraphics.image = new BufferedImage(imageWidth, imageHeight,
                                            BufferedImage.TYPE_INT_RGB);
    Graphics graphics = chartGraphics.image.getGraphics();
    graphics.setColor(Color.white);
    graphics.fillRect(0, 0, imageWidth, imageHeight);
    graphics.setColor(Color.red);
    for (int j = 0; j < num; j++) {
      //坐标的原点在左上角。
      //columnHeight代表的是最高的地方,columnHeight-heightArray[j]代表的是显示图的每个列的最上方的Y坐标。
      //Y=0是显示图的最高点
      graphics.drawRect(X + (j + 1) * columnWidth, columnHeight - heightArray[j],
                        columnWidth, heightArray[j]);
      graphics.setColor(Color.getHSBColor(j * 13 + 257, j * 5 + 147,
                                          j * 35 + 71));
      graphics.fillRect(X + (j + 1) * columnWidth, columnHeight - heightArray[j],
                        columnWidth, heightArray[j]);
      graphics.setColor(Color.BLACK);
      if (columnHeight - heightArray[j] - 10 < 10) {
        graphics.drawString(title[j], X + (j + 1) * columnWidth, columnHeight);
      }
      else {
        graphics.drawString(title[j], X + (j + 1) * columnWidth,
                            columnHeight - heightArray[j] - 10);
      }
    }
    //graphics.drawRect(X+2*columnWidth, columnHeight-h2, columnWidth, h2);
    //graphics.drawRect(X+3*columnWidth, columnHeight-h3, columnWidth, h3);
    //graphics.drawRect(X+4*columnWidth, columnHeight-h4, columnWidth, h4);
    //graphics.drawRect(X+5*columnWidth, columnHeight-h5, columnWidth, h5);
    chartGraphics.createImage(newfile);
  }

  private void jbInit() throws Exception {
  }
}

原创粉丝点击