Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace laba6OOP
- {
- public partial class Form1 : Form {
- Color[] colors = new Color[11];
- public Form1() {
- InitializeComponent();
- this.Height = 450;
- this.Width = 800;
- colors[0] = Color.Black;
- colors[1] = Color.DarkRed;
- colors[2] = Color.Red;
- colors[3] = Color.Orange;
- colors[4] = Color.Yellow;
- colors[5] = Color.LightGreen;
- colors[6] = Color.Green;
- colors[7] = Color.LightBlue;
- colors[8] = Color.Blue;
- colors[9] = Color.DarkBlue;
- colors[10] = Color.Purple;
- }
- Color mycolor = Color.Black;
- MyStorage storage = new MyStorage(20);
- string choosen = "Circle";
- int color = 0;
- int MAXx = 800;
- int MAXy = 450;
- private void pictureBox2_Click(object sender, EventArgs e) {
- if (colorDialog1.ShowDialog() == DialogResult.OK) {
- mycolor = colorDialog1.Color;
- pictureBox1.Refresh();
- }
- }
- private void pictureBox1_SizeChanged(object sender, EventArgs e) {
- MAXx = pictureBox1.Size.Width;
- MAXy = pictureBox1.Size.Height;
- }
- private void pictureBox1_MouseClick(object sender, MouseEventArgs e) {
- bool flag = false;
- for (int i = 0; i < storage.getSizeArr(); i++) {
- if (storage.getObject(i) != null)
- {
- if (storage.getObject(i).IsMouseInObject(e) == true) {
- flag = true;
- storage.getObject(i).select();
- }
- }
- }
- if (flag == false) {
- int k = 0;
- while (storage.getObject(k) != null)
- k++;
- storage.setChoosenObject(choosen, k, e);
- }
- this.Refresh();
- }
- private void pictureBox1_Paint(object sender, PaintEventArgs e) {
- for (int i = 0; i < storage.getCount(); i++)
- storage.getObject(i).Draw(e, mycolor);
- }
- private void btDELETE_Click(object sender, EventArgs e) {
- for (int i = storage.getCount() - 1; i >= 0; i--) {
- if (storage.getObject(i).IsObjectSelected() == true) {
- storage.deleteObject(i);
- }
- }
- pictureBox1.Refresh();
- }
- private void btCircle_Click(object sender, EventArgs e) {
- choosen = "Circle";
- }
- private void btSquare_Click(object sender, EventArgs e) {
- choosen = "Rectangle";
- }
- private void btTriangle_Click(object sender, EventArgs e) {
- choosen = "Triangle";
- }
- private void btSegment_Click(object sender, EventArgs e) {
- choosen = "Segment";
- }
- private void button1_Click(object sender, EventArgs e) {
- color = (int)comboBox1.SelectedIndex;
- mycolor = colors[color];
- pictureBox1.Refresh();
- }
- private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
- pictureBox1.Focus();
- storage.MoveChoosenObjects(e, MAXx, MAXy);
- this.Refresh();
- }
- private void btCursor_Click(object sender, EventArgs e) {
- choosen = "";
- }
- }
- class Point { //базовый класс
- protected int x, y;
- protected bool selected;
- protected Color ObjColor = Color.Black;
- public Point(int _x, int _y) {
- x = _x;
- y = _y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public virtual bool IsA(string name) {
- if (name == "Point")
- return true;
- else return false;
- }
- public virtual void Draw(PaintEventArgs e, Color k) { }
- public virtual bool IsMouseInObject(MouseEventArgs e) {
- return false;
- }
- public virtual void select() { }
- virtual public bool IsObjectSelected() {
- return selected;
- }
- public void setX(int _x) {
- x = _x;
- }
- public void setY(int _y) {
- y = _y;
- }
- virtual public void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) { }
- virtual public void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) { }
- }
- class CRectangle : Point {
- private int a, b;
- public CRectangle(int _x, int _y): base(_x, _y) {
- a = 40;
- b = 40;
- }
- public int getA() {
- return a;
- }
- override public bool IsObjectSelected() {
- return selected;
- }
- override public void select() {
- if (selected == false)
- selected = true;
- else
- selected = false;
- }
- override public void Draw(PaintEventArgs e, Color k) {
- if (selected == true) {
- ObjColor = k;
- e.Graphics.DrawRectangle(new Pen(ObjColor,2), x, y, a, b);
- }
- else {
- e.Graphics.DrawRectangle(new Pen(ObjColor), x, y, a, b);
- }
- }
- override public bool IsA(string name) {
- if (name == "Point" || name == "CRectangle")
- return true;
- else return false;
- }
- override public bool IsMouseInObject(MouseEventArgs e) {
- if (e.X > x && e.X < x + a && e.Y > y && e.Y < y + b) {
- return true;
- }
- else
- return false;
- }
- public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- switch (e.KeyCode) {
- case Keys.Up:
- if(y - 2 >= 47)
- y = y - 2;
- break;
- case Keys.Down:
- if(y + 2 + b <= MAXy)
- y = y + 2;
- break;
- case Keys.Left:
- if(x - 2 >= 0)
- x = x - 2;
- break;
- case Keys.Right:
- if(x + 2 + a <= MAXx)
- x = x + 2;
- break;
- }
- }
- public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- if(e.KeyCode == Keys.Add) {
- if(x + b + 5 <= MAXx && y + a + 5 <= MAXy) {
- b = b + 5;
- a = a + 5;
- }
- }
- else {
- if(b >= 6 && a >= 6) {
- b = b - 5;
- a = a - 5;
- }
- }
- }
- }
- class CCircle : Point {
- private int r;
- public CCircle(int _x, int _y): base(_x, _y){
- r = 20;
- }
- public int getRadius(){
- return r;
- }
- override public bool IsObjectSelected() {
- return selected;
- }
- override public void select() {
- if (selected == false)
- selected = true;
- else
- selected = false;
- }
- override public void Draw(PaintEventArgs e, Color k) {
- if (selected == true) {
- ObjColor = k;
- e.Graphics.DrawEllipse(new Pen(ObjColor, 2), x - r, y - r, r * 2, r * 2);
- }
- else {
- e.Graphics.DrawEllipse(new Pen(ObjColor), x - r, y - r, r * 2, r * 2);
- }
- }
- override public bool IsA(string name) {
- if (name == "Point" || name == "CCircle")
- return true;
- else return false;
- }
- override public bool IsMouseInObject(MouseEventArgs e) {
- int dx = (x - (e.X)) * (x - (e.X));
- int dy = (y - (e.Y)) * (y - (e.Y));
- int distance = dx + dy;
- if (Math.Sqrt(dx + dy) < r) {
- return true;
- }
- else
- return false;
- }
- public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- switch (e.KeyCode) {
- case Keys.Up:
- if(y - 2 >= 47 + r)
- y = y - 2;
- break;
- case Keys.Down:
- if(y + 2 + r <= MAXy)
- y = y + 2;
- break;
- case Keys.Left:
- if(x - 2 >= 0 + r)
- x = x - 2;
- break;
- case Keys.Right:
- if(x + 2 + r <= MAXx)
- x = x + 2;
- break;
- }
- }
- public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- if (e.KeyCode == Keys.Add) {
- if (x + r + 5 <= MAXx && y + r + 5 <= MAXy && x - r - 5 >= 0 && y - r - 5 >= 47) {
- r = r + 5;
- }
- }
- else {
- if (r >= 6) {
- r = r - 5;
- }
- }
- }
- }
- class CTriangle : Point {
- private PointF [] F = new PointF[3];
- private int a;
- public CTriangle(int _x, int _y): base(_x, _y) {
- a = 20;
- F[0] = new PointF(x, y - a);
- F[1] = new PointF(x - a, y + a);
- F[2] = new PointF(x + a, y + a);
- }
- override public bool IsObjectSelected() {
- return selected;
- }
- override public void select() {
- if (selected == false)
- selected = true;
- else
- selected = false;
- }
- override public void Draw(PaintEventArgs e, Color k) {
- if (selected == true) {
- ObjColor = k;
- e.Graphics.DrawPolygon(new Pen(ObjColor, 2), F);
- }
- else {
- e.Graphics.DrawPolygon(new Pen(Color.Black), F);
- }
- }
- override public bool IsA(string name) {
- if (name == "Point" || name == "CTriangle")
- return true;
- else return false;
- }
- override public bool IsMouseInObject(MouseEventArgs e) {
- 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) {
- return true;
- }
- else
- return false;
- }
- public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- switch (e.KeyCode) {
- case Keys.Up:
- if (y - a - 2 >= 47) {
- y = y - 2;
- /*F[0].Y = F[0].Y - 2;
- F[1].Y = F[1].Y - 2;
- F[2].Y = F[2].Y - 2;*/
- }
- break;
- case Keys.Down:
- if (y + a + 2 <= MAXy) {
- y = y + 2;
- /*F[0].Y = F[0].Y + 2;
- F[1].Y = F[1].Y + 2;
- F[2].Y = F[2].Y + 2;*/
- }
- break;
- case Keys.Left:
- if (x - a - 2 >= 0) {
- x = x - 2;
- /*F[0].X = F[0].X - 2;
- F[1].X = F[1].X - 2;
- F[2].X = F[2].X - 2;*/
- }
- break;
- case Keys.Right:
- if (x + a + 2 <= MAXx) {
- x = x + 2;
- /*F[0].X = F[0].X + 2;
- F[1].X = F[1].X + 2;
- F[2].X = F[2].X + 2;*/
- }
- break;
- }
- }
- public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- if(e.KeyCode == Keys.Add) {
- if(x + a + 2 <= MAXx && x - a - 2 >= 47 && y - a - 2 >= 47 && y + a + 2 <= MAXy) {
- a = a + 2;
- F[0] = new PointF(x, y - a);
- F[1] = new PointF(x - a, y + a);
- F[2] = new PointF(x + a, y + a);
- }
- }
- else {
- if(a >= 5) {
- a = a - 2;
- F[0] = new PointF(x, y - a);
- F[1] = new PointF(x - a, y + a);
- F[2] = new PointF(x + a, y + a);
- }
- }
- }
- }
- class CSegment : Point {
- Point p;
- public CSegment(int _x, int _y) : base(_x, _y) {
- p = new Point(_x + 40, _y);
- }
- override public bool IsObjectSelected() {
- return selected;
- }
- override public void select() {
- if (selected == false)
- selected = true;
- else
- selected = false;
- }
- override public void Draw(PaintEventArgs e, Color k) {
- if (selected == true) {
- ObjColor = k;
- e.Graphics.DrawLine(new Pen(ObjColor, 2), x, y, p.getX(), p.getY());
- }
- else {
- e.Graphics.DrawLine(new Pen(ObjColor), x, y, p.getX(), p.getY());
- }
- }
- override public bool IsA(string name) {
- if (name == "Point" || name == "CSegment")
- return true;
- else return false;
- }
- override public bool IsMouseInObject(MouseEventArgs e) {
- if (x <= e.X && p.getX() >= e.X && y + 2 >= e.Y && p.getY() - 2 <= e.Y) {
- return true;
- }
- else
- return false;
- }
- public override void move(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- switch (e.KeyCode) {
- case Keys.Up:
- if (y - 2 >= 47) {
- y = y - 2;
- p.setY(p.getY() - 2);
- }
- break;
- case Keys.Down:
- if (y + 2 <= MAXy) {
- y = y + 2;
- p.setY(p.getY() + 2);
- }
- break;
- case Keys.Left:
- if (x - 2 >= 0) {
- x = x - 2;
- p.setX(p.getX() - 2);
- }
- break;
- case Keys.Right:
- if (x + 42 <= MAXx){
- x = x + 2;
- p.setX(p.getX() + 2);
- }
- break;
- }
- }
- public override void ChangeSize(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- if (e.KeyCode == Keys.Add) {
- if (x - 2 >= 0 && p.getX() + 2 <= MAXx) {
- x = x - 2;
- p.setX(p.getX() + 2);
- }
- }
- else {
- if (p.getX() - 2 >= 0 && x < p.getX())
- {
- x = x + 2;
- p.setX(p.getX() - 2);
- }
- }
- }
- }
- class MyStorage {
- private Point[] arr;//массив с объектами
- private int Count;
- public MyStorage() {
- arr = new Point[2];
- Count = 0;
- }
- public MyStorage(int i) {
- arr = new Point[i];
- Count = 0;
- }
- public MyStorage(MyStorage x) {
- arr = x.arr;
- Count = x.Count;
- }
- public int getCount() {
- return Count;
- }
- public int getSizeArr() {
- return arr.Length;
- }
- public void increaseArr(int k, Point x) {
- //Array.Resize(ref arr, Count + 1);
- Point[] mas = arr;
- arr = new Point[Count + 1];
- for (int i = 0; i < Count; i++) {
- arr[i] = mas[i];
- }
- for (int i = arr.Length - 1; i > k; i--) {
- arr[i] = arr[i - 1];
- }
- arr[k] = x;
- Count++;
- }
- public void setObject(int k, Point x) {
- if (Count == arr.Length) {
- increaseArr(k, x);
- }
- else {
- for (int i = Count; i > k; i--) {
- arr[i] = arr[i - 1];
- }
- Count++;
- arr[k] = x;
- }
- }
- public void deleteObject(int k) {
- for (int i = k; i < arr.Length - 1; i++) {
- arr[i] = arr[i + 1];
- }
- arr[arr.Length - 1] = null;
- Count--;
- }
- public bool isObjectInStorage(Point x) {
- for (int i = 0; i < arr.Length; i++) {
- if (arr[i] == x) {
- return true;
- }
- }
- return false;
- }
- public Point getObject(int i) {
- if (i < arr.Length) {
- return arr[i];
- }
- return null;
- }
- public void setChoosenObject(string s, int k, MouseEventArgs e) {
- if (s == "Circle")
- setObject(k, new CCircle(e.X, e.Y));
- else if (s == "Rectangle")
- setObject(k, new CRectangle(e.X, e.Y));
- else if (s == "Triangle")
- setObject(k, new CTriangle(e.X, e.Y));
- else if (s == "Segment")
- setObject(k, new CSegment(e.X, e.Y));
- }
- public void MoveChoosenObjects(PreviewKeyDownEventArgs e, int MAXx, int MAXy) {
- for(int i = 0; i < Count; i++) {
- if(arr[i].IsObjectSelected() == true) {
- if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract)
- arr[i].ChangeSize(e, MAXx, MAXy);
- else
- arr[i].move(e, MAXx, MAXy);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement