Article originates from a netizen
This article is written by smilelily users. Yesterday, I found my own email record. He has a user in 9cbs, articles source address http://dev.9cbs.net/User/smilelily struts development practice - graph instance
The main function of this case is to complete the drawing of the graph and generate the graph into JPG graphics and display.
1. Map of main function description
Graphics G
g.drawRect (x_top, x_bottom, width, height): painted rectangle
G.SetColor (color): Specify color;
G.FillRect (x_top, x_bottom, width, height): Drawing rectangle ///
g.drawroundRect (x_top, x_bottom, width, height): Draw round rectangular
g.FillRect (x_top, x_bottom, width, height): Draw a filling round rectangle
g.drawarc (beg_x, beg_y, width, height, beg_tangle, end_tangle): scroll
G.fillarc (beg_x, beg_y, width, height, beg_tangle, end_tangle): Fill arc
g.drawline (beg_x, beg_y, end_x, end_y): draw a straight line
g.drawstring (THSSTRING, STRINGTOPLT_X, STRINGTOPLEFT_Y): Drawing
G.SetFont (font): Set the font of the brush
g.setpaint (color): Set the color of the brush
2. Curve drawing file
/ **************** Program curveframe begin *********************** /
Package test;
Import java.awt. *;
Import java.awt.geom. *;
Import java.awt.image. *;
Import javax.swing.jframe;
Import java.lang.math;
/ ** * Curve drawing * /
Public Class Curveframe
Extends jframe {
/**data*/
Private Double [] Result
/ ** Data Item Name * /
Private string [] Title;
/ ** Parameters: Results, Name Set * /
Public curveframe (double [] result, string [] title) {
THIS.RESULT = Result;
THIS.TITLE = Title;
}
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Dimension DIM = this.getsize ();
G2.SetColor (color.white);
G2.FillRect (0, 0, Dim.Width, Dim.Height);
// write title;
Font font = g2.getfont (). DeriveFont (12.0f);
g2.setfont (font);
FontMetrics Metrics = G2.GetFontMetrics ();
G2.SETPAINT (color.black);
g2.drawstring (Title [0], (Dim.Width - Title [0] .length () * 5) / 2, 10);
// Draw the x and y axis
G2.setStroke (New Basicstroke (2.0F));
G2.draw (New Line2D.double (40, 30, 40, Dim.Height - 30)); g2.draw (new line2d.double (40, Dim.Height - 30,
Dim.Width - 20, Dim.Height - 30))));
Long unit = (Math.Round ((Result [0] 750) / 1500)) * 50;
Long Ymax = Result [0] == 0? Unit: Math.Round ((Result [0] Unit / 2) / unit) * unit;
Int widthper = (Dim.Width - 40) / Result.Length;
Long Heightper = (Dim.Height - 60) / (YMAX / Unit);
// Draw the y axis scale;
G2.SETPAINT (color.lightgray);
G2.setStroke (New Basicstroke (1.0F));
For (int I = 0; i <= ymax / unit; i ) {
G2.draw (New Line2D.Double (40, Dim.height - 30 - i * Heightper,
DIM.WIDTH - 40, DIM.HEIGHT - 30 - i * heightper);
String ylabel = String.Valueof (i * unit);
G2.drawstring (YLabel, 35 - Metrics.StringWidth (Ylabel),
Dim.height - 30 - (i * heightper));
}
// DRAW X TITLE;
GeneralPath Path = new generalpath (generalpath.wind_even_odd,
Result.Length - 2);
For (int i = 1; i // Draw x scale; G2.SETPAINT (color.lightgray); G2.setStroke (New Basicstroke (1.0F)); g2.draw (New Line2D.double (40 (i - 1) * Widthper, Dim.Height - 30, 40 (i - 1) * widthper, 30)); Font = g2.Getfont (). DeriveFont (10.0f); g2.setfont (font); G2.SETPAINT (color.black); g2.drawstring (title [i], 40 widthper * (i - 1) -metrics.StringWidth (Title [i]) / 2, DIM.HEIGHT - 10); g2.setpaint (color.red); G2.setStroke (New Basicstroke (2.0F)); IF (i == 1) { Path.Moveto (40, Math.Round (Dim.Height - 30 - (Result [i] / unit) * Heightper); } Else { Path.Lineto (MathPer * (i - 1)) 40, Math.Round (Dim.Height - 30 - (Result [i] / unit) * Heightper); } } g2.draw (path); } } / ******************************************************* / 3. Generate JPG graphics and display / **************** Program begin ******************************** / Package test; Import org.apache.struts.Apache.struts.action. *; Import javax.servlet.http. *; Import javax.servlet. *; Import java.io. *; Import java.util. *; Import java.awt. *; Import java.awt.image. *; Import com.sun.Image.codec.jpeg. *; / ** * Curve * / Public Class Wagechangecurvection EXTENDS ACTION { Private static factory string content_type = "image / gif"; Private static final int width = 700; Private static final int height = 500; Public ActionForward Perform (ActionMApping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, HttpservletResponse response) throws oException, Servletexception { Response.setContentType (Content_Type); ServletOutputStream out = response.getOutputStream (); Double [] result =. . . String [] Title =. . . // Get graphics data Try { Curveframe Dummy = New CurveFrame (Result, Title); Dummy.setsize (New Dimension (Width, Height); BufferedImage Image = New BufferedImage (Width, Height, BufferedImage.Type_INT_RGB); Graphics2d g2 = image.creategraphics (); Dummy.paint (G2); JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.encode (image); Out.flush (); } Catch (throwable e) { E.PrintStackTrace (); } Return mapping.findforward ("Success"); } } / ***************** Program end ******************************* /