|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
我们常常要在网页看到一些动态更新的图片,最常见的莫过于股票的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:tempchart.jpg"></img> </body> </html> 解释:JSP首先调用Bean (GetData..class)读取文件中的数据,再调用Bean(ChartGraphics.class)生成图片,最后显示图片。 结束语:由于文本(ColumnHeightArray.txt)中的数据可以随时变化,因此生成的图片中的5个长方形的高度是随之变化的,从而实现了图片的动态生成.该设计思想还可以用于制作网站的投票系统。 返回类别: 教程 上一教程: JAVA数据报编程之单播 下一教程: 在JSP中发送EMAIL 您可以阅读与"JSP调用JAVABEAN在网页上动态生成柱状图"相关的教程: · 用JSP从数据库中读取图片并显示在网页上 · 分别用java Application 及 JSP调用javaBean的例子 · 使用Servlet动态生成图片 · 使用Java Servlet动态生成图片 · 动态网页制作技术JSP与ASP的比较 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |