Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11.  
  12. namespace laba6OOP
  13. {
  14.     public partial class Form1 : Form {
  15.  
  16.         Color[] colors = new Color[11];
  17.         public Form1() {
  18.             InitializeComponent();
  19.             this.Height = 450;
  20.             this.Width = 800;
  21.             colors[0] = Color.Black;
  22.             colors[1] = Color.DarkRed;
  23.             colors[2] = Color.Red;
  24.             colors[3] = Color.Orange;
  25.             colors[4] = Color.Yellow;
  26.             colors[5] = Color.LightGreen;
  27.             colors[6] = Color.Green;
  28.             colors[7] = Color.LightBlue;
  29.             colors[8] = Color.Blue;
  30.             colors[9] = Color.DarkBlue;
  31.             colors[10] = Color.Purple;
  32.         }
  33.         Color mycolor = Color.Black;
  34.        
  35.         MyStorage storage = new MyStorage(20);
  36.         string choosen = "Circle";
  37.         int color = 0;
  38.         int MAXx = 800;
  39.         int MAXy = 450;
  40.  
  41.  
  42.         private void pictureBox2_Click(object sender, EventArgs e) {
  43.             if (colorDialog1.ShowDialog() == DialogResult.OK)  {
  44.                 mycolor = colorDialog1.Color;
  45.                 pictureBox1.Refresh();
  46.             }
  47.         }
  48.  
  49.         private void pictureBox1_SizeChanged(object sender, EventArgs e) {
  50.             MAXx = pictureBox1.Size.Width;
  51.             MAXy = pictureBox1.Size.Height;
  52.         }
  53.  
  54.         private void pictureBox1_MouseClick(object sender, MouseEventArgs e) {
  55.             bool flag = false;
  56.             for (int i = 0; i < storage.getSizeArr(); i++) {
  57.                 if (storage.getObject(i) != null)
  58.                 {
  59.                     if (storage.getObject(i).IsMouseInObject(e) == true) {
  60.                         flag = true;
  61.                         storage.getObject(i).select();
  62.                     }
  63.                 }
  64.             }
  65.             if (flag == false) {
  66.                 int k = 0;
  67.                 while (storage.getObject(k) != null)
  68.                     k++;
  69.                 storage.setChoosenObject(choosen, k, e);
  70.             }
  71.             this.Refresh();
  72.         }
  73.  
  74.         private void pictureBox1_Paint(object sender, PaintEventArgs e) {            
  75.             for (int i = 0; i < storage.getCount(); i++)
  76.                 storage.getObject(i).Draw(e, mycolor);
  77.         }
  78.  
  79.         private void btDELETE_Click(object sender, EventArgs e) {
  80.             for (int i = storage.getCount() - 1; i >= 0; i--) {
  81.                 if (storage.getObject(i).IsObjectSelected() == true) {
  82.                     storage.deleteObject(i);
  83.                 }
  84.             }
  85.             pictureBox1.Refresh();
  86.         }
  87.  
  88.         private void btCircle_Click(object sender, EventArgs e) {
  89.             choosen = "Circle";            
  90.         }
  91.  
  92.         private void btSquare_Click(object sender, EventArgs e) {
  93.             choosen = "Rectangle";
  94.         }
  95.  
  96.         private void btTriangle_Click(object sender, EventArgs e) {
  97.             choosen = "Triangle";
  98.         }
  99.  
  100.         private void btSegment_Click(object sender, EventArgs e) {
  101.             choosen = "Segment";
  102.         }
  103.  
  104.         private void button1_Click(object sender, EventArgs e) {
  105.             color = (int)comboBox1.SelectedIndex;
  106.             mycolor = colors[color];
  107.             pictureBox1.Refresh();
  108.         }        
  109.  
  110.         private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
  111.             pictureBox1.Focus();
  112.             storage.MoveChoosenObjects(e, MAXx, MAXy);
  113.             this.Refresh();
  114.         }
  115.  
  116.         private void btCursor_Click(object sender, EventArgs e) {
  117.             choosen = "";
  118.         }
  119.     }
  120.  
  121.     class Point { //базовый класс
  122.         protected int x, y;
  123.         protected bool selected;
  124.         protected Color ObjColor = Color.Black;
  125.  
  126.         public  Point(int _x, int _y) {
  127.             x = _x;
  128.             y = _y;
  129.         }
  130.  
  131.         public int getX() {
  132.             return x;
  133.         }
  134.  
  135.         public int getY() {
  136.             return y;
  137.         }
  138.  
  139.         public virtual bool IsA(string name) {
  140.             if (name == "Point")
  141.                 return true;
  142.             else return false;
  143.         }
  144.  
  145.         public virtual void Draw(PaintEventArgs e, Color k) { }
  146.  
  147.         public virtual bool IsMouseInObject(MouseEventArgs e) {
  148.             return false;
  149.         }
  150.  
  151.         public virtual void select() { }
  152.  
  153.         virtual public bool IsObjectSelected() {
  154.             return selected;
  155.         }
  156.  
  157.         public void setX(int _x) {
  158.             x = _x;
  159.         }
  160.  
  161.         public void setY(int _y) {
  162.             y = _y;
  163.         }
  164.  
  165.         virtual public void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) { }
  166.  
  167.         virtual public void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) { }
  168.     }
  169.  
  170.     class CRectangle : Point {
  171.         private int a, b;
  172.  
  173.         public CRectangle(int _x, int _y): base(_x, _y) {
  174.             a = 40;
  175.             b = 40;
  176.         }
  177.  
  178.         public int getA() {
  179.             return a;
  180.         }
  181.  
  182.         override public  bool IsObjectSelected() {
  183.             return selected;
  184.         }
  185.  
  186.         override public void select() {
  187.             if (selected == false)
  188.                 selected = true;
  189.             else
  190.                 selected = false;
  191.         }
  192.  
  193.         override public void Draw(PaintEventArgs e, Color k) {
  194.  
  195.             if (selected == true) {
  196.                 ObjColor = k;
  197.                 e.Graphics.DrawRectangle(new Pen(ObjColor,2), x, y, a, b);
  198.             }
  199.             else {
  200.                 e.Graphics.DrawRectangle(new Pen(ObjColor), x, y, a, b);
  201.             }
  202.         }
  203.  
  204.         override public bool IsA(string name) {
  205.             if (name == "Point" || name == "CRectangle")
  206.                 return true;
  207.             else return false;
  208.         }
  209.  
  210.         override public bool IsMouseInObject(MouseEventArgs e) {
  211.             if (e.X > x && e.X < x + a && e.Y > y && e.Y < y + b) {
  212.                 return true;
  213.             }
  214.             else
  215.                 return false;
  216.         }
  217.  
  218.         public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  219.             switch (e.KeyCode) {
  220.                 case Keys.Up:
  221.                     if(y - 2 >= 47)
  222.                         y = y - 2;
  223.                     break;
  224.                 case Keys.Down:
  225.                     if(y + 2 + b <= MAXy)
  226.                         y = y + 2;
  227.                     break;
  228.                 case Keys.Left:
  229.                     if(x - 2 >= 0)
  230.                         x = x - 2;
  231.                     break;
  232.                 case Keys.Right:
  233.                     if(x + 2 + a <= MAXx)
  234.                         x = x + 2;
  235.                     break;                
  236.             }
  237.         }
  238.  
  239.         public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  240.             if(e.KeyCode == Keys.Add) {
  241.                 if(x + b + 5 <= MAXx && y + a + 5 <= MAXy) {
  242.                     b = b + 5;
  243.                     a = a + 5;
  244.                 }
  245.             }
  246.             else {
  247.                 if(b >= 6 && a >= 6) {
  248.                     b = b - 5;
  249.                     a = a - 5;
  250.                 }
  251.             }
  252.         }
  253.     }
  254.    
  255.     class CCircle : Point {
  256.         private int r;
  257.  
  258.         public CCircle(int _x, int _y): base(_x, _y){
  259.             r = 20;
  260.         }
  261.  
  262.         public int getRadius(){
  263.             return r;
  264.         }
  265.  
  266.         override public bool IsObjectSelected() {
  267.             return selected;
  268.         }
  269.  
  270.         override public void select() {
  271.             if (selected == false)
  272.                 selected = true;
  273.             else
  274.                 selected = false;
  275.         }
  276.  
  277.         override public void Draw(PaintEventArgs e, Color k) {
  278.             if (selected == true) {
  279.                 ObjColor = k;
  280.                 e.Graphics.DrawEllipse(new Pen(ObjColor, 2), x - r, y - r, r * 2, r * 2);
  281.             }
  282.             else {
  283.                 e.Graphics.DrawEllipse(new Pen(ObjColor), x - r, y - r, r * 2, r * 2);
  284.             }
  285.         }
  286.  
  287.         override public bool IsA(string name) {
  288.             if (name == "Point" || name == "CCircle")
  289.                 return true;
  290.             else return false;
  291.         }
  292.  
  293.         override public bool IsMouseInObject(MouseEventArgs e) {
  294.             int dx = (x - (e.X)) * (x - (e.X));
  295.             int dy = (y - (e.Y)) * (y - (e.Y));
  296.             int distance = dx + dy;
  297.             if (Math.Sqrt(dx + dy) < r) {
  298.                 return true;
  299.             }
  300.             else
  301.                 return false;
  302.         }
  303.  
  304.         public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  305.             switch (e.KeyCode) {
  306.                 case Keys.Up:
  307.                     if(y - 2 >= 47 + r)
  308.                         y = y - 2;
  309.                     break;
  310.                 case Keys.Down:
  311.                     if(y + 2 + r <= MAXy)
  312.                         y = y + 2;
  313.                     break;
  314.                 case Keys.Left:
  315.                     if(x - 2 >= 0 + r)
  316.                         x = x - 2;
  317.                     break;
  318.                 case Keys.Right:
  319.                     if(x + 2 + r <= MAXx)
  320.                         x = x + 2;
  321.                     break;
  322.             }
  323.         }
  324.  
  325.         public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  326.             if (e.KeyCode == Keys.Add) {
  327.                 if (x + r + 5 <= MAXx && y + r + 5 <= MAXy && x - r - 5 >= 0 && y - r - 5 >= 47) {
  328.                     r = r + 5;                    
  329.                 }
  330.             }
  331.             else {
  332.                 if (r >= 6) {
  333.                     r = r - 5;
  334.                 }
  335.             }
  336.         }
  337.     }
  338.  
  339.     class CTriangle : Point {
  340.         private PointF [] F = new PointF[3];
  341.         private int a;
  342.         public CTriangle(int _x, int _y): base(_x, _y) {
  343.             a = 20;
  344.             F[0] = new PointF(x, y - a);
  345.             F[1] = new PointF(x - a, y + a);
  346.             F[2] = new PointF(x + a, y + a);
  347.         }
  348.  
  349.         override public bool IsObjectSelected() {
  350.             return selected;
  351.         }
  352.  
  353.         override public void select() {
  354.             if (selected == false)
  355.                 selected = true;
  356.             else
  357.                 selected = false;
  358.         }
  359.  
  360.         override public void Draw(PaintEventArgs e, Color k) {
  361.             if (selected == true)  {
  362.                 ObjColor = k;
  363.                 e.Graphics.DrawPolygon(new Pen(ObjColor, 2), F);
  364.             }
  365.             else {
  366.                 e.Graphics.DrawPolygon(new Pen(Color.Black), F);
  367.             }
  368.         }
  369.  
  370.         override public bool IsA(string name) {
  371.             if (name == "Point" || name == "CTriangle")
  372.                 return true;
  373.             else return false;
  374.         }
  375.  
  376.         override public bool IsMouseInObject(MouseEventArgs e) {
  377.             if (e.X <= x + a && e.X >= x - a && e.Y <= y + a && e.Y >= y - a && e.Y >= 2*e.X + y - a - 2*x && e.Y >= -2*e.X + y - a + 2*x) {
  378.                 return true;
  379.             }
  380.             else
  381.                 return false;
  382.         }
  383.  
  384.         public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  385.             switch (e.KeyCode) {
  386.                 case Keys.Up:
  387.                     if (y - a - 2 >= 47) {
  388.                         y = y - 2;
  389.                         /*F[0].Y = F[0].Y - 2;
  390.                         F[1].Y = F[1].Y - 2;
  391.                         F[2].Y = F[2].Y - 2;*/
  392.  
  393.                     }
  394.                     break;
  395.                 case Keys.Down:
  396.                     if (y + a + 2 <= MAXy) {
  397.                         y = y + 2;
  398.                         /*F[0].Y = F[0].Y + 2;
  399.                         F[1].Y = F[1].Y + 2;
  400.                         F[2].Y = F[2].Y + 2;*/
  401.                     }
  402.                     break;
  403.                 case Keys.Left:
  404.                     if (x - a - 2 >= 0) {
  405.                         x = x - 2;
  406.                         /*F[0].X = F[0].X - 2;
  407.                         F[1].X = F[1].X - 2;
  408.                         F[2].X = F[2].X - 2;*/
  409.                     }
  410.                     break;
  411.                 case Keys.Right:
  412.                     if (x + a + 2 <= MAXx) {
  413.                         x = x + 2;
  414.                         /*F[0].X = F[0].X + 2;
  415.                         F[1].X = F[1].X + 2;
  416.                         F[2].X = F[2].X + 2;*/
  417.                     }
  418.                     break;
  419.             }
  420.         }
  421.         public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  422.             if(e.KeyCode == Keys.Add) {
  423.                 if(x + a + 2 <= MAXx && x - a - 2 >= 47 && y - a - 2 >= 47 && y + a + 2 <= MAXy) {
  424.                     a = a + 2;
  425.                     F[0] = new PointF(x, y - a);
  426.                     F[1] = new PointF(x - a, y + a);
  427.                     F[2] = new PointF(x + a, y + a);
  428.                 }
  429.             }
  430.             else {
  431.                 if(a >= 5) {
  432.                     a = a - 2;
  433.                     F[0] = new PointF(x, y - a);
  434.                     F[1] = new PointF(x - a, y + a);
  435.                     F[2] = new PointF(x + a, y + a);
  436.                 }
  437.             }
  438.         }
  439.      }
  440.  
  441.     class CSegment : Point {
  442.         Point p;
  443.         public CSegment(int _x, int _y) : base(_x, _y) {
  444.             p = new Point(_x + 40, _y);  
  445.         }
  446.  
  447.         override public bool IsObjectSelected() {
  448.             return selected;
  449.         }
  450.  
  451.         override public void select() {
  452.             if (selected == false)
  453.                 selected = true;
  454.             else
  455.                 selected = false;
  456.         }
  457.  
  458.         override public void Draw(PaintEventArgs e, Color k) {
  459.             if (selected == true) {
  460.                 ObjColor = k;
  461.                 e.Graphics.DrawLine(new Pen(ObjColor, 2), x, y, p.getX(), p.getY());
  462.             }
  463.             else {
  464.                 e.Graphics.DrawLine(new Pen(ObjColor), x, y, p.getX(), p.getY());
  465.             }
  466.         }
  467.  
  468.         override public bool IsA(string name) {
  469.             if (name == "Point" || name == "CSegment")
  470.                 return true;
  471.             else return false;
  472.         }
  473.  
  474.         override public bool IsMouseInObject(MouseEventArgs e) {
  475.             if (x <=  e.X &&  p.getX() >= e.X && y + 2 >= e.Y && p.getY() - 2 <= e.Y)  {
  476.                 return true;
  477.             }
  478.             else
  479.                 return false;
  480.         }
  481.  
  482.         public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  483.             switch (e.KeyCode) {
  484.                 case Keys.Up:
  485.                     if (y - 2 >= 47) {
  486.                         y = y - 2;
  487.                         p.setY(p.getY() - 2);
  488.                     }
  489.                     break;
  490.                 case Keys.Down:
  491.                     if (y + 2 <= MAXy) {
  492.                         y = y + 2;
  493.                         p.setY(p.getY() + 2);
  494.                     }
  495.                     break;
  496.                 case Keys.Left:
  497.                     if (x - 2 >= 0) {
  498.                         x = x - 2;
  499.                         p.setX(p.getX() - 2);
  500.                     }
  501.                     break;
  502.                 case Keys.Right:
  503.                     if (x + 42 <= MAXx){
  504.                         x = x + 2;
  505.                         p.setX(p.getX() + 2);
  506.                     }  
  507.                     break;
  508.             }
  509.         }
  510.  
  511.         public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  512.             if (e.KeyCode == Keys.Add) {
  513.                 if (x - 2 >= 0 && p.getX() + 2 <= MAXx) {
  514.                     x = x - 2;
  515.                     p.setX(p.getX() + 2);
  516.                 }
  517.             }
  518.             else {
  519.                 if (p.getX() - 2 >= 0 && x < p.getX())
  520.                 {
  521.                     x = x + 2;
  522.                     p.setX(p.getX() - 2);
  523.                 }
  524.             }
  525.         }
  526.     }
  527.  
  528.     class MyStorage {
  529.         private Point[] arr;//массив с объектами
  530.         private int Count;
  531.         public MyStorage() {
  532.             arr = new Point[2];
  533.             Count = 0;
  534.         }
  535.  
  536.         public MyStorage(int i) {
  537.             arr = new Point[i];
  538.             Count = 0;
  539.         }
  540.  
  541.         public MyStorage(MyStorage x) {
  542.             arr = x.arr;
  543.             Count = x.Count;
  544.         }
  545.  
  546.         public int getCount() {
  547.             return Count;
  548.         }
  549.  
  550.         public int getSizeArr() {
  551.             return arr.Length;
  552.         }
  553.  
  554.         public void increaseArr(int k, Point x) {
  555.             //Array.Resize(ref arr, Count + 1);
  556.             Point[] mas = arr;
  557.             arr = new Point[Count + 1];
  558.             for (int i = 0; i < Count; i++) {
  559.                 arr[i] = mas[i];
  560.             }
  561.             for (int i = arr.Length - 1; i > k; i--) {
  562.                 arr[i] = arr[i - 1];
  563.             }
  564.             arr[k] = x;
  565.             Count++;
  566.         }
  567.  
  568.         public void setObject(int k, Point x) {
  569.             if (Count == arr.Length) {
  570.                 increaseArr(k, x);
  571.             }
  572.             else {
  573.                 for (int i = Count; i > k; i--) {
  574.                     arr[i] = arr[i - 1];
  575.                 }
  576.                 Count++;
  577.                 arr[k] = x;
  578.             }
  579.         }
  580.  
  581.         public void deleteObject(int k) {
  582.             for (int i = k; i < arr.Length - 1; i++) {
  583.                 arr[i] = arr[i + 1];
  584.             }
  585.             arr[arr.Length - 1] = null;
  586.             Count--;
  587.         }
  588.  
  589.         public bool isObjectInStorage(Point x) {
  590.             for (int i = 0; i < arr.Length; i++) {
  591.                 if (arr[i] == x) {
  592.                     return true;
  593.                 }
  594.             }
  595.             return false;
  596.         }
  597.         public Point getObject(int i) {
  598.             if (i < arr.Length) {
  599.                 return arr[i];
  600.             }
  601.             return null;
  602.         }
  603.  
  604.         public void setChoosenObject(string s, int k, MouseEventArgs e) {
  605.             if (s == "Circle")
  606.                 setObject(k, new CCircle(e.X, e.Y));
  607.             else if (s == "Rectangle")
  608.                 setObject(k, new CRectangle(e.X, e.Y));
  609.             else if (s == "Triangle")
  610.                 setObject(k, new CTriangle(e.X, e.Y));
  611.             else if (s == "Segment")
  612.                 setObject(k, new CSegment(e.X, e.Y));
  613.         }
  614.        
  615.         public void MoveChoosenObjects(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
  616.             for(int i = 0; i < Count; i++) {
  617.                 if(arr[i].IsObjectSelected() == true) {
  618.                     if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract)
  619.                         arr[i].ChangeSize(e, MAXx, MAXy);
  620.                     else
  621.                         arr[i].move(e, MAXx, MAXy);
  622.                 }
  623.             }
  624.         }
  625.     }
  626. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement