注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Norton AntiVirus 2005测试版截图
让Ghostbusters为电脑保驾护航
安全小知识:Rundll.exe是病毒吗?
Htran把内网肉鸡做成SOCKS5代理
Windows Vista系统防火墙初探
微软官方解读Vista安全性: 全面的
Windows利器:Rundll.exe高级应用
DNS (domain Name System) 系统的
DNS 系统设定例--7.[Fwd] 特殊网
IMail Anti-Virus简易说明及快速
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
struts开发实践—曲线图实例
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:
本案主要功能是完成曲线图的绘制,并将曲线图生成jpg图形并显示。 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************************/






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务