views:

59

answers:

2

Hi

I'm trying to make a basic soccer game in C#, and ive almost completed the field except for the various arcs and circles that are fairly important in the game, especially to set the bounds the computer's players cannot pass while their teammate/opponent is lining up for the kick. So, all the methods I've tried havent worked because apparently I'm using fields like types, but I'm copying the code exactly. But I don't think its very important to show the buggy code, for a start I deleted it and I'd like the circles to be there permanently, not from when debugging starts. So this is what I need: panels with round borders that stay round, and a way to put it in my code, which I'll post if necessary. Visual Studio C# Express 2010. All help appreciated, thanks

A: 

To draw a Circle onto a panel using Windows.Forms you can do something like this:

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

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

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawCircle(e);
        }

        private void DrawCircle(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // panelToDraw is your panel on the Form
            g = panelToDraw.CreateGraphics();    
            Pen pn = new Pen(Color.Blue, 1);
            Rectangle rect = new Rectangle(20, 20, 200, 200);
            g.DrawEllipse(pn, rect);
        }

    }
}

To shape the actual panel to be circular, i think is harder. Want to learn more on GDI+... take a look at this guide: GDI+ tutorial for beginners

BennySkogberg
Whats with reading the graphics from the form and then calling CreateGraphics on the panel? Why not just override OnPaint of the panel itself?
Jamiec
That is of course another way to do it. Both methods work well, and my solution is just a solution if you want to add more things to draw from the OnPaint-method. I like to devide code into small pieces making them easier to read (for me) :)
BennySkogberg
@Benny - I think @Jamiec's point was that you get the form Graphics object and then immediately throw it away when you call CreateGraphics (which you shouldn't do, btw. See http://bobpowell.net/creategraphics.htm)
Chris Dunaway
Thanx, Chris! Now I know better :)
BennySkogberg
In advance, is it possible to make a mouse event apply to crossing the border, not just entering the actual shape? If so that means all I need is to draw arcs and circles on their respective panels. Thanks
NeoHaxxor
A: 

One easy way to draw a circle on a panel is to inherit from Panel and override the OnPaint method. In this method you would call DrawEllipse on the Graphics object gotten from the event args. On point of interest is that the size is set to Width-1 and Height-1. This stops the right and bottom of the circle from dissapearing out of the Panel control.

One enhancement I have put in this code is to constrain the width & height in the OnResize method, this ensures your panel is always a circle, in oppose to an Ellipse (which can have different width and height). Simply drag this control onto a windows form and have a play in the designer.

public class CirclePanel : Panel
{
    public CirclePanel()
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawEllipse(Pens.Black, 0,0,this.Width-1,this.Height-1);
    }

    protected override void OnResize(EventArgs e)
    {
        this.Width = this.Height;
        base.OnResize(e);
    }
}
Jamiec
I've done that, but nothing has happened :(. I think it probably lies in the public CirclePanel having nothing inside it. I have 8 panels I need to draw arcs on, would I need to put their names in here? Sorry if this is annoyingThanks
NeoHaxxor