123
返回列表 发新帖
楼主: keaide

从一个舆论调查的制作谈面向对象的编程思路

[复制链接]
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
21#
 楼主| 发表于 2006-9-23 14:20 | 只看该作者
//画颜色方块
g.FillRectangle(new SolidBrush(item.Color) , intRectSize * 2 ,
intRectSize * (i * 2 + 1) + m_intHeight / 3 * 2
,intRectSize , intRectSize) ;
//写文字
g.DrawString(intPercent.ToInt32().ToString() + "% " + item.Text ,
new Font("宋体" , intRectSize) ,
new SolidBrush(item.Color) , intRectSize * 4 ,
intRectSize * ( i * 2 + 1) + m_intHeight / 3 * 2) ;

}
}
//清控对象
g.Dispose() ;

//保存为jpg格式
try
{
myBmp.Save(a_strFileName , System.Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw(new Exception("保存图片失败:" + e.ToString())) ;
}

myBmp.Dispose() ;

}

/// <summary>
/// 画折线图
/// </summary>
/// <param name="a_strFileName"> </param>
protected void DrawCurve(string a_strFileName)
{
//绘图准备,新建一个image对象,一个graphics对象
System.Drawing.Image myBmp = new Bitmap(m_intWidth , m_intHeight ) ;
System.Drawing.Graphics g = Graphics.FromImage(myBmp) ;

//填充背景
g.FillRectangle(new System.Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_intWidth , m_intHeight) ;

//如果没有任何回答,则显示没有结果
if (this.m_intTotalCount == 0)
{
g.DrawString("没有统计数字!" , new Font("宋体" , m_intWidth / 14) ,
new SolidBrush(Color.Red) , (m_intWidth - m_intWidth / 8 * 6) / 2 ,
m_intHeight / 2 - m_intWidth / 8) ;
}
else
{
//定义一个一个像素宽的黑色的笔
System.Drawing.Pen pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(Color.Black) , 1) ;

//画标志线
g.DrawLine(pen , 0 , 10 , 0 , m_intHeight / 3 * 2 + 10) ;
g.DrawLine(pen , 0 , m_intHeight / 3 * 2 + 10 , m_intWidth , m_intHeight / 3 * 2 + 10) ;
for (int i = 0 ; i < 10 ; i++)
{
g.DrawLine( pen , 0 , m_intHeight / 3 * 2 / 10 * i + 10 , 5 , m_intHeight / 3 * 2 / 10 * i + 10) ;
}

//写单位
g.DrawString("单位:" + m_strUnit , new System.Drawing.Font("宋体" , m_intWidth/40) ,
new SolidBrush(Color.Black) , 10 , 10) ;

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
22#
 楼主| 发表于 2006-9-23 14:21 | 只看该作者
//画折线

//计算宽度
int intWidth = m_intWidth / (m_arrItems.Count * 2) ;

//定义两个变量,记住上一个点的x , y
int ix = 0 ;
int iy = 0 ;

for ( int i = 0 ; i < m_arrItems.Count ; i ++)
{
ChartItem item = (ChartItem)m_arrItems ;

//计算所占百分比
float intPercent = (float)Decimal.Divide(item.Count * 100 , m_intTotalCount) ;

//计算点高度
int intHeight = (int)(m_intHeight / 3 * 2 * intPercent / 100) ;

//画点
g.FillEllipse(new SolidBrush(item.Color) , intWidth * (i * 2 + 1) ,
m_intHeight / 3 * 2 - intHeight , 10 , 10) ;

//连接线
//定义笔,如果是升则为蓝颜色,否则是红色
if (iy > m_intHeight /3 * 2 - intHeight + 5)
{
pen = new Pen(new SolidBrush(Color.Blue) , 3) ;
}
else
{
pen = new Pen(new SolidBrush(Color.Red) , 3) ;
}
if (i != 0)
{
g.DrawLine(pen , ix , iy , intWidth * (i * 2 + 1) + 5 , m_intHeight /3 * 2 - intHeight + 5) ;
}
ix = intWidth * (i * 2 + 1) + 5 ;
iy = m_intHeight / 3 * 2 - intHeight + 5 ;

//画饼图下方图例文字
//计算矩形大小
int intRectSize = m_intHeight / 3 / (m_arrItems.Count * 2) - 1 ;
intRectSize = intRectSize > 12 ? 12 : intRectSize ;

//画颜色方块
g.FillRectangle(new SolidBrush(item.Color) , intRectSize * 2 ,
intRectSize * (i * 2 + 1) + m_intHeight / 3 * 2 + 5
,intRectSize , intRectSize) ;
//写文字
g.DrawString(intPercent.ToInt32().ToString() + "% " + item.Text ,
new Font("宋体" , intRectSize) ,
new SolidBrush(item.Color) , intRectSize * 4 ,
intRectSize * ( i * 2 + 1) + m_intHeight / 3 * 2 + 5) ;
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
23#
 楼主| 发表于 2006-9-23 14:21 | 只看该作者
//清空
pen.Dispose() ;
}

//清控对象
g.Dispose() ;

//保存为jpg格式
try
{
myBmp.Save(a_strFileName , System.Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw(new Exception("保存图片失败:" + e.ToString())) ;
}

myBmp.Dispose() ;



}

}

/// <summary>
/// 统计图项目类
/// </summary>
/// <remarks>
/// 构造一个统计图项目,属性有要显示的文字、所占的百分比以及想显示的颜色。
/// </remarks>
public class ChartItem : object
{
/// <summary>
/// 要显示的文字
/// </summary>
private string m_strText ;

/// <summary>
/// 数量
/// </summary>
private int m_intCount ;


/// <summary>
/// 颜色
/// </summary>
private System.Drawing.Color m_objColor ;

//属性
public string Text
{
get
{
return m_strText ;
}
set
{
m_strText = value ;
}
}


public int Count
{
get
{
return m_intCount ;
}
set
{
m_intCount = value ;
}
}
public System.Drawing.Color Color
{
get
{
return m_objColor ;
}
set
{
m_objColor = value ;
}
}

/// <summary>
/// 构造函数
/// </summary>
public ChartItem()
{
m_strText = "" ;
m_intCount = 0 ;
m_objColor = Color.White ;
}

/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="a_strText"> </param>
/// <param name="a_intPercent"> </param>
/// <param name="a_objColor"> </param>
public ChartItem(string a_strText , int a_intCount , System.Drawing.Color a_objColor)
{
m_strText = a_strText ;
m_objColor = a_objColor ;
m_intCount = a_intCount ;
}
}
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
24#
 楼主| 发表于 2006-9-23 14:21 | 只看该作者
好了,现在万事俱备,只欠东风了,让我们看看现在做一个舆论调查多么简单:

file : vote.aspx


<%@ Page language="c#" Codebehind="vote.cs" AutoEventWireup="false" Inherits="Football.vote" %>

<html>
<head>
<title>532.com.cn --- 舆论调查 ---</title>
<link rel="stylesheet" href="style/style.css" type="text/css">

</head>
<body>

<form method="post" runat="server">
<table width=400 height=300 align=center>
<tr>
<td class=cn valign=top align=center><b>调查题目:
<asp:label id="lblSurveyTitle" runat=Server class=cn></asp:label>
</td>
</tr>
<tr>
<td alin=center>
<asp:image id="imgSurvey" runat=Server></asp:image>
</td>
</tr>
<tr>
<td align=center>
<input type=button onclick="window.close();" value="关闭此窗口">
</td>
</tr>
</table>
</form>

</body>
</html>

file: vote.cs
namespace Football
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
25#
 楼主| 发表于 2006-9-23 14:22 | 只看该作者
/// <summary>
/// Summary description for vote.
/// </summary>
public class vote : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image imgSurvey;
protected System.Web.UI.WebControls.Label lblSurveyTitle;

private string m_strSurveyID ;
private int m_intVoteID ;
public vote()
{
Page.Init += new System.EventHandler(Page_Init);
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}

protected void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
//
InitializeComponent();
Init() ;
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler (this.Page_Load);
}

private void Init()
{
m_strSurveyID = Request["surveyid"].ToString() ;
FootballSurvey mySurvey = new FootballSurvey() ;

try
{
m_intVoteID = Request["vote"].ToInt32() ;
mySurvey.LoadFromDatabase(m_strSurveyID) ;
lblSurveyTitle.Text = mySurvey.Title ;
if (m_intVoteID < mySurvey.Items.Count)
{
mySurvey.Vote(m_intVoteID) ;
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
26#
 楼主| 发表于 2006-9-23 14:22 | 只看该作者
mySurvey.CreateResultImage(Server.MapPath("survey.jpg" ,
MyClass.Util.MyChart.ChartType.Pie ,
300 ,300 , Color.White) ;
imgSurvey.ImageUrl = "survey.jpg" ;
}
catch(Exception e)
{
#if DEBUG
Response.Write ("初始化页面错误:" + e.ToString()) ;
return ;
#endif
Page.Navigate("error.aspx" ;
}

}
}
}

要看这个调查的效果,请到http://210.12.102.95/football 。怎么样,是不是觉得这种编程思路不错呢?什么?还不如直接在aspx文件里面做?不错,如果单做这么一个调查的确直接做要省事的多,但你知不知道,只要有编译好的这个类的dll,不管你怎么改页面,改数据结构,你都可以在15分钟内把你所需要的舆论调查做出来?这就是这两种编程方法最大的区别。希望通过这个例子你能学到一些编程思想把。

使用道具 举报

回复
论坛徽章:
289
红孩儿
日期:2006-04-14 22:29:56紫蜘蛛
日期:2006-04-14 22:31:56玉石琵琶
日期:2006-08-29 10:08:31生肖徽章:猴
日期:2016-09-12 17:09:52生肖徽章:猴
日期:2016-09-12 17:09:52生肖徽章:猴
日期:2016-09-12 17:01:35生肖徽章:猴
日期:2016-09-12 17:01:35生肖徽章:猴
日期:2016-09-12 17:01:35生肖徽章:猴
日期:2016-09-12 17:01:35生肖徽章:猴
日期:2016-09-12 17:01:35
27#
发表于 2006-9-24 18:26 | 只看该作者
希望今后贴附件,不要拆开贴,我已改正,希望你也努力.

使用道具 举报

回复
论坛徽章:
0
28#
发表于 2006-10-4 05:57 | 只看该作者
If you can read Patterns of Enterprise Application Architecture from Martin Fowler, you can do it better!!!

使用道具 举报

回复
论坛徽章:
8
29#
发表于 2006-10-4 21:14 | 只看该作者
??????

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表