Course 2DCis: 2D-Computer Graphics with C# Chapter C5: Comments to the Controls Project

Similar documents
Code Samples: 3D Digital Straight Line in C#

Course 2DCis: 2D-Computer Graphics with C# Chapter C1: Comments to the Intro Project

Course 3D_MDX: 3D-Graphics with Managed DirectX 9.0 Chapter 6: Mesh Viewer

Course 3D_MDX: 3D-Graphics with Managed DirectX 9.0Chapter C5: Comments to the "Mesh Viewer" Project

Course IPCis: Image Processing with C# Chapter C2: Commented Code of the Histogram Project

Course 2DCis: 2D-Computer Graphics with C# Chapter C1: The Intro Project

Class Test 5. Create a simple paint program that conforms to the following requirements.

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

Chapter 13. Graphics, Animation, Sound and Drag-and-Drop The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Tutorial 5 Completing the Inventory Application Introducing Programming

Chapter 13. Custom Events. Learning Objectives

2IS45 Programming

IBSDK Quick Start Tutorial for C# 2010

Create a memory DC for double buffering

Chapter 7 Applets. Answers

Instructions for Crossword Assignment CS130

This is the empty form we will be working with in this game. Look under the properties window and find the following and change them.

Click on the empty form and apply the following options to the properties Windows.

Visual Applications Graphics Lecture Nine. Graphics

Course 2D_WPF: 2D-Computer Graphics with C# + WPF Chapter C1a: The Intro Project Written in XAML and C#

Visual Studio.NET.NET Framework. Web Services Web Forms Windows Forms. Data and XML classes. Framework Base Classes. Common Language Runtime

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Visual C# Program: Simple Game 3

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects

The Microsoft.NET Framework

Form Properties Window

Exercise 6. Controlling of a bipolar stepper motor with the help of eprodas SCI module and program language Visual Basic

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

10Tec igrid for.net 6.0 What's New in the Release

c.def (pronounced SEE-def) Language Reference Manual

Chapter 13. Graphics, Animation, Sound, and Drag-and-Drop. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

1. Windows Forms 2. Event-Handling Model 3. Basic Event Handling 4. Control Properties and Layout 5. Labels, TextBoxes and Buttons 6.

More Language Features and Windows Forms

More Language Features and Windows Forms. Part I. Some Language Features. Inheritance. Inheritance. Inheritance. Inheritance.

Getting Started with Milestone 2. From Lat Lon, to Cartesian, and back again

Creating a nice GUI. OpenGL and Windows. Note: VS 2003 shown. Create a new Project

Building Java Programs

Topic 7: Algebraic Data Types

Radius= 10 cm, Color= Red, Weight= 200g, X= 3m, Y= 5m, Z= 2m. Radius= 10 cm, Color= Blue, Weight= 200g, X= 3m, Y= 5m, Z= 0m

Programming: You will have 6 files all need to be located in the dir. named PA4:

Tutorial 3 - Welcome Application

SFPL Reference Manual

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Start Visual Studio, start a new Windows Form project under the C# language, name the project BalloonPop MooICT and click OK.

OhioState::OpenGLPanel. OpenGL and Windows. Public methods. OpenGLPanel : Forms::Control

Chapter 3 - Introduction to Java Applets

Java Programming Hello FX

The HOME Tab: Cut Copy Vertical Alignments

Ingegneria del Software T. Interfaccia utente

Mr.Khaled Anwar ( )

Unit 3. Lesson Designing User Interface-2. TreeView Control. TreeView Contol

CST242 Windows Forms with C# Page 1

CISC 1600, Lab 2.1: Processing

Programming in C# Project 1:

Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

CISC 1600 Lecture 3.1 Introduction to Processing

UNIT-3. Prepared by R.VINODINI 1

Events. Event Handler Arguments 12/12/2017. EEE-425 Programming Languages (2016) 1

EL-USB-RT API Guide V1.0

Smoother Graphics Taking Control of Painting the Screen

Instructor s Notes Programming Logic Printing Reports. Programming Logic. Printing Custom Reports

Painting your window

Introduction. Create a New Project. Create the Main Form. Assignment 1 Lights Out! in C# GUI Programming 10 points

Learning VB.Net. Tutorial 19 Classes and Inheritance

XNA 4.0 RPG Tutorials. Part 2. More Core Game Components

Object oriented lab /second year / review/lecturer: yasmin maki

INFORMATICS LABORATORY WORK #4

ACS-1805 Introduction to Programming (with App Inventor)

Garfield AP CS. Graphics

The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at:

PixelSurface a dynamic world of pixels for Unity

Today we spend some time in OO Programming (Object Oriented). Hope you did already work with the first Starter and the box at:

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

MS Office Word Tabs & Tables Manual. Catraining.co.uk Tel:

CISC 1600, Lab 3.1: Processing

You can call the project anything you like I will be calling this one project slide show.

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C1: The Intro Project

Inheritance, and Polymorphism.

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Start Visual Studio, create a new project called Helicopter Game and press OK

Yes, this is still a listbox!

Full file at

Building Java Programs

IN CHAPTER 9 we delved into advanced 2D graphics programming. In

PYTHON NOTES (drawing.py and drawstuff.py)

The Newsletter will contain a Title for the newsletter, a regular border, columns, Page numbers, Header and Footer and two images.

TWO-DIMENSIONAL FIGURES

Printing in JADE. Introduction JADE 6. JADE Development Centre

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Menus and Printing. Menus. A focal point of most Windows applications

XNA 4.0 RPG Tutorials. Part 3. Even More Core Game Components

Main Game Code. //ok honestly im not sure, if i guess its a class ment for this page called methodtimer that //either uses the timer or set to timer..

C4.3, 4 Lab: Conditionals - Select Statement and Additional Input Controls Solutions

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Composite Pattern Diagram. Explanation. JavaFX Subclass Hierarchy, cont. JavaFX: Node. JavaFX Layout Classes. Top-Level Containers 10/12/2018

To enter the number in decimals Label 1 To show total. Text:...

Example: Building a Java GUI

Example: Building a Java GUI

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Solution Notes. COMP 151: Terms Test

Transcription:

1 Course 2DCis: 2D-Computer Graphics with C# Chapter C5: Comments to the Controls Project Copyright by V. Miszalok, last update: 04-01-2006 using namespaces using System; //Namespace of the base class of all classes "System.Object" and of all primitive data types such as Int32, Int16, Single, Double, String. using System.Drawing; //Namespace of the "Graphics" class and its drawing methods such as DrawString, DrawLine, DrawRectangle, FillClosedCurve etc. using System.Windows.Forms; //Namespace of the "Form" class (base class of our main window Form1) and its method Application.Run. Global variables of public class Form1 : Form All objects that are used inside the function OnTimer(...) (see below) must be declared "static", because the function OnTimer(...) is static. There is only one Timer object for all instances of Form1. No matter how often You start Form1, any instance uses the same Timer. Any"static" object resides only once inside its class and not inside each instance of its class as nonstatic objects do. See the help text in Visual Studio.NET: Help -> Index -> Filtered by:.net Framework SDK -> Look for: static keyword, C#. static void Main() { Application.Run( new Form1() ); } //Create an instance of Form1 and ask the operating system to start it as main window of our program. const Int32 nbuttons = 3, ntrackbars = 3, ncheckboxes = 3, nradiobuttons = 3; //Integers that never change their values indicating the numbers of Buttons, TrackBars etc. Button [] button = new Button[nButtons]; //Array of 3 Buttons: Start, Stop, Clear. TrackBar[] trackbar = new TrackBar[nTrackBars]; //Array of 3 TrackBars. Label [] label = new Label[nTrackBars]; //Array of 3 Labels aimed at lettering the 3 TrackBars: TimerInterval, PenThickness, Brightness. CheckBox[] checkbox = new CheckBox[nCheckBoxes]; //Array of 3 CheckBoxes: Red, Green, Blue. static RadioButton[] radiobutton = new RadioButton[nRadioButtons]; //static Array of 3 RadioButtons: Lines, Rectangles, Ellipses. This array must be static because it will be accessed by the static event handler OnTimer(...). static Color linecolor = Color.Black; //Initial drawing color. static Pen pen = new Pen( linecolor, 1 ); //Initial Pen. static Panel panel = new Panel(); //Drawing area covering the complete window space right of the vertical control bar. static Graphics g; //Device context that all drawing commands need. static Random r = new Random(); //Generator for random x,y-coordiantes of lines, rectangles and ellipses. Timer mytimer = new Timer(); //This Timer sends messages at fixed time intervals to Form1, that trigger Form1 to execute its OnTimer(..) method.

Constructor public Form1() 2 BackColor = Color.White; //Background color of Form1 and of panel. Text = "GUI" //Title in the blue header line of Form1. Int32 i; //for-loop counter. for ( i=0; i < nbuttons; i++ ) //3 buttons for Form1. button[i] = new Button(); Controls.Add( button[i] ); //Create a button and affiliate it to Form1. button[i].click += new EventHandler( button_handler ); //Redirect all click events to our button_handler-function. button[0].text = "Start"; //Inscription of button 0; button[1].text = "Stop"; //Inscription of button 1; button[2].text = "Clear"; //Inscription of button 2; for ( i=0; i < ntrackbars; i++ ) //3 track bars for Form1. trackbar[i] = new TrackBar(); Controls.Add( trackbar[i] ); //Create a track bar and affiliate it to Form1. label [i] = new Label(); Controls.Add( label[i] ); //Create a label and affiliate it to Form1. trackbar[i].autosize = false; //A value indicating whether the height or width of the track bar is being automatically sized. trackbar[i].tickstyle = TickStyle.None; //A value indicating how to display the tick marks on the track bar. trackbar[i].valuechanged += new EventHandler( trackbar_handler ); //Redirect all ValueChanged events to our trackbar_handler-function. label [i].textalign = ContentAlignment.TopCenter; //Text is vertically aligned at the top, and horizontally aligned at the center. trackbar[0].name = label[0].text = "TimerInterval" //Inscription of label 0 underneath track bar 0. trackbar[1].name = label[1].text = "PenThickness" //Inscription of label 1 underneath track bar 1. trackbar[2].name = label[2].text = "Brightness" //Inscription of label 2 underneath track bar 2. trackbar[0].minimum = 13; trackbar[0].maximum = 1000; //Lower and upper delimiter of "TimerInterval". trackbar[1].minimum = 1; trackbar[1].maximum = 20; //Lower and upper delimiter of "PenThickness". trackbar[2].minimum = 0; trackbar[2].maximum = 255; //Lower and upper delimiter of "Brightness". for ( i=0; i < ncheckboxes; i++ ) //3 check boxes for Form1. checkbox[i] = new CheckBox(); Controls.Add( checkbox[i] ); //Create a check box and affiliate it to Form1.

checkbox[i].textalign = ContentAlignment.MiddleCenter; //Text is vertically aligned at the middle, and horizontally aligned at the center. 3 checkbox[i].click += new EventHandler( checkbox_handler ); //Redirect all click events to our checkbox_handler-function. checkbox[0].text = "Red"; //Inscription of check box 0 checkbox[1].text = "Green"; //Inscription of check box 1 checkbox[2].text = "Blue"; //Inscription of check box 2 for ( i=0; i < nradiobuttons; i++ ) //3 radio buttons for Form1. radiobutton[i] = new RadioButton(); Controls.Add( radiobutton[i] ); //Create a radio button and affiliate it to Form1. radiobutton[i].textalign = ContentAlignment.MiddleCenter; //Text is vertically aligned at the middle, and horizontally aligned at the center. radiobutton[0].text = "Lines"; radiobutton[0].checked = true; //Inscription of radio button 0. Button is pressed. radiobutton[1].text = "Rectangles"; //Inscription of radio button 1. radiobutton[2].text = "Ellipses"; //Inscription of radio button 2. foreach ( Control c in Controls ) //loop around all different controls. c.backcolor = Color.Gray; //All controls are painted in gray. if ( c.text == "" ) c.text = "nothing"; //All controls without inscription obtain inscription "nothing". Controls.Add( panel ); //Put a drawing canvas on Form1 which will cover the empty client area. Its better to use such a canvas instead of drawing directly onto the client area of Form1, because this canvas can be shifted on Form1 but holds its own relative coordinates always starting at (0/0). mytimer.tick += new EventHandler( OnTimer ); //Redirects the Timer messages to our Form1- method OnTimer(...). mytimer.interval = 1; //Send the Timer messages at the shortest possible time interval (1 millisecond means as fast as possible). Width = 800; //Starting width of Form1. This statement is not obligatory but the default width is rather narrow for drawing. This statement raises a first OnResize-event. Height = 600; //Starting height of Form1. This statement is not obligatory but the default height is rather narrow for drawing. This statement raises a second OnResize-event which overrides the first one.

4 Overridden event handler protected override void OnResize( EventArgs e ) Int32 w = ClientRectangle.Width / 5; //Control width = 20% of Form1-width. Int32 h = ClientRectangle.Height / (Controls.Count-1); //Control height = Form1-width divided by the number of controls (without the last one = panel). Int32 i, top = 1; //i = loop counter, top = stepping y-coordinate. for ( i=0; i < Controls.Count-1; i++ ) //loop through all controls without the last one = panel. Controls[i].Top = top; //y-position. Controls[i].Left = 2; //Common x-position of all controls leaving a narrow left margin. Controls[i].Width = w; //Common width of all controls. Controls[i].Height = h - 2; //Common height of all controls leaving a narrow bottom margin. top += h; //Next y-position below the predecessor control. for ( i=0; i < ntrackbars; i++ ) trackbar[i].height = h; //Suppress the narrow bottom margins between the track bars and their labels. panel.location = new Point( w+2, 0 ); //Canvas relative coordinates: x = right of the horizontal control bar leaving a left narrow margin, y = top of <tt<form1 client area. panel.size = new Size( ClientRectangle.Width-panel.Location.X, ClientRectangle.Height ); //Canvas covers all the rest of Form1 client area. g = panel.creategraphics(); //All further Draw-commands will address this canvas. g.clear( SystemColors.Window ); //Clear the canvas with Form1 background color. Event handler protected void button_handler( object sender, System.EventArgs e ) switch( ((Button)sender).Text )// Cast object sender to type Button and read its inscription, which serves as dispenser. case "Start": mytimer.start(); break; //Unlock mytimer. case "Stop" : mytimer.stop(); break; //Lock mytimer. case "Clear": g.clear( SystemColors.Window ); break; //Erase the canvas. Event handler protected void trackbar_handler( object sender, System.EventArgs e ) Int32 value = ((TrackBar)sender).Value; //Cast object sender to type TrackBar and read its current Value. switch( ((TrackBar)sender).Name ) //Cast object sender to type TrackBar and read its Name, which serves as dispenser. case "TimerInterval": //The event comes from trackbar[0]. mytimer.interval = value; //Set new Timer delay. label[0].text = "TimerInterval = " + value.tostring(); break; //Display the new delay in label[0] below trackbar[0].

5 case "PenThickness": //The event comes from trackbar[1]. pen.width = value; //Change the pen.width property. label[1].text = "PenThickness = " + value.tostring(); break; //Display the new thickness in label[1] below trackbar[1]. case "Brightness": //The event comes from trackbar[2]. label[2].text = "Brightness = " + value.tostring(); //Display the new brightness in label[2] below trackbar[2]. checkbox_handler( sender, e ); break; //call checkbox_handler //Call the event handler for color changes. Event handler protected void checkbox_handler( object sender, System.EventArgs e ) Int32 v; //Local variable carrying TrackBar values. if ( sender == trackbar[2] ) v = trackbar[2].value; //Call comes from "Brightness"-trackbar. Take the new brightness from trackbar[2]. else v = trackbar[2].value = 255; //Call comes from a CheckBox. In this case create a full red and/or green and/or blue. linecolor = Color.Black; //Black = default color = start color. if ( checkbox[0].checked ) linecolor = Color.FromArgb( v,0,0 ); //Just new red. if ( checkbox[1].checked ) linecolor = Color.FromArgb( linecolor.r,v,0 ); //Old red plus new green. if ( checkbox[2].checked ) linecolor = Color.FromArgb( linecolor.r,linecolor.g,v ); //Old red plus old green plus new blue. if ( linecolor == Color.Black && v > 0 ) linecolor = Color.FromArgb( v,v,v ); //If linecolor == default color all checkboxes have been empty. But if brightness is > 0, the line color should be some sort of gray. pen.color = linecolor; //Change the pen.color property. Event handler protected static void OnTimer( Object myobject, EventArgs myeventargs ) Int32 w = r.next( panel.width/2 ), h = r.next( panel.height/2 ); //Random width with maximally half the panel-width and random height with maximally half the panel-height. Int32 x = r.next( panel.width-w ), y = r.next( panel.height-h ); //Random x so, that width always fits into panel-width and random y so, that height always fits into panel-height. if ( radiobutton[0].checked ) g.drawline ( pen, x, y, x+w, y+h ); //A random line. else if ( radiobutton[1].checked ) g.drawrectangle( pen, x, y, w, h ); //A random rectangle. else if ( radiobutton[2].checked ) g.drawellipse ( pen, x, y, w, h ); //A random ellipse.