Solved

Graphic Question

Posted on 2006-11-29
7
262 Views
Last Modified: 2010-04-16
HI

why cant i get this to paint when negative side on the coordinates ?

public partial class offcenter : Form
    {
        int width, height;

        Point center, current;
        Pen pen;
        Font font;
        Brush brush;

        Timer timer1 = new Timer();

        public offcenter()
        {
           
            timer1.Interval = 50; // in milliseconds
           
            timer1.Tick += new EventHandler(Timer_Tick);

            this.BackColor = Color.Yellow;
            this.Text = "Off center";
            //this.Icon = new Icon(@"..\..\Creative.ico");
            this.Name="OffCenter";
           his.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            pen = new Pen(Color.Black,0);
            font = new Font("Times New Roman", 10);
            brush = new SolidBrush(Color.Black);
            OnResize(new System.EventArgs());
            Invalidate();
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            width = this.ClientRectangle.Width;
            height = this.ClientRectangle.Height;
            center = new Point(ClientRectangle.Left + width / 2,
                                        ClientRectangle.Top + height / 2);
            current = center;
            Invalidate();
        }

        protected override void  OnMouseDown(MouseEventArgs e)
        {
            // cusor lokation
            current.X = e.X;
            current.Y = e.Y;

            timer1.Enabled = true;
            Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            // cusor lokation
            current.X = e.X;
            current.Y = e.Y;


            timer1.Enabled = false ;
            Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            float x;
            float y;

            base.OnPaint(e);
            Graphics page = e.Graphics;

            page.DrawRectangle(pen, center.X, center.Y,( current.X-center.X), (current.Y-center.Y));
            x = (current.X - center.X);
             y = (current.Y - center.Y);


            if (center.X != current.X || center.Y != current.Y)
                page.DrawLine(pen, current, center);

            double dX2 = (center.X - current.X) * (center.X - current.X);
            double dY2 = (center.Y - current.Y) * (center.Y - current.Y);
            double distance = Math.Sqrt(dX2 + dY2);
            page.DrawString(string.Format("Distance : {0:0.#}", distance), font, brush, 5, 5);

        }
        private void Timer_Tick(object sender, System.EventArgs e)
        {
            current = new Point(Cursor.Position.X, Cursor.Position.Y);
            Invalidate();
        }
    }

thanks in advance
vbturbo
0
Comment
Question by:vbturbo
  • 3
  • 3
7 Comments
 
LVL 14

Assisted Solution

by:existenz2
existenz2 earned 100 total points
ID: 18041023
Images can't have negative coordinates if you mean that.

Images can only have sizes from X > 0 and Y > 0.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18041101
Hi vbturbo;

The reason why is that when you click the mouse to the left of center or above center the following statement produces a negative length of the object to be drawn, parameters 4 and 5.

            page.DrawRectangle(pen, center.X, center.Y, (current.X - center.X), (current.Y - center.Y));

To always have a positive length you can do this.

            page.DrawRectangle(pen, center.X, center.Y,
                Math.Abs((current.X - center.X)), Math.Abs((current.Y - center.Y)));

Fernando
0
 
LVL 18

Author Comment

by:vbturbo
ID: 18044386
Hi

Tryed out your solution Fernado.
and it work'ed out partly off my intension.

though it draws only in the right lower corner off the X and Y axis ...



             |
not here | not here
________________                            
             |
not here |  only  here


I want to paint the rectangle in any direction from the center, so when fourth and fifth parameter gets negative it should draw also in the Y negative and .X negative zones and not just expand the rectangle in the same zone,

Thanks

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Author Comment

by:vbturbo
ID: 18044397
Sorry

meant Y positive and .X negative zones
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 18045395
Hi vbturbo;

The signature of the DrawRectangle is as follows:

From Microsoft Documentation:

Graphics.DrawRectangle( ByVal pen As Pen, ByVal x As Integer, ByVal y As Integer, _
    ByVal width As Integer, ByVal height As Integer )

pen - Pen object that determines the color, width, and style of the rectangle.
x - x-coordinate of the upper-left corner of the rectangle to draw.
y - y-coordinate of the upper-left corner of the rectangle to draw.
width - Width of the rectangle to draw.
height - Height of the rectangle to draw

Because the 2nd and 3rd parameters define the upper-left corner of the rectangle the rectangle will always be drawn to the right and down from that point. To be able to place a rectangle on the screen with one of its corners in the center of the window you will also need to adjust the 2nd and 3rd parameter so that it is located somewhere else and one of the other corners ends up in the center of the window.

Fernando
0
 
LVL 18

Author Comment

by:vbturbo
ID: 18045464
Thanks Fernado

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18045479
Not a problem, glad I was able to help. ;=)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question