在 C# 中绘制椭圆

本文关键字:绘制 | 更新日期: 2023-09-27 18:34:39

我正在使用onPaint方法在某些坐标处画一条线。这是整个代码:

namespace Lunar_Lander{
public partial class Form1 : Form
{
    System.Media.SoundPlayer player= new System.Media.SoundPlayer();
    System.Media.SoundPlayer player1 = new System.Media.SoundPlayer();
    private double x,y;
    public static double dx,dy;
    private int gorivo = 1000;
    private int zivoti = 3;
    //private int score = 0;
    public System.Drawing.Graphics g;
    public System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
    static int th = 16;
    static int t = 0;
    static Color[] c = new Color[10];
    public Form1()
    {
        InitializeComponent();
        Thread t1 = new Thread(new ThreadStart(SplashForm));
        t1.Start();
        Thread.Sleep(3500); 
        t1.Abort();
        Thread.Sleep(1000);
        //zvuk();
        initGame();
        cSet();
    }
    private void SplashForm()
    {
        Form2 newSplashForm = new Form2();
        newSplashForm.ShowDialog();
        newSplashForm.Dispose();
    }

    void cSet()
    {
        c[0] = Color.Red;
        c[1] = Color.Blue;
        c[2] = Color.Yellow;
        c[3] = Color.Green;
        c[4] = Color.Pink;
        c[5] = Color.Brown;
        c[6] = Color.Violet;
        c[7] = Color.White;
        c[8] = Color.Silver;
        c[9] = Color.Purple;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.PageUnit = GraphicsUnit.Inch;
        Pen MyPen = new Pen(c[t], th / g.DpiX);
        if(v>2)
        g.DrawLine(MyPen, Convert.ToInt32(picLander.Location.X), Convert.ToInt32(picLander.Location.Y), 1, 1);
        g.DrawEllipse(MyPen, Convert.ToInt32(picLander.Location.X), Convert.ToInt32(picLander.Location.Y), 10, 10);
    }
    static int v=0;
    private void zvuk()
    {
        player.Stream = Properties.Resources.System_Shock_2___Engsong;
        player.PlayLooping();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        v++;
        dy += .1;
        picLander.Image = myPics.Images[0];
        kretanje();
        Statistika();
        this.SetStyle(ControlStyles.ResizeRedraw, true); 
    }

    private void kretanje()
    {
        x += dx;
        if (x > this.Width - picLander.Width)
        {
            x = 0;
        } 
        if (x < 0)
        {
            x = Convert.ToDouble(this.Width - picLander.Width);
        } 
        y += dy;
        if (y > this.Height - picLander.Height)
        {
            y = 0;
        } 
        if (y < 0)
        {
            y = Convert.ToDouble(this.Height - picLander.Height);
        } 
        picLander.Location = new Point(Convert.ToInt32(x),Convert.ToInt32(y));
    }
    public void Statistika()
    {
        lbldx.Text = "Horizontalna brzina: " + Math.Round(dx,2);
        lbldy.Text = "Vertikalna brzina: " + Math.Round(dy,2);
        lblGorivo.Text = "Gorivo: " + gorivo;
        lblZivoti.Text = "Brodova: " + zivoti;
        //lblScore.Text = "score: " + score;
        debljina.Text = "Debljina: " + th;
        switch (t)
        {
            case 0:
                boja.Text = "Boja:Crvena";
                break;
            case 1:
                boja.Text = "Boja:Plava";
                break;
            case 2:
                boja.Text = "Boja:Zuta";
                break;
            case 3:
                boja.Text = "Boja:Zelena";
                break;
            case 4:
                boja.Text = "Boja:Pink";
                break;
            case 5:
                boja.Text = "Boja:Braon";
                break;
            case 6:
                boja.Text = "Boja:Ljubicasta";
                break;
            case 7:
                boja.Text = "Boja:Bela";
                break;
            case 8:
                boja.Text = "Boja:Srebrna";
                break;
            case 9:
                boja.Text = "Boja:Purpurna";
                break;
        }
    }
    public void Kraj(){
    DialogResult odgovor;
    zivoti--;
    if (zivoti <= 0){
    odgovor = MessageBox.Show("Igraj ponovo?","Kraj Igre",MessageBoxButtons.YesNo);
    if (odgovor == DialogResult.Yes){
    zivoti = 3;
    gorivo = 1000;
    initGame();
    } else {
    Application.Exit();
    } 
    }}
    public void initGame()
    {
        Random roller = new Random();
        dx = Convert.ToDouble(roller.Next(5) - 2);
        dy = Convert.ToDouble(roller.Next(5) - 2);
        x = Convert.ToDouble(roller.Next(this.Width));
        y = Convert.ToDouble(roller.Next(this.Height));
        gorivo = 1000;
        timer1.Enabled = true;
    }
       private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           gorivo--;
           if (gorivo < 0)
           {
               timer1.Enabled = false;
               MessageBox.Show("Nestalo Vam je goriva!!");
               gorivo += 40;
               Kraj();
               initGame();
           }
           switch (e.KeyData)
           {
               case Keys.Up:
                   picLander.Image = myPics.Images[1];
                   dy -= 0.8;
                   break;
               case Keys.Left:
                   picLander.Image = myPics.Images[2];
                   dx=dx-0.8;
                   break;
               case Keys.Right:
                   picLander.Image = myPics.Images[3];
                   dx=dx+0.8;
                   break;
               case Keys.Down:
                   picLander.Image = myPics.Images[4];
                   dy += 0.8;
                   break;
               case Keys.W:
                   th++;
                   break;
               case Keys.S:
                   {
                       if (th == 0) th = 0;
                       else th--;
                   }
                   break;
               case Keys.A:
                   { if (t == 9)t = 0; else t++; }
                   break;
               case Keys.D:
                   { if (t == 0)t = 9; else t--; }
                   break;
               default:
                   break;
           } 
       }
    }
}

这条线画得很成功,但我没有画椭圆。我错过了什么吗?当我尝试在某些静态坐标上绘制椭圆时,它可以工作。

在 C# 中绘制椭圆

protected override void OnPaint(PaintEventArgs e)
    {
        var th = 10.0f;
        var picLander = new PointF(0.5f, 0.5f);

        Graphics g = e.Graphics;
        g.PageUnit = GraphicsUnit.Inch;
        Pen MyPen = new Pen(Color.Black, th / g.DpiX);
        g.DrawLine(MyPen, Convert.ToInt32(picLander.X), Convert.ToInt32(picLander.Y), 1, 1);
        g.DrawEllipse(MyPen, Convert.ToInt32(picLander.X), Convert.ToInt32(picLander.Y), 3, 5);
    }

试过了,效果很好

我不确定你在做什么。我设置了一个计时器来动态更改picLander.XpicLander.Y值,并且 elipse 确实会移动。这是我使用的代码。你可能希望在g.DrawEllipse调用上放置断点并验证坐标。

法典。

public partial class Form1 : Form
{
    PointF picLander = new PointF(0.5f, 0.5f);
    static int th = 16;
    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }
     protected override  void OnPaint( PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.PageUnit = GraphicsUnit.Inch;
        Pen MyPen = new Pen(Color.Black, th / g.DpiX);
        g.DrawLine(MyPen, Convert.ToInt32(picLander.X), Convert.ToInt32(picLander.Y), 1, 1);
        g.DrawEllipse(MyPen, Convert.ToInt32(picLander.X), Convert.ToInt32(picLander.Y), 3, 5);
        this.Text = picLander.X.ToString() + "----" + picLander.Y.ToString();
    } 

     private void timer1_Tick(object sender, EventArgs e)
     {
         picLander.X = picLander.X +1;
         picLander.Y = picLander.Y +1;
         Invalidate();

     }
}