Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Serializable]
- public class Circle
- {
- public int XCoord { get; set; }
- public int YCoord { get; set; }
- public int Radius { get; set; }
- public bool selected { get; set; }
- public bool secondClicked { get; set; }
- public int Velocity { get; set; }
- public Color color { get; set; }
- public Circle(int xCoord, int yCoord,Color color)
- {
- XCoord = xCoord;
- YCoord = yCoord;
- this.color = color;
- Velocity = 10;
- }
- public void setRadius(int Radius)
- {
- this.Radius = Radius;
- }
- public void select()
- {
- if (selected)
- selected = false;
- else
- selected = true;
- }
- public void draw(Graphics g)
- {
- Rectangle rectangle = new Rectangle(XCoord - Radius, YCoord - Radius, Radius * 2, Radius * 2);
- if (secondClicked)
- {
- Brush brush = new SolidBrush(color);
- g.FillEllipse(brush, rectangle);
- if (selected)
- {
- Pen pen = new Pen(Color.Red,4);
- g.DrawEllipse(pen, rectangle);
- pen.Dispose();
- }
- brush.Dispose();
- }
- else
- {
- Pen pen = new Pen(Color.Black,4);
- pen.DashStyle=DashStyle.Dot;
- pen.DashOffset = 2f;
- g.DrawEllipse(pen, rectangle);
- pen.Dispose();
- }
- }
- }
- }
- [Serializable]
- public class CircleDoc
- {
- public List<Circle> circles;
- public Color currentColor;
- public int Velocity { get; set; }
- public CircleDoc(Color currentColor)
- {
- Velocity = 5;
- circles = new List<Circle>();
- this.currentColor = currentColor;
- }
- public void setColor(Color color)
- {
- this.currentColor = color;
- }
- public void draw(Graphics g)
- {
- foreach(Circle circle in circles)
- {
- circle.draw(g);
- }
- }
- public void move(Keys keyCode)
- {
- for(int i=circles.Count-1;i>=0;i--)
- {
- if (circles.Count - 1 >= i)
- {
- Circle circle = circles.ElementAt(i);
- if (circle.selected && circle != null)
- {
- {
- switch (keyCode)
- {
- case Keys.Up:
- circle.YCoord -= Velocity;
- checkIntersect(circle);
- break;
- case Keys.Right:
- circle.XCoord += Velocity;
- checkIntersect(circle);
- break;
- case Keys.Left:
- circle.XCoord -= Velocity;
- checkIntersect(circle);
- break;
- case Keys.Down:
- circle.YCoord += Velocity;
- checkIntersect(circle);
- break;
- }
- }
- }
- }
- }
- }
- private void checkIntersect(Circle circle)
- {
- for(int i = circles.Count - 1; i >= 0; i--)
- {
- if (circles.ElementAt(i) != circle&&circles.ElementAt(i).color==circle.color)
- {
- double distance = Math.Sqrt(Math.Pow(circles.ElementAt(i).XCoord - circle.XCoord, 2) + Math.Pow(circles.ElementAt(i).YCoord - circle.YCoord, 2));
- if (distance <= circles.ElementAt(i).Radius + circle.Radius)
- {
- circles.RemoveAt(i);
- circles.Remove(circle);
- return;
- }
- }
- }
- }
- public void addCircle(Circle circle)
- {
- circles.Add(circle);
- }
- public void removeCircle(Circle circle)
- {
- circles.Remove(circle);
- }
- public int circleNumber()
- {
- int count = 0;
- foreach(Circle circle in circles)
- {
- if (circle.secondClicked)
- count++;
- }
- return count;
- }
- public void selectCircle(Point clickLocation)
- {
- foreach(Circle circle in circles)
- {
- double distance = Math.Sqrt(Math.Pow(clickLocation.X - circle.XCoord, 2) + Math.Pow(clickLocation.Y - circle.YCoord, 2));
- if(distance<=circle.Radius)
- {
- circle.select();
- return;
- }
- }
- }
- }
- }
- public partial class Form1 : Form
- {
- class DoubleBufferedPanel : Panel
- {
- public DoubleBufferedPanel() : base()
- {
- DoubleBuffered = true;
- }
- }
- String fileName;
- bool toSave;
- CircleDoc circles;
- Circle currentCircle;
- bool onFirstClick;
- bool onSecondClick;
- public Form1()
- {
- InitializeComponent();
- newFile();
- }
- public void openFile()
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Title = "Opening circles...";
- openFileDialog.Filter = "Circle file(*.cir)|*.cir";
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- fileName = openFileDialog.FileName;
- IFormatter formatter = new BinaryFormatter();
- FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
- circles=(CircleDoc)formatter.Deserialize(fileStream);
- fileStream.Close();
- }
- }
- public void saveFile()
- {
- if (fileName == null)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "Saving circles...";
- saveFileDialog.Filter = "Circle file(*.cir)|*.cir";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- fileName = saveFileDialog.FileName;
- }
- }
- if (fileName != null)
- {
- IFormatter formatter = new BinaryFormatter();
- FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
- formatter.Serialize(fileStream, circles);
- fileStream.Close();
- }
- }
- public void newFile()
- {
- circles = new CircleDoc(Color.Green);
- fileName = null;
- toSave = true;
- currentCircle = null;
- onFirstClick = true;
- }
- public void panel1_MouseClick(object sender, MouseEventArgs e)
- {
- Point clickLocation = e.Location;
- if (e.Button == MouseButtons.Left)
- {
- if (onFirstClick)
- {
- Circle newCircle = new Circle(clickLocation.X, clickLocation.Y, circles.currentColor);
- currentCircle = newCircle;
- circles.addCircle(currentCircle);
- onFirstClick = false;
- onSecondClick = true;
- }
- else if (onSecondClick)
- {
- if (currentCircle != null)
- {
- currentCircle.secondClicked = true;
- Invalidate(true);
- onFirstClick = true;
- onSecondClick = false;
- }
- }
- }
- else if (e.Button == MouseButtons.Right)
- {
- circles.selectCircle(clickLocation);
- panel1.Invalidate();
- }
- }
- private void colorToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ColorDialog colorDialog = new ColorDialog();
- if (colorDialog.ShowDialog() == DialogResult.OK)
- {
- circles.setColor(colorDialog.Color);
- }
- }
- private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
- {
- toolStripStatusLabel1.Text = "Circles: " + circles.circleNumber();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- this.Text = "Color circles";
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- Graphics graphics = e.Graphics;
- circles.draw(graphics);
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Escape)
- {
- if (onSecondClick)
- {
- circles.removeCircle(currentCircle);
- currentCircle = null;
- onSecondClick = false;
- onFirstClick = true;
- Invalidate(true);
- }
- }
- else if (e.KeyCode == Keys.Up||e.KeyCode==Keys.Right||e.KeyCode==Keys.Left||e.KeyCode==Keys.Down)
- {
- circles.move(e.KeyCode);
- Invalidate(true);
- }
- }
- private void panel1_MouseMove(object sender, MouseEventArgs e)
- {
- Point mouseLocation = e.Location;
- if (onSecondClick)
- {
- double distance = Math.Sqrt(Math.Pow(mouseLocation.X - currentCircle.XCoord, 2) + Math.Pow(mouseLocation.Y - currentCircle.YCoord, 2));
- currentCircle.setRadius((int)distance);
- panel1.Invalidate();
- }
- }
- private void Form1_Resize(object sender, EventArgs e)
- {
- Invalidate(true);
- }
- private void newToolStripMenuItem_Click(object sender, EventArgs e)
- {
- circles = new CircleDoc(Color.Green);
- currentCircle = null;
- onFirstClick = true;
- toSave = true;
- Invalidate(true);
- }
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- saveFile();
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFile();
- }
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- fileName = null;
- saveFile();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement