Solved

Graphic Question

Posted on 2006-11-29
7
263 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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