Pass Task 4.
1: Drawing Program — Multiple Shape Kinds
Program source code:
Program.cs
using System;
using SplashKitSDK;
namespace Shapedrawerr
{
public class Program
{
private enum ShapeKind
{
Rectangle,
Circle,
Line
}
public static void Main()
{
Drawing drawShape = new Drawing();
ShapeKind kindToAdd = ShapeKind.Rectangle;
new Window("Drawing Shape", 800, 600);
do
{
SplashKit.ProcessEvents();
SplashKit.ClearScreen();
if (SplashKit.KeyTyped(KeyCode.RKey))
{
kindToAdd = ShapeKind.Rectangle;
}
if (SplashKit.KeyTyped(KeyCode.LKey))
{
kindToAdd = ShapeKind.Line;
}
if (SplashKit.KeyTyped(KeyCode.CKey))
{
kindToAdd = ShapeKind.Circle;
}
//Start
//Use this to draw shape
if (SplashKit.MouseClicked(MouseButton.LeftButton))
{
if (kindToAdd == ShapeKind.Rectangle)
{
MyRectangle ShapeDrawn = new MyRectangle();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
if (kindToAdd == ShapeKind.Circle)
{
MyCircle ShapeDrawn = new MyCircle();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
if (kindToAdd == ShapeKind.Line)
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
{
MyLine ShapeDrawn = new MyLine();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
Console.WriteLine("Mouse Left");
//End
if (SplashKit.MouseClicked(MouseButton.RightButton))
{
drawShape.SelectedShapeAt(SplashKit.MousePosition());
Console.WriteLine("Mouse Right");
}
//Start
//Additional Function
if (SplashKit.KeyDown(KeyCode.EscapeKey))
{
drawShape.ChangingShapeColor();
Console.WriteLine("ESC");
}
//End
if (SplashKit.KeyTyped(KeyCode.BackspaceKey) ||
SplashKit.KeyTyped(KeyCode.DeleteKey))
{
if (SplashKit.KeyTyped(KeyCode.BackspaceKey))
{
Console.WriteLine("Backspace");
}
if (SplashKit.KeyTyped(KeyCode.DeleteKey))
{
Console.WriteLine("Delete");
}
drawShape.RemoveShape();
}
if (SplashKit.KeyTyped(KeyCode.SpaceKey))
{
drawShape.Background = SplashKit.RandomRGBColor(255);
Console.WriteLine("SpaceKey");
}
drawShape.Draw();
SplashKit.RefreshScreen();
}
while (!SplashKit.WindowCloseRequested("Drawing Shape"));
}
}
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
Shape.cs
using SplashKitSDK;
namespace Shapedrawerr
{
public abstract class Shape
{
private Color _color;
private float _x, _y;
private int _width, _height;
private bool _selected;
public Shape(Color clr)
{
_color = clr;
}
public Color COLOR
{
get
{
return _color;
}
set
{
_color = value;
}
}
public float X
{
get
{
return _x;
}
set
{
_x = value;
}
}
public float Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public abstract void Draw();
public abstract bool IsAt(Point2D p);
public bool Selected
{
get
{
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
return _selected;
}
set
{
_selected = value;
}
}
public abstract void DrawOutline();
}
}
Drawing.cs
using SplashKitSDK;
namespace Shapedrawerr
{
public class Drawing
{
private readonly List<Shape> _shapes;
private Color _background;
StreamWriter writer;
StreamReader reader;
public Drawing(Color background)
{
_shapes = new List<Shape>();
_background = background;
}
public Drawing() : this(Color.White)
{
public List<Shape> SelectedShapes()
{
List<Shape> _selectedShapes = new List<Shape>();
foreach (Shape s in _selectedShapes)
{
if (s.Selected)
{
_selectedShapes.Add(s);
}
}
return _selectedShapes;
}
public int ShapeCount
{
get
{
return _shapes.Count;
}
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
public Color Background
{
get
{
return _background;
}
set
{
_background = value;
}
}
public void Draw()
{
SplashKit.ClearScreen(_background);
foreach (Shape s in _shapes)
{
s.Draw();
}
}
public void ChangingShapeColor()
{
foreach (Shape s in _shapes)
{
if (s.Selected)
{
s.COLOR = Color.RandomRGB(255);
}
}
}
//End
public void SelectedShapeAt(Point2D pt)
{
foreach (Shape s in _shapes)
{
if (s.IsAt(pt))
{
s.Selected = true;
}
else
{
s.Selected = false;
}
}
}
public void AddShape(Shape s)
{
_shapes.Add(s);
}
public void RemoveShape()
{
foreach (Shape s in _shapes.ToList())
{
if (s.Selected)
{
_shapes.Remove(s);
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
}
}
}
}
MyRectangle.cs
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SplashKitSDK;
namespace Shapedrawerr
{
public class MyRectangle : Shape
{
private int _width, _height;
public MyRectangle(Color clr, float x, float y, int width, int height) :
base(clr)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public MyRectangle() : this(Color.RandomRGB(255), 0, 0, 100, 100) { }
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}
public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
public override void Draw()
{
if (Selected)
{
DrawOutline();
}
SplashKit.FillRectangle(COLOR, X, Y, Width, Height);
}
public override void DrawOutline()
{
SplashKit.FillRectangle(Color.Black, X - 2, Y - 2, _width + 4,
_height + 4);
}
public override bool IsAt(Point2D p)
{
return SplashKit.PointInRectangle(p, SplashKit.RectangleFrom(X, Y,
Width, Height));
}
}
}
MyLine.cs
using SplashKitSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shapedrawerr
{
public class MyLine : Shape
{
private int _length;
public MyLine(Color clr, int length) : base(clr)
{
_length = length;
}
public MyLine() : this(Color.RandomRGB(255), 100) { }
public override void Draw()
{
if (Selected)
{
DrawOutline();
}
SplashKit.DrawLine(COLOR, X, Y, X + _length, Y);
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
public int Length
{
get => _length;
set => _length = value;
}
public override void DrawOutline()
{
SplashKit.DrawRectangle(Color.Black, X - 2, Y - 2, _length + 5, 5);
public override bool IsAt(Point2D p)
{
return SplashKit.PointOnLine(p, SplashKit.LineFrom(X, Y, X + _length,
Y));
}
}
}
MyCircle.cs
using SplashKitSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shapedrawerr
{
public class MyCircle : Shape
{
private int _radius;
public MyCircle(Color clr, int radius) : base(clr)
{
_radius = radius;
}
public MyCircle() : this(Color.RandomRGB(255), 50) { }
public override void Draw()
{
if (Selected)
{
DrawOutline();
}
SplashKit.FillCircle(COLOR, X, Y, _radius);
}
public int Radius
{
get
{
return _radius;
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
}
set
{
_radius = value;
}
}
public override void DrawOutline()
{
SplashKit.FillCircle(Color.Black, X, Y, _radius + 4);
}
public override bool IsAt(Point2D pt)
{
//c^2 = a^2 + b^2
//c = square root(a^2 + b^2)
double a = (double)(pt.X - X);
double b = (double)(pt.Y - Y);
if (Math.Sqrt(a * a + b * b) < _radius)
{
return true;
}
return false;
}
}
}
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
Screenshot of program execution
if the user types the R key, change the kindToAdd to ShapeKind. Rectangle.
if the user types the C key, change the kindToAdd to ShapeKind.Circle
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
When user right clicks on shapes, outlines appear.
Esc key to change colour 0f selected shapes.
Pass Task 4.1: Drawing Program — Multiple Shape Kinds
Add some line using L key: