I want a face to be added to a queue everytime I click inside the form. It should then be drawn where I click every time I click. These faces are defined in a seperate class shown below:
class Face
{
private int x = 0;
private int y = 0;
private int lenght = 0;
private int height = 0;
private int diameter = 0;
public Face(int x, int y, int lenght, int height , int diameter)
{
X = x;
Y = y;
Lenght = lenght ;
Height = height ;
Diameter = diameter;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value ; }
}
public int Lenght
{
get { return lenght ; }
set { lenght = value; }
}
public int Height
{
get { return height; }
set { height = value; }
}
public int Diameter
{
get { return diameter; }
set { diameter = value; }
}
public void DrawLeftEye(Graphics g)
{
g.FillRectangle(new SolidBrush(Color.Black), X+5, Y+5, Lenght, Height);
}
public void DrawRightEye(Graphics g)
{
g.FillRectangle(new SolidBrush(Color.Black), X + 11, Y + 5, Lenght, Height);
}
public void DrawFace(Graphics g)
{
g.FillEllipse(new SolidBrush(Color.Pink), X, Y, Diameter, Diameter);
}
So far I've done this:
public partial class Form1 : Form
{
private Queue<Face> faces= new Queue<Face>();
public Form1()
{
InitializeComponent();
int x = 20;
int y = 70;
int lenght= 5;
int height= 5;
int diameter = 20;
face = new Face (x, y, lenght, height, diameter);
faces.Enqueue(face);
}
protected override void OnMouseClick(MouseEventArgs e)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
face.DrawFace(g);
face.DrawLeftEye(g);
face.DrawRightEye(g);
Invalidate();
}
}
If it isn't obvious I'm kind of lost on how to do this. I have no idea how to get the location of the mouse, yet alone how I should add the face to a queue and then draw it.
A little help is needed.
You can get the mouse position (relative to one of your elements) with
eventArgs.GetPosition(WhateverElementYouLike);
Since the Queue would be empty after every single OnRender
, you might want to replace it with a List.
private List<Face> faces= new List<Face>();
So in your case it would be something similar to this:
protected override void OnMouseClick(MouseEventArgs e)
{
Point position = e.GetPosition(this);
int x = (int)e.X;
int y = (int)e.Y;
faces.Add(new Face (x, y, 5, 5, 20));
InvalidateVisual();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
foreach(Face face in faces)
{
face.DrawFace(g);
face.DrawLeftEye(g);
face.DrawRightEye(g);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.