Solved

Graphic Question

Posted on 2006-11-29
7
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 18

Author Comment

by:vbturbo
ID: 18044397
Sorry

meant Y positive and .X negative zones
0
 
LVL 63

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 63

Expert Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

697 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