c#新建一个画图的类,里面画坐标轴方法的Graphics g 如何赋初值?
我新建一个winform,添加一个panel1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] stationname = { "宋家庄站", "刘家窑站" };
double[] stationposition = { 0.424, 2.095 };
private void panel1_Paint(object sender, PaintEventArgs e)
{
Draw draw1 = new Draw();
draw1.DrawAxis(stationposition, stationname);
}
}
然后新建一个class Draw的类
class Draw
{
//画布
//背景
//坐标原点
int x=100;
int y=540;
//位置放大比例
double stationposition_ratio = 500;
//字体
Font fontO = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font fontT = new System.Drawing.Font("宋体", 12, FontStyle.Regular);
//画笔
Pen traction_pen = new Pen(Color.Red, 2);
Pen coasting_pen = new Pen(Color.Green, 2);
Pen brake_pen = new Pen(Color.Blue, 2);
Pen axis_pen = new Pen(Color.White);
//画刷
Brush brush = new SolidBrush(Color.White);
//画坐标线
public void DrawAxis(double[] stationposition,string[] stationname)
{
Graphics g = CreateGraphics();
//背景色
g.Clear(Color.Black);
//车站中心线
for (int i = 0; i
{
g.DrawLine(axis_pen, x + (int)(stationposition[i] * stationposition_ratio), y, x + (int)(stationposition[i] * stationposition_ratio), y - 500);
g.DrawLine(axis_pen, x + (int)(stationposition[i] * stationposition_ratio - 50), y - 500, (int)(x + stationposition[i] * stationposition_ratio + 50), y - 500);
g.DrawString(stationname[i] + " " + stationposition[i].ToString() + "km", fontT, brush, (int)(x + stationposition[i] * stationposition_ratio - 70), y - 520);
}
//y轴-速度
g.DrawLine(axis_pen, x, y, x, y - 500);
int k = y;
string[] v_value = { "0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100" };
for (int i = 0; i
{
g.DrawString(v_value[i].ToString(), fontO, brush, x - 30, k);