当前位置: 首页 > 图文教程 > 网络编程 > JSP > JSP调用JavaBean在网页上动态生成柱状图

JSP
使用Eclipse开发Jsp
Freebsd+Resin成功建立支持jsp平台
入门教程:JSP标准模板库(上)
入门教程:JSP标准模板库(下)
Struts学习傻瓜式入门篇
新手入门经典:Jsp环境配置
Apache服务器之JSP概述篇
配置Eclpise+tomcat并实现JSP的编写与部署
zip版本Tomcat配置新手入门
Windows2000下Apache2.0.46与Tomcat5.0.2整合配置方法
利用JSP 2.0开发Web应用程序
初学Java所需要注意的几点
整合Tomcat5和IIS5 及正常打开jsp
WIN98/2000下的jsp服务器
配置整合Win+Apache+PHP+MySQL+Tcomcat(或Resin)完全手册
Windows2000下整合Mysql4.0.13与Tomcat4.1.24搭建Jsp环境
JSP实践要点
Servlet/JSP配置详解
使用lomboz调试JSP
Win2000安装Apache+ApacheJserv+gnujsp之完全攻略

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-02   浏览: 43 ::
收藏到网摘: n/a

 背景:本人最近在为某统计局开发项目时,涉及到在网页上动态生成图片的问题,费了一天的时间,终于搞定,为帮助大家在以后遇到同样的问题时不走弯路,现将设计思想及源代码公布出来,与大家共勉。以下代码在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;
 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;
%>
<%
height=gd.getHightArray();
cg.graphicsGeneration(height[0],height,height,height,height);
%>
<html>
<body>
<img src="/upload/tech/20091102/20091102155229_10a7cdd970fe135cf4f7bb55c0e3b59f.jpg"></img>
</body>
</html>

   解释:JavaScript/