时间: 2021-07-31 作者:daque
后台:自己迩来在为某统计局开拓名目时,波及到在网页上动静天生图片的题目,费了一天的功夫,毕竟搞定,为扶助大师在此后遇到同样的题目时不走弯道,现将安排思维及源代码颁布出来,与大师互勉。以次代码在windows2000胜利尝试经过,web运用效劳器沿用allaire公司的jrun3.0。 第一步:创造一个java bean用来天生jpg文献 源步调如次:
import java.io.*; import java.util.*; import com.sun.image.codec.jpeg.*; import java.awt.image.*; import java.awt.*;
public class chartgraphics { bufferedimage image; public 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); } } public void graphicsgeneration(int h1,int h2,int h3,int h4,int h5) { final int x=10; int imagewidth = 300;//图片的宽窄 int imageheight = 300;//图片的莫大 int columnwidth=30;//柱的宽窄 int columnheight=200;//柱的最大莫大 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); graphics.drawrect(x+1*columnwidth, columnheight-h1, columnwidth, h1); 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("d:\\temp\\chart.jpg"); } }
证明:createimage(string filelocation)本领用来创造jpg图片,参数filelocation为文献路途
graphicsgeneration(int h1,int h2,int h3,int h4,int h5)本领用来绘出图片的实质,参数h1……h5为每一个矩形形的莫大
第二步:创造另一个java bean从文本文献中读取数据(每一个矩形形的莫大),在本质运用中数据保存在oracle数据库中
源步调如次:
//读取text文献中数据的 java bean //作家:崔冠宇 //日子:2001-08-24 import java.io.*; public class getdata { int heightarray[] = new int[5]; public int[] gethightarray() { try { randomaccessfile randomaccessfile = new randomaccessfile ("d:\\temp\\columnheightarray.txt","r"); for (int i=0;i<5;i++) { heightarray[i] = integer.parseint(randomaccessfile.readline()); } } catch(exception e) { system.out.println(e); } return heightarray; } }
证明: gethightarray()用来从文本中读取数据,将文本中的string典型变换为int典型,并以数组典型归来。
第三步:创造javascript/" target="_blank">jsp文献
源步调如次:
<%@ page import="chartgraphics" %> <%@ page import="getdata" %> <jsp:usebean id="cg" class="chartgraphics"/> <jsp:usebean id="gd" class="getdata"/> <%! int height[]=new int[5]; %> <% height=gd.gethightarray(); cg.graphicsgeneration(height[0],height[1],height[2],height[3],height[4]); %> <html> <body> <img src="http://edu.chinaz.com/get/program/jsp/d:/temp/chart.jpg"></img> </body> </html>
证明:javascript/" target="_blank">jsp开始挪用bean (getdata..class)读取文献中的数据,再挪用bean(chartgraphics.class)天生图片,结果表露图片。
中断语:因为文本(columnheightarray.txt)中的数据不妨随时变革,所以天生的图片中的5个矩形形的莫大是随之变革的,进而实行了图片的动静天生.该安排思维还不妨用来创造网站的开票体例。