0
class Cycle : Form1
{
    public void Draw(Graphics G,Brush b, float x, float y, float w, float h)
    {
        G.FillEllipse( b, x, y, w, h);
    }
}

This is my Cycle class that inherits from Form1.

    List<Point> star = new List<Point>();
    List<Point> endd = new List<Point>();
    private void Panel1_MouseDown(object sender, MouseEventArgs e)
    {
        moving = true;
        Start = e.Location;
        star.Add(e.Location);
    }

    private void Panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (moving)
        {
            End = e.Location;
            Panel1.Invalidate();
        }
    }

    private void Panel1_MouseUp(object sender, MouseEventArgs e)
    {
        endd.Add(e.Location);
        moving = false;
    }

These are my mouse event args that keeps locations.

    private void Panel1_Paint(object sender, PaintEventArgs e)
    {

            Cycle c = new Cycle();
            if (End.Y > Start.Y && End.X > Start.X)
            {
                foreach (Point pt in star)
                {
                    foreach(Point p in endd)
                        c.Draw(e.Graphics, brush, pt.X - (p.X - pt.X) / 2 - (p.Y - pt.Y) / 2, pt.Y - (p.Y - pt.Y) / 2 - (p.X - pt.X) / 2, ((p.X - pt.X) + (p.Y - pt.Y)), ((p.X - pt.X) + (p.Y - pt.Y)));
                }
            }
    }

And this code draws ellipses in panel. However the first ellipse has been drawn perfect but other ellipses got so big that covered all the panel.

I want to keep locations because after drawing 3-4 ellipses I will try to save the points into .txt. After that I will load it to draw again. But there is this problem in drawing :/

PreS
  • 43
  • 1
  • 5
  • Your calculation of e.g. height or top really should not involve any X values, right? Same for width and left.. – TaW May 03 '18 at 09:56
  • No, there is no problem with that. Because when I write this code below in Panel1Paint, it works and I can draw an ellipse. Cycle c = new Cycle(); if (End.Y > Start.Y && End.X > Start.X) { c.Draw(e.Graphics, brush, Start.X - (End.X - Start.X) / 2 - (End.Y - Start.Y) / 2, Start.Y - (End.Y - Start.Y) / 2 - (End.X - Start.X) / 2, ((End.X - Start.X) + (End.Y - Start.Y)), ((End.X - Start.X) + (End.Y - Start.Y))); } – PreS May 03 '18 at 10:30
  • But that way I can only draw one shape – PreS May 03 '18 at 10:31
  • No. You loop over your lists and calculate x and width from the xes only. Likewise for the y and height from the ys only. Combining them simply makes no sense. Btw: I always store the shapes' bounding rectangle in a ListyRectangle>. But that is another matter. – TaW May 03 '18 at 11:25
  • Also: I see you keep Start and End and Invalidate in the Move but the currently drawn shape is not drawn anywher, right? – TaW May 03 '18 at 11:29
  • Cycle c = new Cycle(); ------------------------------------------------------------if (End.Y > Start.Y && End.X > Start.X) ----------------------------------------{ ----------------------------------------------------------c.Draw(e.Graphics,brush, Start.X - (End.X - Start.X) / 2 - (End.Y - Start.Y) / 2, Start.Y - (End.Y - Start.Y) / 2 - (End.X - Start.X) / 2, ((End.X - Start.X) + (End.Y - Start.Y)), ((End.X - Start.X) + (End.Y - Start.Y))); } --------------------------------------------------------------------------Actually with this code in panel1paint, I can draw the ellipse perfectly. – PreS May 03 '18 at 11:38
  • those x and y problem you're talking about, I want ellipse to be equilateral. That's why I do it that way. And it works. – PreS May 03 '18 at 11:39
  • But I need help about using list – PreS May 03 '18 at 11:40
  • [Here is an example](https://stackoverflow.com/questions/33767406/drawing-rectangle-in-a-panel-using-mouse/33768418#33768418) and [here](https://stackoverflow.com/questions/41659729/how-to-draw-shapes-and-color-them-with-a-button/41660406?s=4|26.1882#41660406) a more complex one. – TaW May 03 '18 at 13:52
  • I checked the example. You said him to use List. What do you mean by yourDrawActionClass ? I didn't get there. – PreS May 03 '18 at 18:55
  • Many folks want to go beyond drawing one rype of shape with one type of pen or brush. In such a class of your own you can encapsulate all sorts of things to draw all sorts of things. [A few posts where I discuss it](https://stackoverflow.com/search?q=user%3A3152130+DrawAction) – TaW May 03 '18 at 19:00

0 Answers0