Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.20/5 (4 votes)
See more:
Hello
This is my project
I have a Problem for Rotation & Move & Scale the Star.
Please Help Me.
I develep this project to:
public Form1()
{
    InitializeComponent();
    this.Paint += new PaintEventHandler(Form1_Paint);
    //System.Drawing.Pen myPen;
    //myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
    //System.Drawing.Graphics formGraphics = this.CreateGraphics();
    //formGraphics.DrawLine(myPen, 0, 0, 200, 200);
}

static void drawFlag(Graphics g)
{
    Point[] star ={
    new Point(0,-60),  new Point(11,-16),
    new Point(58,-16), new Point(18,6),
    new Point(40,41),  new Point(0,18),
    new Point(-40,41), new Point(-18,6),
    new Point(-48,-16),new Point(-11,-16)};
    Point[] starNormal ={
    new Point(0,-50),  new Point(11,-16),
    new Point(48,-16), new Point(18,6),
    new Point(30,41),  new Point(0,18),
    new Point(-30,41), new Point(-18,6),
    new Point(-48,-16),new Point(-11,-16)};
    ////g.FillRectangle(Brushes.Red, 0, 0, 200, 150);
    //g.TranslateTransform(100, 100);
    //g.FillPolygon(Brushes.Blue, star);
    g.TranslateTransform(150, 150);
    g.FillPolygon(Brushes.Blue, star);
    g.FillRectangle(Brushes.Red, 0, 0, 5, 5);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    System.Drawing.Graphics graphicsObj;

    graphicsObj = this.CreateGraphics();

    drawFlag(graphicsObj);
    //Pen myPen = new Pen(System.Drawing.Color.Green, 5);
    //Rectangle myRectangle = new Rectangle(20, 20, 250, 200);
    //graphicsObj.DrawEllipse(myPen, myRectangle);
}

private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (e.Location.X >= 100 && e.Location.X <= 104)
        MessageBox.Show(e.Location.X.ToString());
}


this image url :
http://bac.freeiz.com/photos/5aa4bd09c07d.png[^]
Posted
Updated 28-Jun-12 15:35pm
v5
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 15:20pm    
Help with what? This is not a question.
--SA
Sergey Alexandrovich Kryukov 28-Jun-12 15:26pm    
Yes, bad code, needs some help. I pointed out some problems in my answer, please see.
Another problem is a lot of immediate constants hard-coded. All your 11, -16, 10, -18, 104... This is not supportable at all. At least use explicit ones.
--SA
AliRadman 28-Jun-12 15:34pm    
I want Non-regular Star
Sergey Alexandrovich Kryukov 28-Jun-12 16:53pm    
OK, if you want it, make is. How an irregular start can be a problem if you have the line and the polygon? If you face a problem, ask about it.
--SA
AliRadman 28-Jun-12 15:47pm    
this image url :
http://bac.freeiz.com/photos/5aa4bd09c07d.png

1 solution

First big mistake I can see is creation of the instance of Graphics in the event handler of the event Paint. You should use the instance provided in the event arguments. Well, the way of setting up event handles is bad; and the naming conventions are not observed. Also, it's much better to override OnPaint instead of adding the handler. And, by the way, you will need to use the custom control by sub-classing System.Windows.Forms.Control anyway. First reason for that is using protected SetStyle to add optimized double buffering, to avoid flicker.

Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

[EDIT #1]

See also my second comment to the question. Do not hard-code immediate constants. Work in a supportable way.

[EDIT #2]

AliRadman asked:
I want Rotation & Move & Scale Code
So, what's the problem? You can use the property System.Drawing.Graphics.Transform or the methods having the word "Transform" in their names. You would need RotateTransform, ScaleTransform, TranslateTransform and maybe ResetTransform. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

A note: Please, don't post your questions or comments as answers. Such posts will be deleted; no one gets e-mail notifications.

—SA
 
Share this answer
 
v5
Comments
Tim Corey 28-Jun-12 19:44pm    
Nice answer. +5
Sergey Alexandrovich Kryukov 28-Jun-12 21:31pm    
Thank you, Tim.
--SA
Manoj Kumar Choubey 29-Jun-12 0:49am    
nice
Sergey Alexandrovich Kryukov 29-Jun-12 1:08am    
Thank you, Manoj.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900