Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms graphics not being drawn

I have some code that looks pretty simple and should draw an ellipse, but it doesn't seem to appear. Here is my code:

public partial class ThreeBodySim : Form
{

    public ThreeBodySim()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        Graphics graphics = displayPanel.CreateGraphics(); // Separate panel to display graphics
        Rectangle bbox1 = new Rectangle(30, 40, 50, 50);
        graphics.DrawEllipse(new Pen(Color.AliceBlue), bbox1);
    }
}

Am I missing something important?

like image 498
Jon Martin Avatar asked Oct 28 '25 05:10

Jon Martin


1 Answers

Use the Paint() event to draw on your form. I recommend using a PictureBox on the form as it will not have as much flicker.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        this.DoubleBuffered=true;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Rectangle bbox1=new Rectangle(30, 40, 50, 50);
        e.Graphics.DrawEllipse(new Pen(Color.Purple), bbox1);
    }

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }
}

Form

like image 74
John Alexiou Avatar answered Oct 30 '25 22:10

John Alexiou