1

I'm making a text game with a solar clock that will be drawn to a panel during run-time. I've noticed that I can't exactly just reference the panel (even after making it public) and call DrawEllipse() directly from another class. I will need to update the position of the circle being drawn during run-time, and I'm not exactly sure how to pass in information to panel_Paint().

what I'd like to do is this:

public Class Circle
   {
      public void Draw()
         Graphics g = panel.CreateGraphics();
         Pen p = new Pen(Color.White);
         g.DrawEllipse(p,100,100,50,50);
   {

I have panel set to public so I can access it. This had failed me, so I tried going with:

public partial class Form : System.Windows.Forms.Form
{
    public Form()
    {
        InitializeComponent();
    }

    public void DrawCircle()
    {
        Graphics g = panel1.CreateGraphics();
        Pen p = new Pen(Color.White);
        g.DrawEllipse(p, 100, 100, 50, 50);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void gameText_TextChanged(object sender, EventArgs e)
    {

    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }
}

and then calling DrawCircle() from main(), but this had also lead to more failure. yay... so I tried again with:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form game = new Form();

        var t = new Vec(1, 1);
        var m = +t;

        Clock c = new Clock();

        Graphics g = game.panel1.CreateGraphics();

        game.DrawCircle();

        game.gameText.Text = m.ToString("0.#####");

        Application.Run(game);
    }

and still doesn't work. so I tried accessing panel from my Utils class, can't do that, I've tried pulling the game variable out of the main class, can't do that, I've tried making it public, and I can't do that. I'm seriously at a loss here. any ideas or suggestions would be greatly appreciated.

EDIT: I've looked into the similar post here, but I have no idea where to declare panel1.Paint += new PaintEventHandler(panel1_Paint); like, where can I put it, and how do I use it in the external classes. It says in the constructor, but constructor of what class? the one that sends the draw information? or some other class? some clarification on the proper implementation of what was described.

Raxmo
  • 57
  • 5
  • 1
    Use the Paint event to draw stuff, not CreateGraphics. The most basic search of related posts here will explain that over and over and over – Ňɏssa Pøngjǣrdenlarp Apr 17 '18 at 19:32
  • 1
    `Graphics g = panel.CreateGraphics();` is almost always a bad idea. Call `Invalidate` and draw there. – Ron Beyer Apr 17 '18 at 19:40
  • Not entirely sure how the Paint event really works. after looking over the post listed in these comments again, am I to understand that I can simply have a paint event in another class and then handle everything in the panel_paint method? I suppose it could work for my particular implementation, seems rather... bloatie for something that would require more draw calls though. – Raxmo Apr 17 '18 at 19:57
  • Seems but that's how it works. Remember that the system also needs to refresh stuff all the time; to do so it also call the Paint event. If you have a really large number of drawXXX calls you can cache them in a bitmap. To avoid flicker make sure to make the Panel [doublebuffered](https://stackoverflow.com/questions/32919918/how-to-draw-line-and-select-it-in-panel/32920894#32920894) – TaW Apr 17 '18 at 20:44
  • CreateGraphics is a temporary buffer. You draw it before the form is shown, which is why your code didn't work. – LarsTech Apr 17 '18 at 21:05
  • Okay, so where do I put the PaintEventHandler? – Raxmo Apr 17 '18 at 22:37
  • In the form's constructor. – LarsTech Apr 18 '18 at 03:17
  • so in public Form(){ InitializeComponent; panel1.Paint += new PaintEventHandler(panel1_Paint);} something like that? – Raxmo Apr 22 '18 at 08:50
  • also, how do I use this new paint event handler in external classes? – Raxmo Apr 22 '18 at 08:58

0 Answers0