We often want to see some dynamic updated pictures in the web. The most common Mo stock's K-line map, this article tries to show you how to call JavaBean through JSP to dynamically generate a histogram on the web page through a simple instance.
Background: I have recently developed a project for a statistical bureau, involving the problem of dynamically generating pictures on the web, and finally getting a day, finally getting it, in order to help everyone get along the same problem in the future, now Design ideas and source code are announced, and you will share with you. The following code is successfully tested in Windows2000, and the web application server uses Allaire's JRUN3.0.
Step 1: Create a java bean to generate a JPG file
The source program is as follows:
// generated picture of Java Bean //: // Cuiguan Yu Date: 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; // Picture width int imageHeight = 300; // Picture of INT columnWidth = 30; // Column width int colornheight = 200; // Maximum height
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, ColumnWidth, H5); ChartGraphics.createImage ("D: //Temp//Chart.jpg");}} Interpretation: CREATEIMAGE (STRING FILELOCATION) method is used to create JPG pictures, parameter filelocation is file path
GRAPHICSGENERATION (INT H1, INT H2, INT H3, INT H4, INT H5) method is used to draw contents of the picture, parameters H1 ... H5 is the height of each rectangle
Step 2: Create another Java Bean read data from a text file (height of each rectangle), stored in the actual application in the Oracle database
The source program is as follows:
// read the data from Text files Java Bean // Author: Cuiguan Yu // Date: 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;}}
Explanation: gethHightArray () is used to read data from the text, convert the String type in the text to an int type, and return in an array type.
Step 3: Create a JSP file
The source program is as follows:
<% @ Page import = "chartgraphics"%> <% @ page import = "getData"%>
Conclusion: Since the data in ColumnHeightArray.txt can vary at any time, the height of the five rectangles in the generated picture is changed, thereby realizing the dynamic generation of the picture. This design idea can also be used to make a website Voting system.