5.
3C - Drawing Program - Saving and Loading
Program source code
Drawing.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyGame;
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;
}
}
public Color Background
{
get
{
return _background;
}
set
{
5.3C - Drawing Program - Saving and Loading
_background = value;
}
}
public void Draw()
{
SplashKit.ClearScreen(_background);
foreach (Shape s in _shapes)
{
s.Draw();
}
}
//Start
//This is an additional function to keep the color of the shape changing
//Like the previous part
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);
}
}
}
//save n load
public void Save(string filename)
{
5.3C - Drawing Program - Saving and Loading
writer = new StreamWriter(filename);
writer.WriteColor(_background);
writer.WriteLine(ShapeCount);
foreach (Shape s in _shapes)
{
s.SaveTo(writer);
}
writer.Close();
}
public void Load(string filename)
{
reader = new StreamReader(filename);
Shape s;
string kind;
Background = reader.ReadColor();
int count = reader.ReadInteger();
_shapes.Clear();
for (int i = 0; i < count; i++)
{
kind = reader.ReadLine();
switch (kind)
{
case "Rectangle":
s = new MyRectangle();
break;
case "Circle":
s = new MyCircle();
break;
case "Line":
s = new MyLine();
break;
default:
throw new InvalidDataException("Error at shape: " +
kind);
}
s.LoadFrom(reader);
AddShape(s);
}
reader.Close();
}
}
}
5.3C - Drawing Program - Saving and Loading
ExtensionMethod.cs
using System;
using System.IO;
using SplashKitSDK;
namespace MyGame
{
public static class ExtensionMethods
{
public static int ReadInteger(this StreamReader reader)
{
return Convert.ToInt32(reader.ReadLine ());
}
public static float ReadSingle(this StreamReader reader)
{
return Convert.ToSingle(reader.ReadLine ());
}
public static Color ReadColor(this StreamReader reader)
{
return Color.RGBColor(reader.ReadSingle(), reader.ReadSingle(),
reader.ReadSingle());
}
public static void WriteColor(this StreamWriter writter, Color clr)
{
writter.WriteLine("{0}\n{1}\n{2}", clr.R, clr.G, clr.B);
}
}
}
MyCircle.cs
using MyGame;
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();
5.3C - Drawing Program - Saving and Loading
}
SplashKit.FillCircle(COLOR, X, Y, _radius);
}
public int Radius
{
get
{
return _radius;
}
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;
}
//save n load
public override void SaveTo(StreamWriter writer)
{
writer.WriteLine("Circle");
base.SaveTo(writer);
writer.WriteLine(Radius);
}
public override void LoadFrom(StreamReader reader)
{
base.LoadFrom(reader);
Radius = reader.ReadInteger();
}
}
}
Myline.cs
using MyGame;
using SplashKitSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shapedrawerr
5.3C - Drawing Program - Saving and Loading
{
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);
}
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));
}
public override void SaveTo(StreamWriter writer)
{
writer.WriteLine("Line");
base.SaveTo(writer);
writer.WriteLine(Length);
}
public override void LoadFrom(StreamReader reader)
{
base.LoadFrom(reader);
Length = reader.ReadInteger();
}
}
}
5.3C - Drawing Program - Saving and Loading
MyRectangle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyGame;
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;
}
}
public override void Draw()
{
if (Selected)
{
DrawOutline();
}
SplashKit.FillRectangle(COLOR, X, Y, Width, Height);
5.3C - Drawing Program - Saving and Loading
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));
}
public override void SaveTo(StreamWriter writer)
{
writer.WriteLine("Rectangle");
base.SaveTo(writer);
writer.WriteLine(Width);
writer.WriteLine(Height);
}
public override void LoadFrom(StreamReader reader)
{
base.LoadFrom(reader);
Width = reader.ReadInteger();
Height = reader.ReadInteger();
}
}
}
5.3C - Drawing Program - Saving and Loading
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);
}
5.3C - Drawing Program - Saving and Loading
if (kindToAdd == ShapeKind.Line)
{
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");
}
//save n load
if (SplashKit.KeyDown(KeyCode.SKey))
{
drawShape.Save("G:/shapes (2).txt");
if (SplashKit.KeyTyped(KeyCode.OKey))
{
try
{
drawShape.Load("G:/shapes (2).txt");
5.3C - Drawing Program - Saving and Loading
}
catch (Exception e)
{
Console.Error.WriteLine("Error loading file: {0}",
e.Message);
}
}
drawShape.Draw();
SplashKit.RefreshScreen();
}
while (!SplashKit.WindowCloseRequested("Drawing Shape"));
}
}
}
Shape.cs
using MyGame;
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;
}
}
5.3C - Drawing Program - Saving and Loading
public float Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public abstract void Draw();
public abstract bool IsAt(Point2D p);
public bool Selected
{
get
{
return _selected;
}
set
{
_selected = value;
}
}
public virtual void SaveTo(StreamWriter writer)
{
writer.WriteColor(_color);
writer.WriteLine(X);
writer.WriteLine(Y);
}
public virtual void LoadFrom(StreamReader reader)
{
COLOR = reader.ReadColor();
X = reader.ReadInteger();
Y = reader.ReadInteger();
}
public abstract void DrawOutline();
}
}
5.3C - Drawing Program - Saving and Loading
Screenshot of program execution
First execution:
Pressing the S key will save the Drawing to
Desktop in a file named shapes(2).txt.
5.3C - Drawing Program - Saving and Loading
Location of saved Txt file:
5.3C - Drawing Program - Saving and Loading
Press “o” to Load