# struts开发实践—曲线图实例

struts开发实践-曲线图实例<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1。画图的主要函数说明

Graphics g

g.drawRect(x_top,x_bottom,width,height):画矩形

g.setColor(color):指定颜色；

g.fillRect(x_top,x_bottom,width,height):画填充的矩形//

g.drawRoundRect(x_top,x_bottom,width,height):画圆边矩形

g.fillRoundRect(x_top,x_bottom,width,height):画一个填充的圆边矩形

g.drawArc(beg_x,beg_y,width,height,beg_tangle,end_tangle):画弧线

g.fillArc(beg_x,beg_y,width,height,beg_tangle,end_tangle):填充弧

g.drawLine(beg_x,beg_y,end_x,end_y):画一条直线

g.drawString(theString,stringTopLeft_x,StringTopLeft_y):画字

g.setFont(font)：设置画笔的字体

g.setPaint(color)：设置画笔的颜色

2。曲线图绘制文件

/************program CurveFrame begin**********************/

package test;

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import javax.swing.JFrame;

import java.lang.Math;

/** *曲线图绘制 */

public class CurveFrame

extends JFrame {

/**数据*/

private double[] result;

/**数据项名称*/

private String[] title;

/**参数：结果集，名称集*/

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);

//写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 < result.length; 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(Math.round(widthPer * (i - 1)) + 40,

Math.round(dim.height - 30 -

(result[i] / unit) * heightPer));

}

}

g2.draw(path);

}

}

/*****************program end************************/

3。生成jpg图形并显示

/*****************program begin************************/

package test;

import org.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.*;

/**

* 变化曲线

*/

public class WageChangeCurveAction

extends Action {

private static final 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 IOException,

ServletException {

response.setContentType(CONTENT_TYPE);

ServletOutputStream out = response.getOutputStream();

double[] result=。。。String[] title = 。。。//获得图形数据

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************************/

• 本文已收录于以下专栏：

举报原因： 您举报文章：struts开发实践—曲线图实例 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)