Using C# for Graphics and GUIs Handout #2 Learning Objectives: C# Arrays Global Variables Your own methods Random Numbers Working with Strings Drawing Rectangles, Ellipses, and Lines Start up Visual Studio now so it has time to load. 1
C# Arrays Arrays in C# are somewhat like Vectors in C++. You can size them with variables and they have functions associated with them. Declaring Arrays: 1-D integer array declaration: int[ ] x = new int[5]; 2-D Rectangular integer array declaration: int[, ] y = new int[5, 4]; Using 1-D arrays = same as C++, e.g. x[2]=3; Using 2-D rectangular arrays: y[1, 3] = 4; Use x.length instead of x.size() For 2-D: y.length gives number of rows 2 y.longlength gives total number of elements
Resizing 1-D C# Arrays Like a vector, you don t need to size the array initially. To size it later, use Array.Resize() Example: int[ ] x; Array.Resize(ref x, 25); //this resizes the array to 25 The ref is like the & for a pointer ref x gives the memory address of x 3
Global Variables in Forms Remember that most variables are only available to the method in which they are declared. These are called Local Variables. Global Variables are variables that are available throughout the program. For C# forms: Declare global variables in the same area as all of the methods, but outside of all of the methods. These variables can be of standard type such as double or int, or any types available with the form such as Bitmap or Color. If they need to be initialized when the program starts, initialize them under the line: InitializeComponent(); 4
Your Own Methods For C# forms: Your methods will look just like functions in C à the only difference is that you don t need a function prototype. Define your methods in the same area as all of the pre-defined (like button1_click()) methods. If they need to be called when the program starts, call them under the line: InitializeComponent(); 5
Random Numbers in C# Just one of these: (declares a random number generator object) Random r = new Random( ); (You can make your Random variable global) To get a random integer from 0 to 5: i = r.next(6); //this is just like i = rand()%6 To get another random integer from 0 to 9: i = r.next(10) 6
Working With Strings To declare a String variable, use: String <name> Example: String s = textbox1.text; //declares a String called s and sets its value //to whatever is in textbox1 One way to append Strings together, use the + operator: Examples: s = textbox1.text + s = Hello, + s; + textbox2.text; To get the length of a String, use the.length property Example: for(int i = 0; i < s.length; i++) { } //code to do something with each character would go here 7
Working With Strings To get a portion of a String, use: Substring(start index, length of substring): Example: String s = textbox1.text; label1.text = s.substring(2, 7); //this will display the 3 rd to 9 th characters //in label1 Another Example: String s = textbox1.text; label1.text = s.substring(2); //this will display the 3 rd char through the end To change a String to upper case, use the ToUpper() method Example: s = s.toupper(); //this will make the whole string uppercase. How could you loop through each character in a String? Note: C++ has a string class which is similar to String. To use it, you 8 need to add #include <string>
Drawing Rectangles Note: This is available because at the top we have: using namespace System.Drawing 1. Create an object of the Graphics class to tell where you want your graphics: Graphics g=this.creategraphics(); //do this only once //g is the object name, this refers to the form itself //if you wanted graphics in a picturebox (you d use something //like: Graphics gp = picturebox1.creategraphics() 2. To fill in objects with color, first declare a SolidBrush: SolidBrush sbblue = new SolidBrush(Color.Blue); 3. To create a filled rectangle on g (the form s graphics object): g.fillrectangle(sbblue,0,0,100,75) //creates a blue rectangle that // starts at pixel 0,0 and is 100 pixels wide and 75 pixels high. Try it. Draw a rectangle when you click a button. If you have time, draw two rectangles when you hit the button. 9
Drawing Ellipses and Lines Ellipses: Same as FillRectangle, except use FillEllipse. The position x and y values represent the upper left of a rectangle that would just fit around the ellipse. Lines: The following code draws a line that is blue and 4 pixels wide from pixel (100,150) to pixel (300,0): Graphics gr=this.creategraphics(); Pen p = new Pen(Color.Blue, 4); gr.drawline(p,100,150,300,0); Note: You use a Pen instead of a SolidBrush. You can also draw rectangles and ellipses that aren t filled in, and you would use a Pen with these also: gr.drawrectangle(p,100,150,40,60); gr.drawellipse(p, 100,150,40,60); Try drawing a circle that s not filled in. 10