国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP調用JavaBean在網頁上動態生成柱狀圖

2019-11-18 19:54:10
字體:
來源:轉載
供稿:網友

  我們經常要在網頁看到一些動態更新的圖片,最常見的莫過于股票的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個長方形的高度是隨之變化的,從而實現了圖片的動態生成.該設計思想還可以用于制作網站的投票系統。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永寿县| 涪陵区| 富川| 温泉县| 望江县| 三门峡市| 翼城县| 兴和县| 万州区| 精河县| 隆尧县| 肥西县| 陕西省| 涞水县| 宁乡县| 宜州市| 响水县| 米脂县| 犍为县| 闻喜县| 鄂托克旗| 南雄市| 大同市| 奉节县| 承德市| 永定县| 浦县| 富平县| 县级市| 凌云县| 镇远县| 珠海市| 大邑县| 攀枝花市| 久治县| 施秉县| 屏东县| 二连浩特市| 分宜县| 江都市| 哈巴河县|