C# class and Graphics

本文关键字:Graphics and class | 更新日期: 2023-09-27 18:26:33

线程的继续:C#使故障无效。我创建了这个类,但现在我得到了一个错误2嵌入式语句,它不能是声明或标记语句。我正试图通过Car aCar=新车(50100)创建"汽车";这就是我犯错误的地方。谢谢你的建议。

  class Car
    {
     private Pen pen1 = new Pen(Color.Blue, 2F);
     private Pen pen2 = new Pen(Color.Green, 2F);
     int cost = 0; 
      int x, y;
      Graphics g;
public Car(int x, int y)
{
    this.x = x;
    this.y = y;
}
public void printCar()
{
    g.DrawEllipse(pen1, x, y, 30, 30);
    g.DrawEllipse(pen1, x + 100, y, 30, 30);
    g.DrawRectangle(pen2, x - 5, y + 50, 140, 50);
    g.DrawLine(pen2, x + 15, y + 50, x + 30, y + 90);
    g.DrawLine(pen2, x + 30, y + 90, x + 90, y + 90);
    g.DrawLine(pen2, x + 90, y + 90, x + 110, y + 50);
    // Create string to draw.
    String drawString = "Price: " + (cost).ToString("C");
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    // Create point for upper-left corner of drawing.
    PointF drawPoint = new PointF(50, 95);
    // Draw string to screen.
    g.DrawString(drawString, drawFont, drawBrush, drawPoint);

} 

公共分部类Form1:Form{

    private Pen pen1 = new Pen(Color.Blue, 2F);
    private Pen pen2 = new Pen(Color.Green, 2F);
    private double cost ;
    private int days = 0;
    private double air;
    private double automatic;
    int count;
    int m = 50;
    Car aCar = new Car(50, 60);
    public Form1()
    {
        InitializeComponent();
        days = int.Parse(textBox1.Text);
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "Van")
        {
            cost = 110;
            label1.Text = "The Cost of van per day" + (cost).ToString("C");
            textBox1.Text = "1";
            textBox1.Focus();
        }
        else if (comboBox1.SelectedItem.ToString() == "Car")
        {
            cost = 85.20;
            label1.Text = "The Cost of car per day" + (cost).ToString("C");
            textBox1.Text = "1";
            textBox1.Focus();
        }
        else
        {
            cost = 135;
            label1.Text = "Van" + (cost).ToString("C");
            textBox1.Text = "1";
            textBox1.Focus();
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        count++;
        button1.Text = "Move";
        pictureBox1.Invalidate();
        if(count == 2)
            button1.Text = "Reset";
        if (count == 3)
        {
            textBox1.Text = "0";
            count = 0;
            comboBox1.Text = "select type of vehical";
            checkBox1.Checked = false;
            checkBox2.Checked = false;
        }
    }
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
       e.Graphics.Clear(Color.White);
       if (count == 1)
       aCar.printCar();
      if (count == 2)

}

    private void Form1_Load(object sender, EventArgs e)
    {
    }

C# class and Graphics

picturebox1_Paint方法的末尾打开一个if语句,但实际上并不包含正文。这是非法的:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
   e.Graphics.Clear(Color.White);
   if (count == 1)
   aCar.printCar();
   if (count == 2)  // <-- This is illegal since the if statement is just dangling
}

您必须传递从Paint事件中获得的Graphic对象:

Public void printCar(Graphic g)
{
  // etc, etc
}

所以当你称之为:

aCar.printCar(e.Graphics);