Graphics and Multimedia Part #2

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Chapter 9 Color, Sound and Graphics
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
User Interface Programming in C#: Graphics
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
1 Chapter 26 D&D – Graphics Outline 26.1 Introduction 26.3 Graphics Contexts and Graphics Objects 26.4 Color Control 26.5 Font Control 26.6 Drawing Lines,
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
C# Programming Lecture 4 “GDI+” PGL01/CSP/2006.
BIM211 – Visual Programming Objects, Collections, and Events 1.
PROCESSING & JAVA An Introduction to Computing. Objectives Be able to state and apply phases of development Be able to state and describe the goals of.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
MA/CS 3751 Fall 2002 Lecture 24. MA/CS 3752 ginput ginput is a Matlab function which takes one argument input: number of points to select in the image.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 12: A Display Model 1 Based on slides created by Bjarne.
Introduction to Exception Handling and Defensive Programming.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Lecture 5 Graphics Erick Pranata. » Graphics Overview » About GDI+ » Getting Started.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Tkinter Canvas.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 Graphic Device Interface (GDI). 2 Class Form A Form is a representation of any window displayed in your application. The Form class can be used to create.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Images Part 11 dbg. 2 Images The form and several controls can display a number of different types of image files in the BackgroundImage Property. These.
Windows Programming C# Software Development. Overview  DLLs / PInvoke  DirectX .NET Component Library  Custom Controls  Event Model (in C++)  GUI.
Paint Tutorial Created February 2006 Start Paint: Start>Programs>Accessories>Paint.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
(PART III) Graphics and Multimedia 16/02/1437 Lect6 CT
GDI +. Graphics class's methods System.Drawing Graphics Objects.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
(PART II) Graphics and Multimedia. Font Control Font s  After a Font is created, its properties cannot be modified  Programmers must create a new Font.
Flash Interface, Commands and Functions
Computer Programming I
Graphics and Multimedia
Basic Graphics Drawing Shapes 1.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
CASE Tools Graphical User Interface Programming Using C#
Programming in C# CHAPTER - 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 12 Graphics in Windows and the Web
CPT 450 Computer Graphics 3rd Lecture.
Presentation transcript:

Graphics and Multimedia Part #2 IT 211 Graphics and Multimedia Part #2 Lab #10

Introduction C# language contains many sophisticated drawing capabilities as part of namespace System.Drawing and System.Drawing.Drawing2D those namespace contains the .NET resource for GDI+. GDI+, an extension of the Graphical Device Interface, is an application programming interface (API) that provides classes for advanced two-dimensional drawing. Therefore when you aims to draw shapes in your project first step is to add these namespace. Resource means classes. Therefore when you draw any shape The first step is to include two namespace in project Using System.Drawing Using System.Drawing.Drawing2D Using is similar to import in JAVA. Using System.Drawing; Using System.Drawing.Drawing2D;

System.Drawing namespace’s Classes and Structures. Bitmap Font   FontFamily Graphics Icon Pen Region SolidBrush TextureBrush Image HatchBrush LinearGradient PathGradient class Structure Color Point Rectangle Size Key Brush PathGradientBrush Class means we will study this in the lab insha’ allah blue background means class while gray background means structure. Explain to students “What is classes and what is the structure?” -Classes are Reference types and Structures are Values types. -Classes will support an Inheritance whereas Structures won’t. -Classes can have explicitly parameterless constructors whereas structures can’t. -Member variable initialization is possible in class whereas in Structures, it is not. -It is not possible to declare destructor in structure but in class it is possible. -Process of converting structure type into object type is called boxing and process of converting object type into structure type is called unboxing. -The “this” keyword gives different meaning in structure and class. How? System.Drawing namespace’s Classes and Structures.

Structures in system.Drawing Color has: Properties to set color of various graphical components. Methods to create new colors. Class Font has: Properties to define unique fonts. Class FontFamily has: Methods for obtaining font information. In this lecture we will study color structure

Color Structure Structure Color A R G B values A means Alpha its value determines intensity 0 = transparent, 255 = opaque color. R means red, G means green and B means blue. R value defines the amount of red in the color G value defines the amount of green B value defines the amount of blue. The larger the value, the greater the amount of that particular color. public Color color; color.A color.R color.G color.B All Get the value we cannot use it to set value of ARGB to set the values we will use Color.FromArgb(a,r,g,b);

Color Structure

Color Structure To call these method . Color.FromArgb(a,r,g,b); Color.FromArgb(r,g,b); The overloaded version takes four arguments and allows the user to specify alpha; the three-argument version defaults the alpha to 255.

Coordinate System

Coordinate System In order to drawing in C# you need to understand coordinate system: Upper-left corner of component has coordinates (0, 0) Coordinate pairs: Horizontal coordinate (x-coordinate) Distance to the right from upper-left corner Vertical coordinate (y-coordinate) Distance down from upper-left corner Coordinate units measured in Pixels. Used with structures Rectangle and Point that are provided by System.Drawing namespace. And also to change the location property   x-axis y-axis (0, 0) We use the same coordinate system to change the location property.

Using Coordinate System Rectangle structure defines rectangular shapes with ( width & height) dimension.     x-axis y-axis (x, y) width height Determine the width of the rectangle (x,+ width, y+ height) X Coordinate of the Top left Point in rectangle Y Coordinate of the Top left Point in rectangle Rectangle R= new Rectangle (X, Y, Width, Height);

Using Coordinate System Point structure represents the x-y coordinates of a point on a two-dimensional plane.   X Coordinate of point Point P1= new Point (X, Y);   x-axis y-axis (0,0) x Y Coordinate of point y point

Paint Event

Paint Event We can paint in any event or function. However, Every controller have Paint event which Occurs when the control is redrawn. Paint event is also called automatically by system when events occur such as moving or resizing of windows. Similarly, when controls( such as Label or Button) are displayed the program calls that controls paint method. All controller have paint method however we always use form paint event.

Paint Event When you use Paint event you can deal with the arguments to the Paint method include a PaintEventArgs object from which we can obtain a Graphics object for the control. private void Form1_Paint(object sender, PaintEventArgs e) { //1- Declares the Graphics object and sets it to the Graphics object //2- Insert code to paint the form here. } We can say: e.Graphics

Pen and Brush

Pen and Brush The drawing methods of class Graphics usually require a Pen or Brush object as parameter to render a specified shape. The Pen draws shape outlines; the Brush draws solid objects

Pen Programmers can draw shapes and Strings using Brushes and Pens. Pen objects functions similarly to an ordinary pen, is used to draw lines. constructors allow programmers to specify the colors and widths of the lines that they wish to draw. Pens collection (System.Drawing) contains predefined Pens. We can change color by: Color.Blue When you press dot all colors will display. Define Pen with width =1 Default width // This line creates a black pen with a default thickness of 1. Pen myPen = new Pen(Color.Black); // This line creates a black pen with a thickness of 5. Pen myPen = new Pen(Color.Black, 5); Define Pen with width =5

Brush Brush objects Used to color interiors of shapes In most Fill methods, Brushes fill a space with a color, pattern or image.

Brush Brush Class SolidBrush using System.Drawing.Drawing2D; …. // This line creates a Red brush of type Solid Bruch. SolidBrush myBrush = new SolidBrush(Color.Red); // Start drawing using myBrush Any object paints using myBrush will have red color (internally)

Brush Brush Class HatchBrush For more detail http://www.java2s.com/Code/CSharp/2D-Graphics/HatchBrushStyles.htm using System.Drawing.Drawing2D; int i= 1; Color cb = Color.Red; // define Background color Color  cf =Color.White; // define foreground color HatchBrush  hb = new HatchBrush((HatchStyle)i, cf, cb); // Start drawing using hb We have about 53 HatchStyle each have number. When i=1 When i=2

Brush Brush Class Linear Gradient Brush For more detail http://www.java2s.com/Code/CSharp/2D-Graphics/LineGradient.htm LinearGradientMode lGM = LinearGradientMode.Horizontal; // lGM = LinearGradientMode.Vertical; // lGM = LinearGradientMode.ForwardDiagonal; // lGM = LinearGradientMode.BackwardDiagonal; lGB = new LinearGradientBrush(Rectangle, Color1, Color2,  lGM); When IGM is Horizontal When IGM is Vertical Color1 in our case is red Color2 is white When IGM is ForwardDiagonal When IGM is BackwardDiagonal

Brush Brush Class Texture Brush For more detail http://www.java2s.com/Code/CSharp/2D-Graphics/TextureBrushes.htm TextureBrush myBrush = new TextureBrush(Image.FromFile("tile.bmp"));

Drawing

Start Drawing Till now we study concepts (Tools) to drawing. Exactly like real word Step to draw any object we follow the following steps: Choose Pen Choose Brush Start Drawing shapes

Drawing Lines, Rectangles and Ovals There are two different kind of method of drawing shape one take pen and other take brush. Drawing shape outlines Versions that take a Pen and four Integers Drawing solid shapes Versions that take a Brush and four Integers Four Integer arguments First two represent the coordinates of the upper-left corner of the shape or its enclosing area Last two indicate the shape’s width and height Method take pen -> draw shape outlines. Method take brush-> draw solid shape.

Drawing Lines, Rectangles and Ovals We will see how to call all these function in next slides….

Ellipse bounded by a rectangle. Draw Circle and Ovals DrawEllipse(Pen Pen, Rectangle R) height width (x, y) To draw ellipse or circle we need to define rectangle. To draw ellipse -> we define rectangle. To draw circle -> we define square (rectangle with same width and height) Ellipse bounded by a rectangle.

Drawing Example (1) Demo Demo 30 (run demo to student)… Demo

Drawing Example (1) Design Control Two Combo box Single panel

Drawing Example (1) Add colors to combo box The same for shape… 2

Drawing Example (1) Coding What is our event in this example? When IDE call this event? Every time user change her/his choices color -> automatically call this event…

Drawing Example (1) Coding Any thing we try to draw it will be draw in panel1 not in form When selected index is =0 means user select blue color Note: Point (1,1) is not 1,1 point in form but 1,1 in the panel1. draw position for student in board. When selected index is =2 means user select Red color

Drawing Example (1) Coding When user select index=0 means that draw line When user select index=1 means that draw Circle Clear panel by fill panel with background color

Drawing Example (2) Demo We can draw any thing using primitive graphics shape. However, the idea is understand the coordinate system. Demo 31 Lines Rectangle Rectangle Demo

Drawing Example (2) Design control Single panel

Drawing Example (2) Coding Create graphics object associated with panel1 (150,40) As we said earlier every time we need to paint use paint event for form control and draw inside the panel. (200,100) (100,100) (130,150) (170,150) (100,200) (130,200) (170,200) (200,200)

Reading Assignment: Arcs Drawing Object Intersection Needed by department and used to satisfy HCI rule.

Exception Handling using C# Needed by department and used to satisfy HCI rule.

Exception handling overview Indication of a problem during program execution Problems are exceptional, normally no problems Exception handling Enables programmers to create application that can handle exceptions Enable clear, robust and more fault-tolerant programs Error-Prevention: Exception handling helps improve a program’s fault tolerance.

Exception handling overview CLR in C# support exception handling Structure of exceptions is similar to Java There is no throws clause in C# Example of Exception Handling: Multiple catch clauses are allowed Finally block is executed either the try block raises or not an exception try { … } catch(XXXException) { … } finally { … } Common Language Runtime (CLR)

Exception handling overview Exceptions are unforeseen errors that happen in your programs. There are times when you don't know if an error will occur. This is where exception handling comes in. When exceptions occur, they are said to be "thrown". The System.Exception class provides several methods and properties for obtaining information on what went wrong.

Common C# Exceptions System.ArithmeticException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.InvalidCastException System.MulticastNotSupportedException System.NullReferenceException System.OutOfMemoryException System.OverflowException System.StackOverflowException System.TypeInitializationException

Exception handling keywords Try Include codes in which exceptions might occur Catch represent types of exceptions the catch can handle Finally (Optional) codes present here will always execute.

Try Catch Blocks When exceptions are thrown, you need to be able to handle them, by implementing try catch. Code that could throw an exception is put in the try block. Exception handling code goes in the catch block.

C# exception handling structure try { // Code to try here. } catch (System.Exception ex) // Code to handle exception here. finally // Code to execute after try (and possibly catch) here.

C# exception Example int[] numbers = new int[2]; try { We have defined an array of integers for 2 items, yet we try to use 3 spaces in it. this leads to an error, we can handle it by placing it in try catch block as explained below. int[] numbers = new int[2]; try { numbers[0] = 23; numbers[1] = 32; numbers[2] = 42; } // try catch(Exception ex) { MessageBox.Show(“an Exception“+ex.Message); } // catch

References Color http://www.flounder.com/csharp_color_table.htm Brush 1- http://www.java2s.com/Code/CSharp/2D-Graphics/HatchBrushStyles.htm 2- http://www.java2s.com/Code/CSharp/2D-Graphics/LineGradient.htm 3-http://www.java2s.com/Code/CSharp/2D-Graphics/TextureBrushes.htm Drawing in C# 1- http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html