Hi everyone,

I'm supposed to make a form with the basic shapes (circle, ellipse, pie, arc, etc...) drawn onto a panel. For each shape one will be hollow and one will be filled ontop of eachother. I can't seem to get the positions correct. All of the images are being drawn into eachother. I know it has to do with the x and y coordinates I have setup but I can't seem to figure out the correct numbers to put in to make them look nice. Is there a function or something that will align them all? Here's my code:

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            #region Shapes
            Graphics g = e.Graphics;
            //initialize starting point variables
            int x = 0;
            int y = 0;          


            Pen blkPen = new Pen(Brushes.Black, 2);  //width = 2
            g.SmoothingMode = SmoothingMode.AntiAlias; //draws smooth lines
            //draw circle and filled circle
            recCircle = new Rectangle(new Point(x,y),new Size(40,40));
            g.DrawEllipse(blkPen, recCircle);
            g.FillEllipse(Brushes.Black, x, y + 60, 40, 40);
            //draw ellipse and filled ellipse
            int w = 60;
            int h = 40;
            recEllipse = new Rectangle(new Point(x,y),new Size(w,h));
            g.DrawEllipse(blkPen, recEllipse);
            recEllipse = new Rectangle(new Point(x,y+60), new Size(w,h));
            g.FillEllipse(Brushes.Black, recEllipse);
            //draw polygon            
            Point pt1 = new Point(x, y);
            Point pt2 = new Point(x + 22, y + 12);
            Point pt3 = new Point(x + 22, y + 32);
            Point pt4 = new Point(x, y + 44);
            Point pt5 = new Point(x - 22, y + 32);
            Point pt6 = new Point(x - 22, y + 12);
            Point[] myPoints = { pt1, pt2, pt3, pt4, pt5, pt6 };
            g.DrawPolygon(blkPen, myPoints);
            //points changed to not draw over original polygon
            g.FillPolygon(Brushes.Black, myPoints);
            //draw pie shape and fill pie shape
            recPie = new Rectangle(new Point(x,y),new Size(80,80));
            //create start and sweep 
            int startAngle = 0;    //clockwise from x-axis
            int sweepAngle = -60;    //clockwise from startangle
            g.DrawPie(blkPen, recPie, startAngle, sweepAngle);
            g.FillPie(Brushes.Black, x, y + 60, 80, 80, startAngle, sweepAngle);
            //draw and fill rectangle with beveled edges
            blkPen.Width = 5;
            recRectangle = new Rectangle(x, y, 50, 40);
            g.DrawRectangle(blkPen, recRectangle);
            blkPen.LineJoin = LineJoin.Bevel;
            recRectangleBeveled = new Rectangle(x, y + 60, 50, 40);
            g.DrawRectangle(blkPen, recRectangleBeveled);
            //draw arc and filled pie
            startAngle = 45;
            sweepAngle = 180;
            g.DrawArc(blkPen, x, y, 40, 40, startAngle, sweepAngle);
            g.FillPie(Brushes.Black, x, y + 60, 40, 40, startAngle, sweepAngle);
            #endregion
        }

Here's what it looks like:
http://img3.imageshack.us/img3/8454/paintg.png

Recommended Answers

All 9 Replies

Cannot read your pgn file.
On line 19 you draw an ellipse on the same spot where your circle is drawn, because x and y are still equal to zero.
Update those values before every draw and I think you will work it out.
Succes!

To stay with line 19 : instead of
recEllipse = new Rectangle(new Point(x,y),new Size(w,h));
it should read
Rectangle recEllipse = new Rectangle(new Point(x,y),new Size(w,h));
All your lines with new Rectangle in it should be corrected this way. Wonder how you could compile this.
Instead of just adding a value to x and y, you should use assignment.

To stay with line 19 : instead of
recEllipse = new Rectangle(new Point(x,y),new Size(w,h));
it should read
Rectangle recEllipse = new Rectangle(new Point(x,y),new Size(w,h));
All your lines with new Rectangle in it should be corrected this way. Wonder how you could compile this.
Instead of just adding a value to x and y, you should use assignment.

Sorry I should have mentioned this before. I declared all of my variables before the Paint event above InitializeComponent like this:

public partial class frmFun : Form
    {
        Rectangle recCircle;
        Rectangle recEllipse;
        Rectangle recPie;
        Rectangle recRectangle;
        Rectangle recRectangleBeveled;
        Rectangle recArc;
        GraphicsPath gcPath;

        public frmFun()
        {
            gcPath = new GraphicsPath();
            InitializeComponent();
        }

Ok, I've made all of the objects successfully display next to eachother instead of ontop of eachother by changing the x-axis to 'x+=90' after every shape. The only thing that I can't get to work is the polygon; the filled shape is sitting ontop of the hollowed shape even though I've changed the x and y axis values... Here's the code snippet for it:

//draw polygon            
            Point pt1 = new Point(x, y);
            Point pt2 = new Point(x + 22, y + 12);
            Point pt3 = new Point(x + 22, y + 32);
            Point pt4 = new Point(x, y + 44);
            Point pt5 = new Point(x - 22, y + 32);
            Point pt6 = new Point(x - 22, y + 12);
            Point[] myPoints = { pt1, pt2, pt3, pt4, pt5, pt6 };
            g.DrawPolygon(blkPen, myPoints);
            //points changed to not draw over original polygon
            Point pt11 = new Point(x, y);
            Point pt22 = new Point(x + 22, y + 72);
            Point pt33 = new Point(x + 22, y + 92);
            Point pt44 = new Point(x, y + 104);
            Point pt55 = new Point(x - 22, y + 92);
            Point pt66 = new Point(x - 22, y + 72);
            Point[] myPoints2 = { pt1, pt2, pt3, pt4, pt5, pt6 };
            g.FillPolygon(Brushes.Black, myPoints2);

No problem, I thought it could be something like that. Just to be sure. Have you made any progress yet with the other suggestions? If not please ask.

No problem, I thought it could be something like that. Just to be sure. Have you made any progress yet with the other suggestions? If not please ask.

Yes I solved the placement for everything except for the polygon placement. I must have re-posted as you were posting this :) look at my previous posted.

Haargh..., in what follows you probably gonna shoot yourself in the head.;)
My head is already full of such holes.:P
Change line 11 in your last post in:
Point pt11 = new Point(x, y + 60);
Change line 17 (This the one that really does it:ooh: )
Point[] myPoints2 = { pt11, pt22, pt33, pt44, pt55, pt66 };

Did you see it? ( pt11 versus pt1 etc.)

commented: a shapely reply +1

Haargh..., in what follows you probably gonna shoot yourself in the head.;)
My head is already full of such holes.:P
Change line 11 in your last post in:
Point pt11 = new Point(x, y + 60);
Change line 17 (This the one that really does it:ooh: )
Point[] myPoints2 = { pt11, pt22, pt33, pt44, pt55, pt66 };

Did you see it? ( pt11 versus pt1 etc.)

Haha wow, what a dumb mistake... Thanks for the help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.