Using C# for Graphics and GUIs Handout #2



Similar documents
Lab 7 Keyboard Event Handling Mouse Event Handling

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Petrel TIPS&TRICKS from SCM

Introduction to Data Structures

Visual Basic 2010 Essentials

DIA Creating Charts and Diagrams

Designing a Graphical User Interface

Visual Basic Programming. An Introduction

Answers to Review Questions Chapter 7

Systems Programming & Scripting

Getting Started on the Computer With Mouseaerobics! Windows XP

paragraph(s). The bottom mark is for all following lines in that paragraph. The rectangle below the marks moves both marks at the same time.

Lesson 10: Video-Out Interface

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

App Inventor Drum Machine Instructions (Project #1) (Version 2 of App Inventor) Description:

LabVIEW Day 6: Saving Files and Making Sub vis

Lecture 22: C Programming 4 Embedded Systems

Passing 1D arrays to functions.

Windows Presentation Foundation (WPF) User Interfaces

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Sources: On the Web: Slides will be available on:

First Bytes Programming Lab 2

Smartboard and Notebook 10 What s New

Using Loops and Timers

The VB development environment

To Begin Customize Office

We are going to investigate what happens when we draw the three angle bisectors of a triangle using Geometer s Sketchpad.

Introduction to Microsoft PowerPoint

Using WINK to create custom animated tutorials

What is Microsoft PowerPoint?

CS106A, Stanford Handout #38. Strings and Chars

Part 1 Foundations of object orientation

Motion tween is nothing but tweening a Symbol's movement from one position to another.

Appendix K Introduction to Microsoft Visual C++ 6.0

Canterbury Maps Quick Start - Drawing and Printing Tools

Petrel TIPS&TRICKS from SCM

Task Card #2 SMART Board: Notebook

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

a) What is the major difference between a program that runs under a virtual machine vs. one that does not?

Introduction to Java

Instructions for Creating Silly Survey Database

An Incomplete C++ Primer. University of Wyoming MA 5310

Fireworks CS4 Tutorial Part 1: Intro

Wincopy Screen Capture

Computer Science 217

Basic Website Creation. General Information about Websites

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

A Step by Step Guide for Building an Ozeki VoIP SIP Softphone

Interactive Data Visualization for the Web Scott Murray

When To Work Scheduling Program

Common Beginner C++ Programming Mistakes

Unit 21 - Creating a Button in Macromedia Flash

m ac romed ia Fl a s h Curriculum Guide

Macros in Word & Excel

C++ INTERVIEW QUESTIONS

PIC 10A. Lecture 7: Graphics II and intro to the if statement

HOW TO CREATE A SCANNED DIGITAL SIGNATURE AND INSERT INTO A PDF DOCUMENT

Python Lists and Loops

Input and Interaction

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

MATH 140 Lab 4: Probability and the Standard Normal Distribution

Action settings and interactivity

Beginner s Matlab Tutorial

Microsoft Word 2010 Tutorial

Introduction to the Visual Studio.NET IDE

Final Examination Semester 2 / Year 2011

How to Create a Fun Circus Tent Icon. Final Image Preview. Tutorial Details. Step 1. By: Andrei Marius

Opening, Importing and Saving Images

Komumaldata Barcoded PDF Forms PDF417 Barcode Generator Reference XFA (LiveCycle) Version Generator Version: 1.12

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Microsoft Word 2010 Basics

Create a Poster Using Publisher

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

INFOCOMMUNICATIONS JOURNAL Mojette Transform Software Hardware Implementations and its Applications

Introduction to ROOT and data analysis

Using Excel to find Perimeter, Area & Volume

Excel -- Creating Charts

Using SQL Server Management Studio

Hands-On Practice. Using Notebook Software in the Office

Create Charts in Excel

Aras Corporation Aras Corporation. All rights reserved. Notice of Rights. Notice of Liability

8. MACROS, Modules, and Mouse

Epson Brightlink Interactive Board and Pen Training. Step One: Install the Brightlink Easy Interactive Driver

Updox, LLC

arrays C Programming Language - Arrays

5 Airport. Chapter 5: Airport 49. Right-click on Data Connections, then select Add Connection.

Get post program 7 W.a.p. that Enter two numbers from user and do arithmetic operation

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Using Microsoft Project 2000

public static void main(string[] args) { System.out.println("hello, world"); } }

A Quick Start Guide to Using PowerPoint For Image-based Presentations

LESSON 7: IMPORTING AND VECTORIZING A BITMAP IMAGE

Microsoft PowerPoint 2010 Computer Jeopardy Tutorial

Installing An ActiveX Control in InTouch

Excel Project From the Start menu select New Office Document. If necessary, click the General tab and then double-click Blank workbook.

A Guide to Microsoft Paint (Windows XP)

AP Computer Science Static Methods, Strings, User Input

Transcription:

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