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.
Step 1: Create a Java Bean to generate the JPG file source program as follows: // Generate the Java Bean Import Java.io.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 High INT ColumnWidth = 30; // Cap int columnHeight = 200; the maximum height of the column 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");}} Explanation: CreateImage (String FileLocation) method is used to create a JPG picture, parameter filelocation is the file path GRAPHICSGENERATION (Int H1 , INT H2, INT H3, INT H4, INT H5 method is used to draw the contents of the picture, parameters H1 ... H5 for each rectangular height second step: Create another Java bean read data from text files ( Each rectangular height is stored in the actual application in the Oracle database Source program as follows: // read the java bean import java.io.io. *; public class getData {Int HeightArray in the text file in the text file [] = new int intelect [5]; public int [] gethiGhtarray () {Try {randomaccessfile randomaccessfile = new randomaccessfile ("d: //temp//columnhemheightaRray.txt", "r"); for (int i = 0; i <5; i ) ) { HeightArray [I] = integer.parseint (randomaccessfile.readline ());}} catch (exception e) {system.out.println (e);} Return HeightArray;}}}}} Take the data, convert the String type in the text to the int type and return it as an array type.