/*********************************** * Author: Angelo M Sanches * makes a new resized canvas to draw to *************************************/ private void Bu_ReSize_Click(object sender, EventArgs e) { DrawCheck.Enabled = false; Drawer.Close(); Drawer = new CDrawer(Scale * trackBar1.Value, Scale * trackBar1.Value, false); Drawer.Scale = Scale; DrawLife = new Form1.Life[trackBar1.Value, trackBar1.Value]; DrawCheck.Enabled = true; }
private void FormMain_Load(object sender, EventArgs e) { if (_ThreadGameLoop != null) { _ThreadGameLoop.Abort(); _Canvas.Close(); } //Launch thread _ThreadGameLoop = new Thread(GameLoop); _ThreadGameLoop.Start(); timerMouseClicks.Start(); }
static void RandomBlocks() { Random rnd = new Random(); CDrawer can = new CDrawer(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Reset(); watch.Start(); can.AddText("Random Known Colors SetBBPixel : 2s", 28, 0, 0, can.ScaledWidth, can.ScaledHeight, Color.White); can.AddText("Random Known Colors SetBBPixel : 2s", 28, 2, 2, can.ScaledWidth + 2, can.ScaledHeight + 2, Color.Black); while (watch.ElapsedMilliseconds < 2000) { can.SetBBPixel(rnd.Next(can.ScaledWidth), rnd.Next(can.ScaledHeight), RandColor.GetKnownColor()); } can.Close(); can = new CDrawer(800, 800); can.Scale = 10; Console.WriteLine("Random Known Colors SetBBScaledPixel : 2s"); watch.Reset(); watch.Start(); can.AddText("Random Known Colors SetBBScaledPixel : 2s", 24); while (watch.ElapsedMilliseconds < 2000) { can.SetBBScaledPixel(rnd.Next(can.ScaledWidth), rnd.Next(can.ScaledHeight), RandColor.GetKnownColor()); } can.Close(); }
internal void Restart() { _Canvas.Close(); _Canvas = null; _Embers.Clear(); _EmbersToRemove.Clear(); _Canvas = new CDrawer(MAXSIZE, MAXSIZE, false); Generate(1500); Display(); }
/// <summary> /// on click with set number of lines wanting to test /// simulate a queue system /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void simulateBtn_Click(object sender, EventArgs e) { //clear running flag _running = false; //use any() with a lambda to perform sleep(10) in a loop //until all queues are empty(signal that threads r terminated) while (Qsheep.Any(Qs => Qs.Count > 0) || StackSheep.Count > 0) { Thread.Sleep(10); } //Clear your list and stats. Qsheep.Clear(); count = 0; //close exsisting drawer and allocate new one if (CDrawer != null) { CDrawer.Close(); } CDrawer = new CDrawer(800, (int)howManyUpDown1.Value * scale, false); CDrawer.Position = new Point(this.Width, this.Height - 124); //CDrawer.Scale = scale; for (count = 0; count < MAXSHEEP; count++) { StackSheep.Push(new sheeple()); } for (count = 0; count < howManyUpDown1.Value; count++) { Qsheep.Add(new Queue <sheeple>()); } _running = true;//enable run bool //create and start a Thread, passing the thread its processing Queue from our list //(it doesn't need to see any other Queues) foreach (Queue <sheeple> item in Qsheep) { Thread newTH = new Thread(() => QProcess(item)); newTH.IsBackground = true; newTH.Start(); } }
static void Background() { Console.WriteLine("Resource Picture"); Bitmap bm = new Bitmap(Properties.Resources.jupiter); CDrawer dr = new CDrawer(bm.Width, bm.Height); dr.ContinuousUpdate = false; dr.SetBBPixel(bm.Width / 2, bm.Height / 2, Color.Wheat); for (int y = 0; y < bm.Height; ++y) { for (int x = 0; x < bm.Width; ++x) { dr.SetBBPixel(x, y, bm.GetPixel(x, y)); } } dr.Render(); System.Threading.Thread.Sleep(1000); dr.Close(); }
private void UI_Sim_Butt_Click(object sender, EventArgs e) { mySheeple.Clear(); queue.Clear(); for (int i = 0; i < UI_NumUpDown.Value; i++) { queue.Add(new Queue <Sheeple>()); } if (canvas != null) { canvas.Close(); } canvas = new CDrawer(800, 20 * (int)UI_NumUpDown.Value); canvas.Scale = 20; totalNum = 0; tickNum = 0; for (int i = 0; i < 200; i++) { mySheeple.Push(new Sheeple()); } }
/*If the Load button is clicked then, the maze is loaded on the * screen*/ private void buttonLoadMaze_Click(object sender, EventArgs e) { Bitmap bm = new Bitmap(500, 600); //Create a new OpenFileDialog(not on the form * ) then setting : System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); // the initial directory to the current directory and up 2 levels ( ..\ ) string directory = Path.GetFullPath(Path.Combine(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString(), @"..\..\")); ofd.InitialDirectory = directory; //set the title bar to “Load Bitmap maze to solve” this.Text = "Load Bitmap maze to solve"; // filter to show two groups, Bitmaps, and All Files ofd.Filter = "BMP|*.bmp"; ofd.Filter = "BMP|*.bmp|" + "All Files|*.bmp"; if (ofd.ShowDialog() == DialogResult.OK) { StreamReader sr = null; //perform error checking in opening the picture file try { //read from the file sr = new StreamReader(ofd.FileName); //Utilizing the Bitmap load file snippet in the CDrawer manual, //attempt to load the bitmap bm = new Bitmap(ofd.FileName); /* include 2 catches, one for a load failure ( as illustrated in the bitmap load snippet ) * //and an additional generic Exception catch for your width/height constraint, * //both should display a Message box indicating the specified error * */ //If the bitmap width exceeds 190 or height exceeds 100 throw a new Exception() //with the message “Bitmap size exceeds displayable area” ( caught later ) /* if (bm.Height > 100 || bm.Width > 190) * { * throw new System.ArgumentException("Bitmap size exceeds displayable area", "(caught later)"); * MessageBox.Show("Bitmap size exceeds displayable area", "Error !", MessageBoxButtons.OK); * } */ //set the member maximum column and row to their respective //bitmap width and height values MazeInfo_struct.height = bm.Height; MazeInfo_struct.width = bm.Width; } //if the operation is not sucessful throw an error catch (Exception message) { MessageBox.Show("Error opening the file: ", "Error !", MessageBoxButtons.OK); } finally { //close the file sr.Close(); } //If your current Drawer object is not null, Close() it, we are done, //and need a new one for the new maze. if (canvas != null) { canvas.Close(); } this.Text = ofd.FileName; //call the function to draw the image CreateCanvas(bm, this.Text); } }
/// <summary> /// When the load button is pressed loading the bitmap into the cdrawer and parcing the information that we need to solve the map /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UI_btn_Load_Click(object sender, EventArgs e) { //variable that represents the current scale factor on the maze int mapScaling = 10; //initalizing a file dialog to hand OpenFileDialog openFileDialog1 = new OpenFileDialog(); //customizing the filters in the dialog to only show .bmp or all files openFileDialog1.Filter = "All files(*.*)|*.*|bmp files (*.bmp)|*.bmp"; //setting the inital directory openFileDialog1.InitialDirectory = Path.GetFullPath(Environment.CurrentDirectory + @"\..\.."); //opening the file dialog and if the ok button is pressed if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { if (_canvas != null)//trying to close a canvas if the canvas is not null { _canvas.Close(); } //importing the new bitmap from the file dialog Bitmap bm = new Bitmap(openFileDialog1.FileName); //if the bit map is a medium sized bit map setting the scaling appropriately if (bm.Width > 150 || bm.Height > 100) { //throw new Exception("Bitmap Exceeds Size Requirements"); mapScaling = 5; } //if the bit map is larger reducing the scaling to make it fit the screen if (bm.Width > 200 || bm.Height > 150) { mapScaling = 2; } //creating a new enum array set to the width and height of the bitmap mazeArray = new state[bm.Width, bm.Height]; //creating a new canvas set to the height and width of the bitmap scaled with continuous update off, pixel scale set to the scaling and a background of white _canvas = new CDrawer(bm.Width * mapScaling, bm.Height * mapScaling, false, false) { Scale = mapScaling, BBColour = Color.White }; //moving the canvas to be right next to the form _canvas.Position = new Point(Location.X + Width, Location.Y); //moving through the bit maps x and y coords for (int y = 0; y < bm.Height; ++y) { for (int x = 0; x < bm.Width; ++x) { //getting the color of the current pixel _canvas.SetBBScaledPixel(x, y, bm.GetPixel(x, y)); if (bm.GetPixel(x, y) == Color.FromArgb(0, 0, 0))//if its black setting it to a wall state in the enum array { mazeArray[x, y] = state.wall; } if (bm.GetPixel(x, y) == Color.FromArgb(255, 255, 255))//if its white setting it to open state in the array { mazeArray[x, y] = state.open; } if (bm.GetPixel(x, y) == Color.FromArgb(255, 0, 0))//if its red setting that pixel as the maze exit { _maze._mEnd = new Point(x, y); } if (bm.GetPixel(x, y) == Color.FromArgb(0, 255, 0))//if its green setting that pixel as the maze entrance { _maze._mStart = new Point(x, y); } } } //rendering the canvas _canvas.Render(); //setting the name of the form to the file name Text = Path.GetFileName(openFileDialog1.FileName); //displaying that the file has been loaded to the user with the dimensions UI_LB_MessageViewer.Items.Insert(0, $"Loaded: {Path.GetFileName(openFileDialog1.FileName)} file Dimensions {bm.Width} X {bm.Height}"); //saving the file name for later use _file = Path.GetFileName(openFileDialog1.FileName).ToString(); //initialing the maze info struct _maze._xWidth = bm.Width; //setting the maze width _maze._yHeight = bm.Height; //setting the maze height _maze._cSol = UI_BTN_SColor.BackColor; //setting the color of the solution path _maze._cDed = UI_btn_DColor.BackColor; //setting the color of the visited path _maze._steps = 0; //initializing the step counter _maze._solved = false; //setting solved to false } catch (Exception ex) { MessageBox.Show(ex.Message); //catching exceptions during the loading process } UI_btn_Solve.Enabled = true; //enabling the solve button } }
static void Main(string[] args) { //variables string choice = ""; CDrawer sky = new CDrawer(1, 1); int height = 0; int width = 0; bool[,] stars = new bool[0, 0]; int numberOfStars = 0; //title Console.WriteLine("\t\tStarry Night\n"); sky.Close(); //inputs do { Console.Write("Please choose one of the following:\nM - Make a New Sky\nA - Add Stars\nR - Remove All Stars\nD - Draw Sky\nX - Exit\nYour Choice: "); choice = Console.ReadLine().ToUpper(); switch (choice) { case ("M"): sky = MakeSky(); height = sky.m_ciHeight; width = sky.m_ciWidth; break; case ("A"): if (width * height == 0) { Console.Clear(); Console.WriteLine("\t\tYou haven't drawn a sky yet, do that first please\n"); } else { stars = MakeStars(width, height); numberOfStars = AddStars(stars, (width * height)); } break; case ("R"): RemoveStars(stars); break; case ("D"): if (width * height == 0) { Console.Clear(); Console.WriteLine("\t\tYou haven't drawn a sky yet, do that first please\n"); } else { DrawSky(sky, stars); } break; case ("X"): continue; default: Console.Clear(); Console.WriteLine("You have entered an invalid choice, try again\n"); break; } } while (choice != "X"); }
//occurs evertime a key is pressed private void Pressed_KeyDown(object sender, KeyEventArgs e) { // if x is pressed and released program closes if (e.KeyCode == Keys.X) { this.Close(); _canvas.Close(); } // if up draw shpe 1 posistion up if (e.KeyCode == Keys.Up) { _YPOS -= _shapePos; drawStuff(); range(); } // if down draw shpe 1 posistion down else if (e.KeyCode == Keys.Down) { _YPOS += _shapePos; drawStuff(); range(); } // if left draw shpe 1 posistion left else if (e.KeyCode == Keys.Left) { _XPOS -= _shapePos; drawStuff(); range(); } // if right draw shpe 1 posistion right else if (e.KeyCode == Keys.Right) { _XPOS += _shapePos; drawStuff(); range(); } // move in arrow diretion 2 posistions if (e.Shift) { // 2 pos inted of 1 _shapePos = 2; } // move in arrow diretion 3 posistions if (e.Control) { // 3 pos insted of 1 _shapePos = 3; } // move in arrow diretion 4 posistions if (e.Alt) { // 4 pos insted of 1 _shapePos = 4; } // if R is pressed set color to red if (e.KeyCode == Keys.R) { _color = Color.Red; } // if G is pressed set color to green if (e.KeyCode == Keys.G) { _color = Color.Green; } // if B is pressed set color to blue if (e.KeyCode == Keys.B) { _color = Color.Blue; } // draw border around shape if (e.KeyCode == Keys.F1) { _border = 1; } // reset to original shape distance if (e.KeyCode == Keys.Z) { _shapePos = 2; } }
//Occurs everytime the 'Load' button is pressed. Attempts to open a bitmap file onto the canvas //Side-Effect: sets the maze's information while scanning the bitmap private void BtnLoad_Click(object sender, EventArgs e) { int x, y; //position of the pixels on bitmap bool good = true; //used to see if the file we are trying to open is valid OpenFileDialog openFile = new OpenFileDialog(); string path = Path.GetFullPath(Environment.CurrentDirectory + @"\..\..\mazes"); //initial settings for the open file dialog openFile.InitialDirectory = path; openFile.Title = "Load Bitmap maze to solve"; openFile.Filter = "Bitmaps (*.bmp)|*.bmp|All files (*.*)|*.*"; Bitmap map = null; _mazeDets = new MazeInfo { livePath = Color.Orange, deadPath = Color.Gray }; //checked to see if the file was able to open try { //user chooses a file to open if (openFile.ShowDialog() == DialogResult.OK) { //loads the bitmap file map = new Bitmap(openFile.FileName); //checks the valid size(x, y) of the bitmap file try {//sets the scale as an attempt to fit the bitmap onto the canvas if (map.Height * map.Width < 10000) { _scale = 10; } else if (map.Height * map.Width < 20000) { _scale = 8; } else if (map.Height * map.Width < 150000) { _scale = 3; } else if (map.Height * map.Width < 450000) { _scale = 2; } else if (map.Height * map.Width < 1260000) { _scale = 1; } else { throw new Exception(); } } //error message for the bitmap file that is too big catch (Exception) { MessageBox.Show("Bitmap size exceeds displayable area", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); good = false; } } //user decided to close the open file dialog else { good = false; } } //error message when the file chosen could not be opened catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); good = false; } if (good) { //creates a 2D array of enumeration to set the state of the pixel (open, wall, visited) _state = new Wall[map.Height + 1, map.Width + 1]; _mazeDets.mazeHeight = map.Height; _mazeDets.mazeWidth = map.Width; if (_canvas != null) { _canvas.Close(); } //new canvas with appropriately adjusted size _canvas = new CDrawer(map.Width * _scale, map.Height * _scale, false) { Scale = _scale, BBColour = Color.White }; //loads the scaled pixels from bitmap onto CDrawer canvas //depending on the colour of the pixel, decide the state and put it in 2D Enum array for (y = 0; y < map.Height; y++) { for (x = 0; x < map.Width; x++) { //black pixel found? It's a wall if (map.GetPixel(x, y) == Color.FromArgb(255, 0, 0, 0)) { _state[y, x] = Wall.wall; } //white pixel found? It's an open path else if (map.GetPixel(x, y) == Color.FromArgb(255, 255, 255)) { _state[y, x] = Wall.open; } //finds position of start of the maze (green pixel) if (map.GetPixel(x, y) == Color.FromArgb(0, 255, 0)) { _mazeDets.startPos.X = x; _mazeDets.startPos.Y = y; } //finds the position of end of the maze (red pixel) if (map.GetPixel(x, y) == Color.FromArgb(255, 0, 0)) { _mazeDets.endPos.X = x; _mazeDets.endPos.Y = y; } _canvas.SetBBScaledPixel(x, y, map.GetPixel(x, y)); } } //sets the postion of the canvas so it loads right next to the form _canvas.Position = new Point(Location.X + Size.Width + 10, Location.Y); _canvas.Render(); btnSolve.Enabled = true; listMazeLog.Items.Insert(0, $"Loaded: {Path.GetFileName(openFile.FileName)}"); } }
/*********************************** * Author: Angelo M Sanches * simple wrap alga. keeps it in bounds set by min / max *************************************/ // this is a form closing to make sure that the other form closes too since interceped normaly and all; private void Form1_FormClosing(object sender, FormClosingEventArgs e) { SpeedSelector.CanClose = true; SpeedSelector.Close(); Box.Close(); }
static void ClickEllipses() { Random rnd = new Random(); CDrawer can = new CDrawer(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Reset(); watch.Start(); can.AddText("Random Bounding Box Ellipses : 2s", 28, 0, 0, can.ScaledWidth, can.ScaledHeight, Color.White); can.AddText("Random Bounding Box Ellipses : 2s", 28, 2, 2, can.ScaledWidth + 2, can.ScaledHeight + 2, Color.Black); while (watch.ElapsedMilliseconds < 5000) { Point p = new Point(rnd.Next(-50, can.ScaledWidth + 50), rnd.Next(-50, can.ScaledHeight - 50)); switch (rnd.Next(6)) { case 0: can.AddEllipse(p.X, p.Y, 100, 100); break; case 1: can.AddEllipse(p.X, p.Y, 100, 100, RandColor.GetKnownColor(), rnd.Next(1, 4), RandColor.GetKnownColor()); break; case 2: can.AddPolygon(p.X, p.Y, 100, rnd.Next(3, 8)); break; case 3: can.AddPolygon(p.X, p.Y, 100, rnd.Next(3, 8), rnd.NextDouble() * Math.PI, RandColor.GetKnownColor(), 2, RandColor.GetKnownColor()); break; case 4: can.AddRectangle(p.X, p.Y, 100, 100); break; case 5: can.AddRectangle(p.X, p.Y, 100, 100, RandColor.GetKnownColor(), rnd.Next(1, 4), RandColor.GetKnownColor()); break; default: break; } System.Threading.Thread.Sleep(100); } can.Close(); can = new CDrawer(1000, 400, false); //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Reset(); watch.Start(); can.AddText("Random Bounding Box Ellipses : 2s", 28, 0, 0, can.ScaledWidth, can.ScaledHeight, Color.White); can.AddText("Random Bounding Box Ellipses : 2s", 28, 2, 2, can.ScaledWidth + 2, can.ScaledHeight + 2, Color.Black); while (watch.ElapsedMilliseconds < 2000) { Point p = new Point(rnd.Next(50, can.ScaledWidth - 50), rnd.Next(50, can.ScaledHeight - 50)); can.AddCenteredEllipse(p.X, p.Y, 100, 100, RandColor.GetKnownColor(), 2, Color.White); can.AddCenteredEllipse(p.X, p.Y, 5, 5, RandColor.GetKnownColor(), 1, Color.Red); System.Threading.Thread.Sleep(100); } can.Render(); System.Threading.Thread.Sleep(1000); can.Close(); }
static void Main(string[] args) { string choice = ""; //input from user on what action they'd like to take int[] grades = new int[0]; //the array that will hold all the randomly generated grades CDrawer gdi; //the drawing window for displaying the histogram bool error = false; //used to confirm if an array has been created or not //repeats program and menu options until letter q is pressed do { //title Console.WriteLine("\t\tLab4 - Array of Marks\n"); Console.WriteLine("Actions available...\n"); //options and input from user Console.Write("1. Create random array.\n2. Array stats.\n3. Draw histogram.\n4. Save array to file."); Console.Write("\n5. Load array from file.\nq. Exit the program.\n\nYour selection: "); choice = Console.ReadLine().ToLower(); switch (choice) { //creates array with as large as the user desires, fills with random grade values then displays it case ("1"): grades = GetArray(); DisplayArray(grades); Refresh(); break; //sorts array then displays some simple stats about it case ("2"): if (error = ArrayCheck(grades)) { break; } Array.Sort(grades); DisplayArray(grades); Console.WriteLine("\nThe minimum value is {0}", grades.Min()); Console.WriteLine("The average value is {0}", grades.Average()); Console.WriteLine("The maximum value is {0}", grades.Max()); Refresh(); break; //creates histogram based on the array case ("3"): if (error = ArrayCheck(grades)) { break; } gdi = new CDrawer(800, 600, false); DrawHistogram(gdi, grades); Refresh(); gdi.Close(); break; //stores the array as a file case ("4"): if (error = ArrayCheck(grades)) { break; } SaveFile(grades); Refresh(); break; //loads a file, designed only to work with the file save formatting of the SafeFile() method case ("5"): LoadFile(); Refresh(); break; case ("q"): continue; default: Console.Clear(); Console.WriteLine("\tYou have entered an invalid option\n"); break; } } while (choice != "q"); }