动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Jsp → 阅读文章

 JSP调用JavaBean在网页上动态生成柱状图

作者来源: 
阅读 1322 人次 , 2006-3-29 4:23:00 

  我们经常要在网页看到一些动态更新的图片,最常见的莫过于股票的k线图,本文试图通过一个简单的实例,向大家展示如何通过jsp 调用javabean在网页上动态生成柱状图。


  背景:本人最近在为某统计局开发项目时,涉及到在网页上动态生成图片的问题,费了一天的时间,终于搞定,为帮助大家在以后遇到同样的问题时不走弯路,现将设计思想及源代码公布出来,与大家共勉。以下代码在windows2000成功测试通过,web应用服务器采用allaire公司的jrun3.0。


  第一步:创建一个java bean用来生成jpg文件

  源程序如下:

//生成图片的 java bean
//作者:崔冠宇
//日期:2001-08-24
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类型,并以数组类型返回。

  第三步:创建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="d:\temp\chart.jpg"></img>
</body>
</html> 

   解释:jsp首先调用bean (getdata..class)读取文件中的数据,再调用bean(chartgraphics.class)生成图片,最后显示图片。

  结束语:由于文本(columnheightarray.txt)中的数据可以随时变化,因此生成的图片中的5个长方形的高度是随之变化的,从而实现了图片的动态生成.该设计思想还可以用于制作网站的投票系统。


 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:jsp+javascript打造级连菜单
· 下一篇:JSP单页面网站文件管理器
· JSP的9种基本内置组件
· java在Linux下的web应用(二)
· JSP中的TagLib应用(4-3)
· JSP由浅入深(9)
· JSP中的TagLib应用(4-4)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077