练习1
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//创建Graphics对象
Graphics GPS = this.CreateGraphics();
//创建黑色pen对象
Pen MyPen = new Pen(Color.Black, 2f);
//确定起点和终点
Point pt1 = new Point(270, 100);
Point pt2 = new Point(270, 150);
//使用DrawLine方法绘制直线
GPS.DrawLine(MyPen, pt1, pt2);
//改变坐标原点为第一个点
GPS.TranslateTransform(270,100);
//每隔15度
int angle = 15;
for (int i = 0; i <= 9; ++i)
{
GPS.RotateTransform(angle);
GPS.DrawLine(MyPen, 0, 0, 300, 20);
}
}
}
}
运行结果
练习2
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsFormsApp18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Point center = new Point(200, 100);
int radius = 80;
Point[] pts = new Point[5];
//获取五角星5个顶点
pts[0] = new Point(center.X, center.Y - radius);
pts[1] = Rotate72(pts[0], center);
pts[2] = Rotate72(pts[1], center);
pts[3] = Rotate72(pts[2], center);
pts[4] = Rotate72(pts[3], center);
//简单地拉5条线
Pen pen = new Pen(new SolidBrush(Color.Red));
g.DrawLine(pen, pts[0], pts[2]);
g.DrawLine(pen, pts[0], pts[3]);
g.DrawLine(pen, pts[1], pts[3]);
g.DrawLine(pen, pts[1], pts[4]);
g.DrawLine(pen, pts[2], pts[4]);
/*
Point vOrigin = new Point(100, 100); // 圆心
int vRadius = 70; // 半径
Point[] vPoints = new Point[5];
double vAngle = 2.0 * Math.PI / 4 + Math.PI;
for (int i = 0; i < vPoints.Length; i++)
{
vAngle += 2.0 * Math.PI / (double)vPoints.Length;
vPoints[i] = new Point(
(int)(Math.Cos(vAngle) * vRadius) + vOrigin.X,
(int)(Math.Sin(vAngle) * vRadius) + vOrigin.Y);
}
e.Graphics.DrawPolygon(Pens.Blue, vPoints);
for (int i = 0; i < vPoints.Length; i++)
{
vPoints[i].Offset(150, 0);
}
Point[] vTemps = new Point[vPoints.Length];
vTemps[0] = vPoints[0];
vTemps[1] = vPoints[2];
vTemps[2] = vPoints[4];
vTemps[3] = vPoints[1];
vTemps[4] = vPoints[3];
e.Graphics.DrawPolygon(Pens.Red, vTemps);
}
*/
}
//旋转72度
private Point Rotate72(Point pt, Point center)
{
int x = (int)(center.X + (pt.X - center.X) * Math.Cos(72.0 * Math.PI / 180) - (pt.Y - center.Y) * Math.Sin(72.0 * Math.PI / 180)),
y = (int)(center.Y + (pt.X - center.X) * Math.Sin(72.0 * Math.PI / 180) + (pt.Y - center.Y) * Math.Cos(72.0 * Math.PI / 180));
return new Point(x, y);
}
}
}
运行结果
练习3
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SolidBrush SB;//定义单色画刷
Graphics g;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch(comboBox1.SelectedIndex)
{
case 0:
{
//填充矩形
g.Clear(BackColor);//通过清除背景达到清除之前的图形的目的
Rectangle myR = new Rectangle(40, 60, 400, 200);
g.FillRectangle(SB, myR);
break;
}
case 1:
{
//填充多边形
//定义多边形顶点
g.Clear(BackColor);//通过清除背景达到清除之前的图形的目的
Point pt1 = new Point(90, 30);
Point pt2 = new Point(50, 60);
Point pt3 = new Point(90, 90);
Point pt4 = new Point(170, 90);
Point pt5 = new Point(210, 60);
Point pt6 = new Point(170, 30);
//定义point结构数组,用来表示多边形的点
Point[] curvePoints = { pt1, pt2, pt3, pt4, pt5, pt6 };
g.FillPolygon(SB, curvePoints);
break;
}
default:
{
g.Clear(BackColor);//通过清除背景达到清除之前的图形的目的
Rectangle Rec = new Rectangle(40, 60, 70, 70);
g.FillEllipse(SB, Rec);
break;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
SB = new SolidBrush(Color.Red);//创建单色画刷对象
g = this.CreateGraphics();//创建Graphics对象
}
}
}