-1

EDIT: This is not a duplicate of "How to use the OnPaint event in C#" because I didn't even know it existed. I am asking a question, when I don't know the answer. It's like asking "How do I check oil in my car" when i don't even know what a dip stick is. Please remove the duplicate sign if this edits it in a way that explains why it is not a duplicate.

I am trying to make a random terrain generator. At the moment, I am using Windows Forms Graphics to draw it all. I can't seem to get the engine to work. The following code ran does not display anything on the form, even though it should draw a black rectangle from 0,0 to 50,50. Can anyone solve?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Random_Terrain_Generator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Engine e = new Engine(100, 100);
            e.Color3(Color.Blue);
            e.rect(new Rectangle(0,0,50,50));
            Graphics fg = this.CreateGraphics();
            fg.DrawImage(e.getBuffer(), 1, 100, 100, 100);
        }
    }

    public class Engine
    {
        Color fillColor;
        Bitmap buffer;
        Graphics bufferGraphics;

        public Engine(int bufferWidth, int bufferHeight)
        {
            buffer = new Bitmap(bufferWidth, bufferHeight);
            bufferGraphics = Graphics.FromImage(buffer);
        }

        public void fRect(Rectangle r)
        {
            bufferGraphics.FillRectangle(new SolidBrush(fillColor), r);
        }

        public void rect(Rectangle r)
        {
            bufferGraphics.DrawRectangle(new Pen(fillColor), r);
        }

        public void Color3(Color c)
        {
            fillColor = c;
        }

        public Bitmap getBuffer()
        {
            return buffer;
        }
    }
}
Noah Heber
  • 82
  • 1
  • 12
  • 2
    You did everything in the form constructor. Once the form paints, your box is gone. Forms are constantly repainting when it feels it needs to update visually. That means you need to paint every time it does. http://stackoverflow.com/questions/10076116/how-to-use-the-onpaint-event-in-c – TyCobb Dec 21 '16 at 22:54
  • would it help if i put it in the Form Load event? i am doing it after initializeConponent, so i would assume it would paint after the form. – Noah Heber Dec 21 '16 at 22:56
  • 1
    No, you have to override the OnPaint method so when it's done painting, you can paint over it. Look at the example I linked. – TyCobb Dec 21 '16 at 22:56
  • ok ill try that, thanks! I'll edit this comment if it does not work. – Noah Heber Dec 21 '16 at 22:57

1 Answers1

1

This worked! Thanks to TyCobb for the quick answer.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Random_Terrain_Generator
{
    public partial class Form1 : Form
    {
        Engine e;

        public Form1()
        {
            InitializeComponent();

            e = new Engine(100, 100);
            e.Color3(Color.Blue);
            e.fRect(new Rectangle(0,0,50,50));
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Call the OnPaint method of the base class.
            base.OnPaint(pe);
            pe.Graphics.DrawImage(e.getBuffer(), 1, 1, 100, 100);

        }
    }

    public class Engine
    {
        Color fillColor;
        Bitmap buffer;
        Graphics bufferGraphics;

        public Engine(int bufferWidth, int bufferHeight)
        {
            buffer = new Bitmap(bufferWidth, bufferHeight);
            bufferGraphics = Graphics.FromImage(buffer);
        }

        public void fRect(Rectangle r)
        {
            bufferGraphics.FillRectangle(new SolidBrush(fillColor), r);
        }

        public void rect(Rectangle r)
        {
            bufferGraphics.DrawRectangle(new Pen(fillColor), r);
        }

        public void Color3(Color c)
        {
            fillColor = c;
        }

        public Bitmap getBuffer()
        {
            return buffer;
        }
    }
}

I'm overriding the form's OnPaint event, which redraws the rectangle every frame. The issue was that the form was painting over the rectangle. How to use the OnPaint event in C#?

Community
  • 1
  • 1
Noah Heber
  • 82
  • 1
  • 12