Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Fillrectangle method

SchwarzeneggerSchwarzenegger Posts: 10Member
[b][red]This message was edited by Schwarzenegger at 2006-2-11 6:6:23[/red][/b][hr]
Hello dear programmers.
I'm having a problem with this program im writing. (Im trying to create a paint brush like windows has), anyway, I've managed to form a filled rectangle but it can only be drwan from the upper left corner of the "mouse-down" method and to the right or down. If im trying to draw in the other direction, nothing happens - I just get nothing. Now, I've noticed that the rectangle is limited in the X-coordinate to 0 and the same in Y. That means that if the end-point is smaller than the start-point it doesnt respond. I've also come to realize that in the FillRectangle method itself when im opening a brackets () im getting that Im suppose to write the "X-coordinate of the UPPER-LEFT rectangle to fill" - so what does that mean? that I can only draw rectanglr from the upper left corner and down?!! probably not but I just dont know how to solve this problem. (I've pasted some line from my code) so if u know the answer or have a lead please answer me..

Sincirly yours,

Rotem

private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
pictureBox1.Invalidate();

if (flag == true)
{
Graphics G = pictureBox1.CreateGraphics();

if (Rectangle.Checked)
{
if (e.Button == MouseButtons.Left)
{
Point mp1 = new Point(0,0);
mp1.X = e.X;
mp1.Y = e.Y;
f1.PointEnd = mp1;
}
}


Comments

  • VanilleBertVanilleBert Posts: 29Member
    You want to make something like this? ;) http://www.eecs.wsu.edu/paint.net/

    First you have to points. Point1, the point where the mouse was pressed, and Point2, the point where the mouse is now. Now you have to calculate the upperleft point of your rect and the size.

    [code]
    //Point1 is set at Mouse_Down
    //Point2 is set at Mouse_Move

    // upperleft point, Math.Min will choose the minimal
    Left = Math.Min(Point1.X, Point2.X); // X of the point
    Top = Math.Min(Point1.Y, Point2.Y); // Y of the point

    // size, Math.Abs keeps it positive
    Width = Math.Abs(Point1.X - Point2.X];
    Height = Math.Abs(Point1.Y - Point2.Y];

    FillRectangle(Brush, Left, Top, Width, Height); // fill it
    [/code]
  • tsagldtsagld Posts: 621Member
    : [b][red]This message was edited by Schwarzenegger at 2006-2-11 6:6:23[/red][/b][hr]
    : Hello dear programmers.
    : I'm having a problem with this program im writing. (Im trying to create a paint brush like windows has), anyway, I've managed to form a filled rectangle but it can only be drwan from the upper left corner of the "mouse-down" method and to the right or down. If im trying to draw in the other direction, nothing happens - I just get nothing. Now, I've noticed that the rectangle is limited in the X-coordinate to 0 and the same in Y. That means that if the end-point is smaller than the start-point it doesnt respond. I've also come to realize that in the FillRectangle method itself when im opening a brackets () im getting that Im suppose to write the "X-coordinate of the UPPER-LEFT rectangle to fill" - so what does that mean? that I can only draw rectanglr from the upper left corner and down?!! probably not but I just dont know how to solve this problem. (I've pasted some line from my code) so if u know the answer or have a lead please answer me..
    :
    : Sincirly yours,
    :
    : Rotem
    :
    : private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    : {
    : pictureBox1.Invalidate();
    :
    : if (flag == true)
    : {
    : Graphics G = pictureBox1.CreateGraphics();
    :
    : if (Rectangle.Checked)
    : {
    : if (e.Button == MouseButtons.Left)
    : {
    : Point mp1 = new Point(0,0);
    : mp1.X = e.X;
    : mp1.Y = e.Y;
    : f1.PointEnd = mp1;
    : }
    : }
    :
    :
    :

    Don't forget to do a G.Dispose() when you don't longer need the Graphics object. If you don't, you will be sucking up memory.

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

Sign In or Register to comment.