Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to keep muliple ellipse's on a grid.

Status
Not open for further replies.

tobermory20

Programmer
Dec 7, 2007
20
US
Hello,

I have a small scale 5x5 grid. When you click on an intersection like x=25 & y=25 I have set to draw an ellipse and fill it in red or black depending on positive or negative entries. That was easy enough.

But when I click on the next intersect like x=25 & y=60, the first filled in ellipse disappears, how can I make the first ellipse stay, while I add a second plot on my grid?
 
I don't have much experience in this, but my guess would be that you need to store the previous ellipse (and all others as well) and when a new one is redrawn, redraw the stored ones.


----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
After hacking around a bit with this, I am thinking the best way is a 2-D array to hold all the points as the mouse clicks the intersections. Does anyone know if there is a better way to store data points other than an array?

T.
 
I would create an ellipse class and keep a list of them. Then when I wanted them to draw, i'd pass in the graphics object and let them draw themselves. Makes your running code look elegant and easy that way. The example below was not tested or compiled - I just wrote it now inside this little box.

Example:

Code:
public class Ellipse
{
   public int X;
   public int Y;
   public int Width;
   public int Height;

   public Ellipse(int x, int y, int width, int height)
   {
     X = x;
     Y = y;
     Width = width;
     Height = height;
   }

   public void Draw(Graphics g)
   {
       g.DrawEllipse(X, Y, Width, Height); //Or however you draw an ellipse again...
   }
}


public class Form1
{
    private List<Ellipse> lstEllipses = new List<Ellipse>();

    public Form1()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(form1_paint);
        this.MouseDown += new MouseEventHandler(form1_mousedown);
    }

    private void form1_mousedown(object sender, MouseEventArgs e)
    {
        //This adds a new ellipse at the clicked point. The ellipse will be 100px wide by 50px high.
        lstEllipses.Add(new Ellipse(e.X, e.Y, 100, 50));\

        //Ask the form to redraw itself - which includes the new ellipse
        this.Invalidate();
    }

    prviate void form1_paint(object sender, PaintEventArgs e)
    {
        foreach (Ellipse el in lstEllipses)
        {
            el.Draw(e.Graphics);
        }
    }
}

 
Thanks for the input JurkMonkey, that sounds like a better idea then trying to hassle with arrays. And it would save a few lines of code, with the way this is starting to grow...

Thanks,
T.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top