/** * jeruGraphics v 1.0 ** servlet完成了一些动态生成图像的例子,* 而且程序很长,感觉在实用性和可读性上都不是很好。* 给出段代码,命令行生成图像文件。这是不是简单易用?* * 创建一个 BufferedImage 对象,把你的“画”放在这个缓冲器里,* 打开另一个文件,在输入文件后输入图像流编码,以便有一个* jpg文件出现了,试试吧。。。** Mender : *Jeru Liu * Homepage : *http://javaren.126.com* Email: [email protected] * * 这只是一个例子程序,不实用,但很有参考价值。* */ 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 publicic文件到指定路径 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"); }}
![](/images/780-200-2.jpg)