detecting mouseclick in arraylist
-
Hi, i need some help in figuring this out. i have a class as shown below:
public interface IDrawable { void Draw( Graphics g ); int CountNo(ArrayList al); } public class Ellipse : IDrawable { public Color c = Color.Black; public Point l = new Point(0, 0); public Size s = new Size(0, 0); public int stroke = 0; public Ellipse( Color c, Point l, Size s, int stroke ) { this.c = c; this.l = l; this.s = s; this.stroke = stroke; } public void Draw( Graphics g ) { g.DrawEllipse( new Pen( c, stroke ), l.X ,l.Y, s.Width,s.Height ); }
in the form:private ArrayList alDrawingObjects = new ArrayList(); ....... alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); ....... private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics; foreach ( IDrawable d in alDrawingObjects ) { d.Draw( objGraphics ); } // Free up resources. objGraphics.Dispose(); }
i need to be able to select with a mouseclick on any of the shape drawn. I thought of using Contain method but i don't know how to implement it. i know that i need to search through the array list and test if the mouseclick point or position is in the drawn shape. may i know how to implement this? Thank u in advance. -
Hi, i need some help in figuring this out. i have a class as shown below:
public interface IDrawable { void Draw( Graphics g ); int CountNo(ArrayList al); } public class Ellipse : IDrawable { public Color c = Color.Black; public Point l = new Point(0, 0); public Size s = new Size(0, 0); public int stroke = 0; public Ellipse( Color c, Point l, Size s, int stroke ) { this.c = c; this.l = l; this.s = s; this.stroke = stroke; } public void Draw( Graphics g ) { g.DrawEllipse( new Pen( c, stroke ), l.X ,l.Y, s.Width,s.Height ); }
in the form:private ArrayList alDrawingObjects = new ArrayList(); ....... alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); ....... private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics; foreach ( IDrawable d in alDrawingObjects ) { d.Draw( objGraphics ); } // Free up resources. objGraphics.Dispose(); }
i need to be able to select with a mouseclick on any of the shape drawn. I thought of using Contain method but i don't know how to implement it. i know that i need to search through the array list and test if the mouseclick point or position is in the drawn shape. may i know how to implement this? Thank u in advance.cyn8 wrote:
Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics;
Bizarre. This is totally superfluous, AFAIK. What you need to do is, in your mouse down event, iterate over the objects again, and check if the mouse is inside any of them. Obviously, for an ellipse, you need to read up on the maths to find out if a point is inside an ellipse. One cheats way of doing it, is to create a new bitmap, draw the objects onto it in white, and check if your mousedown point is white or black as you draw each one. This is very inefficient, really should only be used where you can't work it out mathematically ( for example if you had a completely irregular shape )
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
cyn8 wrote:
Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics;
Bizarre. This is totally superfluous, AFAIK. What you need to do is, in your mouse down event, iterate over the objects again, and check if the mouse is inside any of them. Obviously, for an ellipse, you need to read up on the maths to find out if a point is inside an ellipse. One cheats way of doing it, is to create a new bitmap, draw the objects onto it in white, and check if your mousedown point is white or black as you draw each one. This is very inefficient, really should only be used where you can't work it out mathematically ( for example if you had a completely irregular shape )
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
What you need to do is, in your mouse down event, iterate over the objects again,... can you explain further? The problem is that for all the shape drawn before, i can't seem to save the area which they contained. if i mouseclick on the form, how can i check that the mouse click in within the area of the previously drawn shape. Please help, i've been cracking my head for hours. Thanks