histogram.java

来自「功能描述:jsp开发答疑解惑200问题说明」· Java 代码 · 共 62 行

JAVA
62
字号
package myBean;

import java.io.*;
import java.util.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;

public class Histogram {
	public Histogram() {
	}
	
	public double getHistogram(int width, int totalWidth, int height, OutputStream os) {
		
		if(width>totalWidth)
			return 0;
		
		BufferedImage image = new BufferedImage(totalWidth, height, 
									BufferedImage.TYPE_INT_RGB); 

		// 获取图形上下文 
		Graphics g = image.getGraphics(); 

		// 设定背景色 
		g.setColor(new Color(0xFDFDFD)); 
		g.fillRect(0, 0, totalWidth, height); 

		// 画边框 
		g.setColor(Color.black); 
		g.drawRect(0,0,totalWidth-1,height-1);

		// 画柱状图
		g.setColor(Color.blue);
		g.fillRect(0, 0, width, height);
		

		// 释放图形上下文
		g.dispose(); 
		
		try {
			// 输出图象到页面 
			ImageIO.write(image, "JPEG", os);
		} catch (IOException e) {
			return 0;
		}
		
		return (double)width/totalWidth;
	}

/*	
	public static void main(String []args) {
		try{
			BufferedOutputStream os = new BufferedOutputStream(
											new FileOutputStream("c:\\test.jpg"));
											
			new Histogram().getHistogram(180, 200, 20, os);
			os.flush();
			os.close();
		} catch(Exception e) {}
	}
*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?