免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
JSP調(diào)用JavaBean在網(wǎng)頁上動態(tài)生成柱狀圖

  我們經(jīng)常要在網(wǎng)頁看到一些動態(tài)更新的圖片,最常見的莫過于股票的K線圖,本文試圖通過一個簡單的實例,向大家展示如何通過JSP 調(diào)用JavaBean在網(wǎng)頁上動態(tài)生成柱狀圖。


  背景:本人最近在為某統(tǒng)計局開發(fā)項目時,涉及到在網(wǎng)頁上動態(tài)生成圖片的問題,費了一天的時間,終于搞定,為幫助大家在以后遇到同樣的問題時不走彎路,現(xiàn)將設(shè)計思想及源代碼公布出來,與大家共勉。以下代碼在Windows2000成功測試通過,Web應(yīng)用服務(wù)器采用Allaire公司的Jrun3.0。


  第一步:創(chuàng)建一個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)方法用于創(chuàng)建JPG圖片,參數(shù)fileLocation為文件路徑

  graphicsGeneration(int h1,int h2,int h3,int h4,int h5)方法用于繪出圖片的內(nèi)容,參數(shù)h1……h5為每一個長方形的高度

  第二步:創(chuàng)建另一個Java Bean從文本文件中讀取數(shù)據(jù)(每一個長方形的高度),在實際應(yīng)用中數(shù)據(jù)存儲在Oracle數(shù)據(jù)庫中

  源程序如下:

//讀取Text文件中數(shù)據(jù)的 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()用于從文本中讀取數(shù)據(jù),將文本中的String類型轉(zhuǎn)換為int類型,并以數(shù)組類型返回。

  第三步:創(chuàng)建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首先調(diào)用Bean (GetData..class)讀取文件中的數(shù)據(jù),再調(diào)用Bean(ChartGraphics.class)生成圖片,最后顯示圖片。

  結(jié)束語:由于文本(ColumnHeightArray.txt)中的數(shù)據(jù)可以隨時變化,因此生成的圖片中的5個長方形的高度是隨之變化的,從而實現(xiàn)了圖片的動態(tài)生成.該設(shè)計思想還可以用于制作網(wǎng)站的投票系統(tǒng)。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java面試題集
JavaBean
學(xué)習筆記之JAVA圖形設(shè)計卷IAWT——第3章圖形
java研究館--jsp+javabean循序漸進教程
JSP useBean詳解
jsp入門學(xué)習教程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服