2012-03-02 4 views
0

Продолжение темы: C# Invalidate troubles. Я создал класс, но теперь я получаю оператор Error 2 Embedded не может быть объявлением или помеченной инструкцией. И я пытаюсь создать «Автомобиль» на автомобиле aCar = новый автомобиль (50 100); вот где я получаю ошибку. Спасибо за предложения.C# класс и графика

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) 
    { 

    } 
+0

Вы говорите, что ошибка происходит на «Car aCar = new Car (50, 100)», за исключением того, что в вашем примере не существует строки. Пожалуйста, покажите нам, что раздел вашего кода. –

+0

FYI, это не форум для обсуждения. Вы задаете новый вопрос, связанный с другим вопросом. Вы не продолжаете нить. –

ответ

0

Вы должны пройти графический объект, вы получаете от события Paint:

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

Так что, когда вы это называете:

aCar.printCar(e.Graphics); 
1

Конец вашего метода 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 
} 
Смежные вопросы