题目(2和3):
第2题的界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int x= pictureBox1.Width/2;
int y= pictureBox1.Height/2;
Point mypoint = new Point(x, y);
Pen mypenline = new Pen(Color.Black, 2);//设置颜色和宽度
Graphics g = e.Graphics;
g.TranslateTransform(x, y); // 将原点移至pictureBox中心
//绘制X、Y轴
g.DrawLine(mypenline, -mypoint.X, 0, mypoint.X, 0);
g.DrawLine(mypenline, mypoint.X - 10, -10, mypoint.X, 0);
g.DrawLine(mypenline, mypoint.X - 10, 10, mypoint.X, 0);
g.DrawLine(mypenline, 0, -mypoint.Y, 0, mypoint.Y);
g.DrawLine(mypenline, -10, -mypoint.Y + 10, 0, -mypoint.Y);
g.DrawLine(mypenline, 10, -mypoint.Y + 10, 0, -mypoint.Y);
//绘制原点
RectangleF mytangle = new RectangleF(-3f, -3f, 6f, 6f);
g.FillEllipse(Brushes.Black, mytangle);
//直接用了路径的类,这个类对象方法可以里面可以画出一个椭圆,只需要给出坐标位置和长宽
GraphicsPath myPath2 = new GraphicsPath();
myPath2.AddEllipse(-105, -50, 150, 100);//参数的设置怎么感觉要调很久??
//以宽度为1、绿线实线绘制路径myPath2
mypenline.Width = 1;
mypenline.Color = Color.Black;
g.DrawPath(mypenline, myPath2);
Matrix myMatrix = new Matrix();
for (int i = 1; i <= 1000; i = i + 1)
{
myMatrix.Reset();
myMatrix.RotateAt(5, new Point(0, 0));//后面的点代表以哪个点为中心进行旋转
myPath2.Transform(myMatrix);
g.DrawPath(mypenline, myPath2);
}
}
}
}
比较简单,同样也是需要掌握如何进行平移旋转。
第3题的界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
//注释版本
public partial class Form1 : Form
{
enum GraphType
{
Empty,Rectangle, Ellipse, Polygon, ClosedCurve//末尾不需要分号
}
GraphType myGraphType=GraphType.Empty;//初始为当前图形类型
Bitmap myBitmap;// 用于绘制的位图对象
Graphics myBMPGraphics, myPicGraphics;//用于绘制的位图和图片的绘图对象
Pen myPen, // 画笔对象,用于绘制边框
myClearTrackPen, // 画笔对象,用于擦除绘制路径
myOutlinePen;// 画笔对象,用于绘制轮廓线
bool startDrawMark = false;// 标记是否开始绘制
Point startPt, prePt, preTrackPt;// 开始绘制的起点、上一个绘制点、上一个绘制路径点
Brush myBrush = new SolidBrush(Color.Blue); // 画刷对象,用于填充颜色
List<Point> myCurvePts = new List<Point>();// 存储绘制曲线的点集合
List<Point> myPolygonPts = new List<Point>(); // 存储绘制多边形的点集合
public Form1()
{
InitializeComponent();
myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);// 创建指定大小的位图对象
myBMPGraphics = Graphics.FromImage(myBitmap); 从位图中创建绘图对象
myPicGraphics = this.pictureBox1.CreateGraphics(); // 创建图片的绘图对象
myPen = new Pen(Color.Blue, 3);
myBrush = new SolidBrush(this.BackColor); // 创建黑色的画刷对象
myClearTrackPen = new Pen(pictureBox1.BackColor, 3); // 创建背景色对应的画笔对象,用于擦除绘制路径
myOutlinePen = new Pen(Color.Red, 1); //画轮廓线的笔
myOutlinePen.DashStyle = DashStyle.Dot; // 设置轮廓线的样式为虚线
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
switch (myGraphType)
{
case GraphType.Rectangle:
startDrawMark = true;
startPt = e.Location;
preTrackPt = startPt;
break;
case GraphType.Ellipse:
startDrawMark = true;
startPt = e.Location;
preTrackPt = startPt;
break;
case GraphType.Polygon:
if (e.Clicks == 1)
{
if (startDrawMark == false)// 如果之前没有开始绘制
{
startDrawMark = true;
startPt = e.Location;
prePt = startPt;
preTrackPt = startPt;
myPolygonPts.Clear(); // 清空多边形点集合
myPolygonPts.Add(e.Location); // 将当前点添加到点集合中
}
else// 如果已经开始绘制多边形
{
myBMPGraphics.DrawLine(myPen, prePt, e.Location); // 绘制当前点和上一个点之间的线段
prePt = e.Location;// 更新上一个点为当前点
preTrackPt = e.Location;
myPolygonPts.Add(e.Location); // 将当前点添加到点集合中
}
}
if (e.Clicks == 2)//双击
{
myBMPGraphics.DrawLine(myPen, e.Location, startPt);// 绘制最后一个点和起点之间的线段
myBMPGraphics.FillPolygon(myBrush, myPolygonPts.ToArray()); // 填充多边形区域
startDrawMark = false;
// 清空多边形点集合(因为可能需要再次绘制)
myPolygonPts.Clear();
}
break;
case GraphType.ClosedCurve:
if (e.Clicks == 1)
{
if (startDrawMark == false)
{
startDrawMark = true;
startPt = e.Location;
prePt = startPt;
preTrackPt = startPt;
myCurvePts.Clear();
myCurvePts.Add(e.Location);
}
else
{
myPicGraphics.DrawLine(myClearTrackPen, prePt, e.Location);
myBMPGraphics.DrawLine(myOutlinePen, prePt, e.Location);
prePt = e.Location;
preTrackPt = e.Location;
myCurvePts.Add(e.Location);// 将当前点添加到点集合中
}
}
if (e.Clicks == 2)
{
myBMPGraphics.DrawLine(myOutlinePen, e.Location, startPt);
if (myCurvePts.Count >= 3)
{
myBMPGraphics.DrawClosedCurve(myPen, myCurvePts.ToArray());
myBMPGraphics.FillClosedCurve(myBrush, myCurvePts.ToArray()); // 填充曲线区域
}
startDrawMark = false;
}
break;
}
myPicGraphics.DrawImage(myBitmap, 0, 0);// 将位图绘制到图片上
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (startDrawMark)
{
switch (myGraphType)
{ case GraphType.Rectangle:
myPicGraphics.DrawRectangle(myClearTrackPen, Math.Min(startPt.X, preTrackPt.X), Math.Min(startPt.Y, preTrackPt.Y),
Math.Abs(preTrackPt.X - startPt.X), Math.Abs(preTrackPt.Y - startPt.Y));
myPicGraphics.DrawRectangle(myPen, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y), Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));//可以实时显示画的边框
preTrackPt = e.Location;
break;
case GraphType.Ellipse:
myPicGraphics.DrawEllipse(myClearTrackPen, Math.Min(startPt.X, preTrackPt.X), Math.Min(startPt.Y, preTrackPt.Y),
Math.Abs(preTrackPt.X - startPt.X), Math.Abs(preTrackPt.Y - startPt.Y));
myPicGraphics.DrawEllipse(myPen, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y), Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
preTrackPt = e.Location;
break;
case GraphType.Polygon:
myPicGraphics.DrawLine(myClearTrackPen, prePt, preTrackPt);
myPicGraphics.DrawLine(myPen, prePt, e.Location);
preTrackPt = e.Location;
break;
case GraphType.ClosedCurve:
myPicGraphics.DrawLine(myClearTrackPen, prePt, preTrackPt);
myPicGraphics.DrawLine(myPen, prePt, e.Location);
preTrackPt = e.Location;
break;
}
myPicGraphics.DrawImage(myBitmap, 0, 0);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
switch (myGraphType)
{
case GraphType.Rectangle:
myBMPGraphics.DrawRectangle(myPen, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y),
Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
myBMPGraphics.FillRectangle(myBrush, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y),
Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
startDrawMark = false;
break;
case GraphType.Ellipse:
myBMPGraphics.DrawEllipse(myPen, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y),
Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
myBMPGraphics.FillEllipse(myBrush, Math.Min(startPt.X, e.X), Math.Min(startPt.Y, e.Y),
Math.Abs(e.X - startPt.X), Math.Abs(e.Y - startPt.Y));
startDrawMark = false;
break;
}
myPicGraphics.DrawImage(myBitmap, 0, 0);
}
private void 矩形ToolStripMenuItem1_Click(object sender, EventArgs e)
{
myGraphType = GraphType.Rectangle;
}
private void 椭圆ToolStripMenuItem1_Click(object sender, EventArgs e)
{
myGraphType = GraphType.Ellipse;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void 多边形ToolStripMenuItem1_Click(object sender, EventArgs e)
{
myGraphType = GraphType.Polygon;
}
private void 闭合曲线ToolStripMenuItem1_Click(object sender, EventArgs e)
{
myGraphType = GraphType.ClosedCurve;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// 将位图绘制到图片上//重绘
e.Graphics.DrawImage(myBitmap, 0, 0);
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
ColorDialog myColorDialog = new ColorDialog();
myColorDialog.AllowFullOpen = true;
myColorDialog.Color = myPen.Color;
if (myColorDialog.ShowDialog() == DialogResult.OK)
{
myPen.Color = myColorDialog.Color;
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
ColorDialog myColorDialog = new ColorDialog();
myColorDialog.AllowFullOpen = true;
myColorDialog.Color = ((SolidBrush)myBrush).Color; // 设置颜色选择对话框的初始颜色为当前填充画刷的颜色
if (myColorDialog.ShowDialog() == DialogResult.OK)
{
myBrush = new SolidBrush(myColorDialog.Color);
}
}
}
}
代码比较长,加了题目上的一些功能,比如除了画椭圆,还需要画直线,画折线,大家可以根据代码中的名称标注去找,有中文。除此之外,还加了边界颜色、填充等。