some problems with drawing

T

Tony Johansson

Hello!

This program draw an x-axis and a y-axes and also add values to the interval
on the x and y axes.
The program also move the Page coordinates to the bottom left corner which
will be the origo.
All this works good.

I also want a cross to be drawn in the following way one horizontal dashed
line all over the panel called pnlDiagram and one vertical dashed line from
bottom to top of the panel control. So as a summary it should be a cross
from left to right and from top to bottom of the Panel control and the cross
should be in sync with the mouse pointer. If I move the mouse pointer the
cross should also be moved. For example if a position the mouse pointer at
the page origo(0,0) the cross should also be there.

The vertical dashed line works perfect and is in sync with the mouse
pointer.
The horizontal dashed line is not in sync with the mouse pointer and I can't
figure out where I should change to make it work. ?

When I position the mouse pointer at the upper horizontal line of the Panel
my dashed horizontal line is located at the x-axis and when I move the mouse
poiner down the dashed horizontal line is going up.

It's this drawing of the horizontal dashed line that is wrong because it's
not in sync with the mouse pointer
e.Graphics.DrawLine(pen,
new PointF(pnlDiagram.Width, -originalMousePosition.Y),
new PointF(0, -originalMousePosition.Y));


Below is all the code that has to do with my problem.

public partial class MainForm : Form
{
private PointF currPos;
private PointF newOriginOffset;
private PointF originalMousePosition;

private DiagramData data = new DiagramData();
private DrawDiagram drawDiagram = new DrawDiagram();

public MainForm() //C-tor
{
InitializeComponent();
newOriginOffset = new Point(pnlDiagram.Width / 10,
pnlDiagram.Height / 10);
InitializeNewRun();
}

private void InitializeNewRun()
{
grpStart.Enabled = false;
groupSettings.Enabled = true;
pnlDiagram.Invalidate();
}

private void pnlDiagram_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(pnlDiagram.Width * 1 / 10,
pnlDiagram.Height * 9 / 10);
drawDiagram.Draw(g, data.Points, pnlDiagram.Height,
pnlDiagram.Width,
int.Parse(txtXIntervals.Text),
int.Parse(txtXIntervalValue.Text),
int.Parse(txtYIntervals.Text),
int.Parse(txtYIntervalValue.Text));

Pen pen = new Pen(Brushes.Black);
pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(pen,
new PointF(currPos.X, -pnlDiagram.Height),
new PointF(currPos.X, 0));

e.Graphics.DrawLine(pen, new
PointF(pnlDiagram.Width, -originalMousePosition.Y),
new
PointF(0, -originalMousePosition.Y));
}

private void pnlDiagram_MouseMove(object sender, MouseEventArgs e)
{
originalMousePosition = new Point(e.X, e.Y);

currPos.X = e.X - newOriginOffset.X;
currPos.Y = e.Y - newOriginOffset.Y - e.Y;
pnlDiagram.Invalidate();
}
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
[...]
When I position the mouse pointer at the upper horizontal line of the
Panel
my dashed horizontal line is located at the x-axis and when I move the
mouse
poiner down the dashed horizontal line is going up.

It's this drawing of the horizontal dashed line that is wrong because
it's
not in sync with the mouse pointer
e.Graphics.DrawLine(pen,
new PointF(pnlDiagram.Width, -originalMousePosition.Y),
new PointF(0, -originalMousePosition.Y));

Why are you not using "currPos" instead of "originalMousePosition"? That's
what you're using for the vertical line. Any time your code is
inconsistent, that should be a red flag to you.

I'm not going to spend time picking apart the rest of the code. It would
take too long. But here are some of the highlights of other issues:

. Parsing the input during drawing. Computers are fast, but why make
them work harder than they need to? You should maintain pre-parsed values
used just for drawing, updated whenever the original data changes.

. Speaking of pointless work, there's this calculation: "e.Y -
newOriginOffset.Y - e.Y". So, you start with "e.Y". Then you subtract
something from it. Then you subtract "e.Y" from that. Basic junior-high
algebra tells us that if you start with "e.Y" and then later subtract
"e.Y", you might as well not have "e.Y" in the calculation at all.

Now, whether "-newOriginOffset.Y" is in fact a useful number to set the
"currPos" to, I don't know. I seriously doubt it, since it doesn't take
into account the mouse position at all. But there's definitely something
wrong with your formula (and I will leave it to you to figure out what
that is).

Once you get that calculation correct, then you can use "currPos" for your
horizontal line and everything will work like you want.

. Finally, WHY ARE YOU NOT DISPOSING YOUR PEN? Seriously. I know this
isn't the first time you've heard that you have to dispose disposable
objects, nor that a Pen is a disposable object. Heck, just a week ago you
_specifically_ asked about disposing a Pen.

Please dispose your disposable objects. It's the right thing to do.

Pete

It works now but there are still two minor problem.
Yes I will dispose the object you are talking about as soon as it works.
My first one is that I don't want the arrow to be visible as the cursor I
just want to have the ellips that I draw.
So how do I make the arrow disapper as the cursor
My second is how do I make my ellips transparent ?

public partial class MainForm : Form
{
private PointF currPos;
private PointF newOriginOffset;

private DiagramData data = new DiagramData();
private DrawDiagram drawDiagram = new DrawDiagram();
private bool OK = true;

public MainForm() //C-tor
{
InitializeComponent();
newOriginOffset = new PointF((float)pnlDiagram.Width / 10,
(float)pnlDiagram.Height / 10);
InitializeNewRun();
}

private void InitializeNewRun()
{
UpdateTextBoxValues();
grpStart.Enabled = false;
groupSettings.Enabled = true;
pnlDiagram.Invalidate();
}

private void pnlDiagram_Paint(object sender, PaintEventArgs e)
{
if (OK)
{
Graphics g = e.Graphics;
g.TranslateTransform(pnlDiagram.Width * 1 / 10,
pnlDiagram.Height * 9 / 10);
drawDiagram.Draw(g, data.Points, pnlDiagram.Height,
pnlDiagram.Width,
int.Parse(txtXIntervals.Text),
int.Parse(txtXIntervalValue.Text),
int.Parse(txtYIntervals.Text),
int.Parse(txtYIntervalValue.Text));

Pen pen = new Pen(Brushes.Black);
Font font = new Font("Arial", 9);

pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(pen, new
PointF(currPos.X, -pnlDiagram.Height), new PointF(currPos.X, 0));
e.Graphics.DrawLine(pen, new
PointF(pnlDiagram.Width, -currPos.Y), new PointF(0, -currPos.Y));

Point currPoint = new Point((int)currPos.X -
20, -(int)currPos.Y - 20);
e.Graphics.FillEllipse(Brushes.Yellow, new Rectangle(currPoint,
new Size(40, 40)));

string x = string.Format("{0:F2}", currPos.X /
drawDiagram.MyScale.ScaleX);
string y = string.Format("{0:F2}", currPos.Y /
drawDiagram.MyScale.ScaleY);

string coordinate = "(" + x + " / " + y + ")";
e.Graphics.DrawString(coordinate, font, Brushes.Black, new
PointF(currPos.X, -currPos.Y));
}
}

private void pnlDiagram_MouseMove(object sender, MouseEventArgs e)
{
currPos.X = e.X - newOriginOffset.X;
currPos.Y = newOriginOffset.Y - e.Y + 302;
pnlDiagram.Invalidate();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top