C#类和图形
线程的延续:。我创建了这个类,但现在我得到一个错误2嵌入语句不能是声明或标记语句。我试图通过Car aCar=newcar(50100)创建“Car”;这就是我得到错误的地方。 谢谢你的建议C#类和图形,c#,forms,graphics,C#,Forms,Graphics,线程的延续:。我创建了这个类,但现在我得到一个错误2嵌入语句不能是声明或标记语句。我试图通过Car aCar=newcar(50100)创建“Car”;这就是我得到错误的地方。 谢谢你的建议 class Car { private Pen pen1 = new Pen(Color.Blue, 2F); private Pen pen2 = new Pen(Color.Green, 2F); int cost = 0; int x, y;
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
{
}
picturebox1\u-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
}
private void pictureBox1\u Paint(对象发送方,PaintEventArgs e)
{
e、 图形。清晰(颜色。白色);
如果(计数=1)
aCar.printCar();
如果(count==2)/必须传递从绘制事件中获取的图形对象:
Public void printCar(Graphic g)
{
// etc, etc
}
所以当你称之为:
aCar.printCar(e.Graphics);
您说错误发生在Car aCar=new Car(50,100)
上,但您的示例中不存在该行。请向我们展示您代码的该部分。仅供参考,这不是一个讨论论坛。您正在问一个与另一个问题相关的新问题。您没有继续一个线程。
Public void printCar(Graphic g)
{
// etc, etc
}
aCar.printCar(e.Graphics);