Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.30 KB | None | 0 0
  1.     [Serializable]
  2.     public class Circle
  3.     {
  4.         public int XCoord { get; set; }
  5.         public int YCoord { get; set; }
  6.         public int Radius { get; set; }
  7.         public bool selected { get; set; }
  8.         public bool secondClicked { get; set; }
  9.         public int Velocity { get; set; }
  10.         public Color color { get; set; }
  11.  
  12.         public Circle(int xCoord, int yCoord,Color color)
  13.         {
  14.             XCoord = xCoord;
  15.             YCoord = yCoord;
  16.             this.color = color;
  17.             Velocity = 10;
  18.         }
  19.  
  20.         public void setRadius(int Radius)
  21.         {
  22.             this.Radius = Radius;
  23.         }
  24.  
  25.         public void select()
  26.         {
  27.             if (selected)
  28.                 selected = false;
  29.             else
  30.                 selected = true;
  31.         }
  32.  
  33.      
  34.  
  35.         public void draw(Graphics g)
  36.         {
  37.             Rectangle rectangle = new Rectangle(XCoord - Radius, YCoord - Radius, Radius * 2, Radius * 2);
  38.             if (secondClicked)
  39.             {
  40.                 Brush brush = new SolidBrush(color);
  41.                 g.FillEllipse(brush, rectangle);
  42.                 if (selected)
  43.                 {
  44.                     Pen pen = new Pen(Color.Red,4);
  45.                     g.DrawEllipse(pen, rectangle);
  46.                     pen.Dispose();
  47.                 }
  48.                 brush.Dispose();
  49.             }
  50.             else
  51.             {
  52.                 Pen pen = new Pen(Color.Black,4);
  53.                 pen.DashStyle=DashStyle.Dot;
  54.                 pen.DashOffset = 2f;
  55.                 g.DrawEllipse(pen, rectangle);
  56.                 pen.Dispose();
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62.  
  63.    [Serializable]
  64.     public class CircleDoc
  65.     {
  66.         public List<Circle> circles;
  67.         public Color currentColor;
  68.         public int Velocity { get; set; }
  69.         public CircleDoc(Color currentColor)
  70.         {
  71.             Velocity = 5;
  72.             circles = new List<Circle>();
  73.             this.currentColor = currentColor;
  74.         }
  75.  
  76.         public void setColor(Color color)
  77.         {
  78.             this.currentColor = color;
  79.         }
  80.  
  81.         public void draw(Graphics g)
  82.         {
  83.             foreach(Circle circle in circles)
  84.             {
  85.                 circle.draw(g);
  86.             }
  87.         }
  88.  
  89.         public void move(Keys keyCode)
  90.         {
  91.             for(int i=circles.Count-1;i>=0;i--)
  92.             {
  93.                 if (circles.Count - 1 >= i)
  94.                 {
  95.                     Circle circle = circles.ElementAt(i);
  96.                     if (circle.selected && circle != null)
  97.                     {
  98.                         {
  99.                             switch (keyCode)
  100.                             {
  101.                                 case Keys.Up:
  102.                                     circle.YCoord -= Velocity;
  103.                                     checkIntersect(circle);
  104.                                     break;
  105.                                 case Keys.Right:
  106.                                     circle.XCoord += Velocity;
  107.                                     checkIntersect(circle);
  108.                                     break;
  109.                                 case Keys.Left:
  110.                                     circle.XCoord -= Velocity;
  111.                                     checkIntersect(circle);
  112.                                     break;
  113.                                 case Keys.Down:
  114.                                     circle.YCoord += Velocity;
  115.                                     checkIntersect(circle);
  116.                                     break;
  117.                             }
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         private void checkIntersect(Circle circle)
  125.         {
  126.            for(int i = circles.Count - 1; i >= 0; i--)
  127.             {
  128.                 if (circles.ElementAt(i) != circle&&circles.ElementAt(i).color==circle.color)
  129.                 {
  130.                     double distance = Math.Sqrt(Math.Pow(circles.ElementAt(i).XCoord - circle.XCoord, 2) + Math.Pow(circles.ElementAt(i).YCoord - circle.YCoord, 2));
  131.                     if (distance <= circles.ElementAt(i).Radius + circle.Radius)
  132.                     {
  133.                         circles.RemoveAt(i);
  134.                         circles.Remove(circle);
  135.                         return;
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.  
  141.         public void addCircle(Circle circle)
  142.         {
  143.             circles.Add(circle);
  144.         }
  145.  
  146.         public void removeCircle(Circle circle)
  147.         {
  148.             circles.Remove(circle);
  149.         }
  150.  
  151.         public int circleNumber()
  152.         {
  153.             int count = 0;
  154.             foreach(Circle circle in circles)
  155.             {
  156.                 if (circle.secondClicked)
  157.                     count++;
  158.             }
  159.  
  160.             return count;
  161.         }
  162.  
  163.         public void selectCircle(Point clickLocation)
  164.         {
  165.             foreach(Circle circle in circles)
  166.             {
  167.            
  168.                 double distance = Math.Sqrt(Math.Pow(clickLocation.X - circle.XCoord, 2) + Math.Pow(clickLocation.Y - circle.YCoord, 2));
  169.                 if(distance<=circle.Radius)
  170.                 {
  171.                     circle.select();
  172.                     return;
  173.                 }
  174.             }
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180.  
  181.     public partial class Form1 : Form
  182.     {
  183.         class DoubleBufferedPanel : Panel
  184.         {
  185.             public DoubleBufferedPanel() : base()
  186.             {
  187.                 DoubleBuffered = true;
  188.             }
  189.         }
  190.  
  191.         String fileName;
  192.         bool toSave;
  193.         CircleDoc circles;
  194.         Circle currentCircle;
  195.         bool onFirstClick;
  196.         bool onSecondClick;
  197.         public Form1()
  198.         {
  199.             InitializeComponent();
  200.             newFile();
  201.         }
  202.  
  203.         public void openFile()
  204.         {
  205.             OpenFileDialog openFileDialog = new OpenFileDialog();
  206.             openFileDialog.Title = "Opening circles...";
  207.             openFileDialog.Filter = "Circle file(*.cir)|*.cir";
  208.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  209.             {
  210.                 fileName = openFileDialog.FileName;
  211.                 IFormatter formatter = new BinaryFormatter();
  212.                 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  213.                 circles=(CircleDoc)formatter.Deserialize(fileStream);
  214.                 fileStream.Close();
  215.             }
  216.         }
  217.  
  218.         public void saveFile()
  219.         {
  220.             if (fileName == null)
  221.             {
  222.                 SaveFileDialog saveFileDialog = new SaveFileDialog();
  223.                 saveFileDialog.Title = "Saving circles...";
  224.                 saveFileDialog.Filter = "Circle file(*.cir)|*.cir";
  225.                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
  226.                 {
  227.                     fileName = saveFileDialog.FileName;
  228.                 }
  229.             }
  230.             if (fileName != null)
  231.             {
  232.                 IFormatter formatter = new BinaryFormatter();
  233.                 FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
  234.                 formatter.Serialize(fileStream, circles);
  235.                 fileStream.Close();
  236.             }
  237.         }
  238.         public void newFile()
  239.         {
  240.             circles = new CircleDoc(Color.Green);
  241.             fileName = null;
  242.             toSave = true;
  243.             currentCircle = null;
  244.             onFirstClick = true;
  245.         }
  246.  
  247.         public void panel1_MouseClick(object sender, MouseEventArgs e)
  248.         {
  249.             Point clickLocation = e.Location;
  250.             if (e.Button == MouseButtons.Left)
  251.             {
  252.                 if (onFirstClick)
  253.                 {
  254.                     Circle newCircle = new Circle(clickLocation.X, clickLocation.Y, circles.currentColor);
  255.                     currentCircle = newCircle;
  256.                     circles.addCircle(currentCircle);
  257.                     onFirstClick = false;
  258.                     onSecondClick = true;
  259.                 }
  260.                 else if (onSecondClick)
  261.                 {
  262.                     if (currentCircle != null)
  263.                     {
  264.                         currentCircle.secondClicked = true;
  265.                         Invalidate(true);
  266.                         onFirstClick = true;
  267.                         onSecondClick = false;
  268.                     }
  269.                 }
  270.             }
  271.             else if (e.Button == MouseButtons.Right)
  272.             {
  273.                 circles.selectCircle(clickLocation);
  274.                 panel1.Invalidate();
  275.             }
  276.         }
  277.  
  278.         private void colorToolStripMenuItem_Click(object sender, EventArgs e)
  279.         {
  280.             ColorDialog colorDialog = new ColorDialog();
  281.             if (colorDialog.ShowDialog() == DialogResult.OK)
  282.             {
  283.                 circles.setColor(colorDialog.Color);
  284.             }
  285.         }
  286.  
  287.         private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
  288.         {
  289.             toolStripStatusLabel1.Text = "Circles: " + circles.circleNumber();
  290.         }
  291.  
  292.         private void Form1_Load(object sender, EventArgs e)
  293.         {
  294.             this.Text = "Color circles";
  295.         }
  296.  
  297.         private void panel1_Paint(object sender, PaintEventArgs e)
  298.         {
  299.             Graphics graphics = e.Graphics;
  300.             circles.draw(graphics);
  301.         }
  302.  
  303.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  304.         {
  305.             if (e.KeyCode == Keys.Escape)
  306.             {
  307.                 if (onSecondClick)
  308.                 {
  309.                     circles.removeCircle(currentCircle);
  310.                     currentCircle = null;
  311.                     onSecondClick = false;
  312.                     onFirstClick = true;
  313.                     Invalidate(true);
  314.                 }
  315.             }
  316.             else if (e.KeyCode == Keys.Up||e.KeyCode==Keys.Right||e.KeyCode==Keys.Left||e.KeyCode==Keys.Down)
  317.             {
  318.                 circles.move(e.KeyCode);
  319.                 Invalidate(true);
  320.             }
  321.         }
  322.  
  323.         private void panel1_MouseMove(object sender, MouseEventArgs e)
  324.         {
  325.             Point mouseLocation = e.Location;
  326.             if (onSecondClick)
  327.             {
  328.                 double distance = Math.Sqrt(Math.Pow(mouseLocation.X - currentCircle.XCoord, 2) + Math.Pow(mouseLocation.Y - currentCircle.YCoord, 2));
  329.                 currentCircle.setRadius((int)distance);
  330.                 panel1.Invalidate();
  331.             }
  332.         }
  333.  
  334.         private void Form1_Resize(object sender, EventArgs e)
  335.         {
  336.             Invalidate(true);
  337.         }
  338.  
  339.         private void newToolStripMenuItem_Click(object sender, EventArgs e)
  340.         {
  341.             circles = new CircleDoc(Color.Green);
  342.             currentCircle = null;
  343.             onFirstClick = true;
  344.             toSave = true;
  345.             Invalidate(true);
  346.         }
  347.  
  348.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  349.         {
  350.             saveFile();
  351.         }
  352.  
  353.         private void openToolStripMenuItem_Click(object sender, EventArgs e)
  354.         {
  355.             openFile();
  356.         }
  357.  
  358.         private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
  359.         {
  360.             fileName = null;
  361.             saveFile();
  362.         }
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement