Solved

Graphic Question

Posted on 2006-11-29
7
261 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 32
Events in static methods 3 50
Vb.net dynamic formulas in runtime 11 63
VB.Net How to Exit Sub - Exit Form??? 5 51
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now