Solved

Graphic Question

Posted on 2006-11-29
7
260 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

13 Experts available now in Live!

Get 1:1 Help Now