隨筆-71  評論-4  文章-0  trackbacks-0
          我們經(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……h(huán)5為每一個長方形的高度 

             第二步:創(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文件 

             源程序如下: 
          <jsp:useBean id="cg" class="example.img.ChartGraphics"/> 
          <jsp:useBean id="gd" class="example.img.GetData"/> 
          <%! 
          int height[]=new int[5]; 
          %> 
          <% 
          height=gd.getHightArray(); 
          for(int i=0;i<height.length;i++)
             out.println(height[i]+"<br>");
          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)。
          posted on 2006-01-12 13:06 zjw_albert 閱讀(106) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 邳州市| 林甸县| 湘阴县| 屏边| 桦南县| 卓资县| 延寿县| 江油市| 昔阳县| 盖州市| 开江县| 阳高县| 金寨县| 怀集县| 九龙坡区| 宾川县| 吴江市| 古蔺县| 仁怀市| 平顶山市| 泰和县| 珲春市| 黎城县| 河源市| 湖口县| 湘西| 萨嘎县| 乐清市| 黑山县| 巧家县| 长春市| 军事| 钟祥市| 建水县| 汝阳县| 乐昌市| 兴和县| 黄浦区| 将乐县| 定陶县| 高青县|