22
Chapter 9 Color, Sound and Graphics Key Concepts: Applying color to GUI Using images in applications Using Sounds in applications Using Graphics class © 2012 EMC Publishing, LLC Slide 1

Chapter 9 Color, Sound and Graphics Key Concepts: Applying color to GUI Using images in applications Using Sounds in applications Using Graphics

Embed Size (px)

Citation preview

© 2012 EMC Publishing, LLC

Chapter 9Color, Sound and Graphics

Chapter 9Color, Sound and Graphics

Key Concepts:

Applying color to GUI Using images in applications Using Sounds in applications Using Graphics class

Slide 1

© 2012 EMC Publishing, LLC

Slide 2

Chapter 9

Using ColorChapter 9

Using Color

Color can enhance an application interface.

Control objects have BackColor and ForeColor properties.

Visual Basic color constants can be used to change background and foreground colors at run time.

BackColor and ForeColor can be chnaged at runtime: see synthax in Notes

© 2012 EMC Publishing, LLC

Slide 3

Chapter 9

The Color Dialog BoxChapter 9

The Color Dialog Box

Predefined dialog box that allows the user to select a color.

© 2012 EMC Publishing, LLC

Slide 4

Chapter 9

The ColorDialog ControlChapter 9

The ColorDialog Control

Properties include:

Color is the color selected in the dialog box.

AllowFullOpen allows the user to create a custom color.

The Color dialog box is displayed using the ShowDialog() method.

© 2012 EMC Publishing, LLC

Slide 5

Chapter 9

Using ImagesChapter 9

Using Images

Most objects have a BackgroundImage property.

The Button and Label control objects have an Image property.

Images can be changed at run time using the My.Resources object:

Me.btnEvents.Image = My.Resources.Flower

Me.BackgroundImage = My.Resources.Balloons

© 2012 EMC Publishing, LLC

Slide 6

Chapter 9

The SystemSounds ClassChapter 9

The SystemSounds Class

Includes five properties for making Windows operating system sounds:

AsteriskBeepExclamationHandQuestion

The Play() method is used to make the sound:SystemSounds.Beep.Play()

Applications require the Imports System.Media statement before any declaration

© 2012 EMC Publishing, LLC

Slide 7

Chapter 9

Playing Sound FilesChapter 9

Playing Sound Files

Visual Basic applications can play Windows WAV audio files.

My.Computer.Audio objects includes the Play() and Stop() methods for working with WAV files.

-Play(resource, playmode)

-Stop()

Audio files must be added to the Resources folder for an application to access them.

© 2012 EMC Publishing, LLC

Slide 8

Chapter 9

The Timer ControlChapter 9

The Timer Control

Applications often perform actions at regular intervals.

The Timer object is used to execute code at regular intervals

Add from the Tollbox to the component tray

© 2012 EMC Publishing, LLC

Slide 9

Chapter 9

The Timer ControlChapter 9

The Timer Control

Properties include:

(Name) should begin with tmr.

Interval is the amount of time that passes before the Tick event procedure executes.

Enabled allows a Tick event to occur at the end of each interval.

Methods include Start() and Stop().

© 2012 EMC Publishing, LLC

Slide 10

Chapter 9

AnimationChapter 9

Animation

Uses a timer control object and a set of images.

Images are cycled in a PictureBox control object to give the impression of motion.

A Case statement can be used to determine which image to display.

© 2012 EMC Publishing, LLC

Slide 11

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

Includes numerous methods for creating lines, shapes, solid shapes, and curves and polygons.

Methods require that a drawing surface be defined.

Dim formSurface as Graphics=Me.CreateGraphics

- This creates a Graphics object from the surface of the form

- CreateGraphics method is a control class method that encases a specific’s object area

© 2012 EMC Publishing, LLC

Slide 12

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

Methods require that a Pen object be defined. Pen styles can be solid, dashed, and dotted. There are numerous colors to choose from.

Dim thinAquaPen As New Pen(Color.Aqua, 2)

- the keyword New declares a new object

- ColorAqua is a Visual Basic color constant

- 2 is the thickness of the line

© 2012 EMC Publishing, LLC

Slide 13

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

A drawing surface can be thought as a grid consisting of a set of points with (x, y) values

Each point is a pixel ( picture element ) and the number of pixels in a surface depends on the screen resolution. The point with coordinates (0, 0)is the pixel in the very upper-left corner

Methods can accept coordinates in (x1, y1), (x2, y2) format or as Point structures that define each point.

© 2012 EMC Publishing, LLC

The Graphics ClassThe Graphics Class

The Size property of an object stores both height and width. For many objects ( labels, buttons…)the height and the width correspond to the coordinates of point in the lower right of the object

For a Form object the Size property returns the size of the form

Dim formMaxX As Integer = Me.Size.Width ‘300

Dim formMaxY as Integer = Me.Size.Height ‘300

Returns the Form’s Size property data.

Slide 14

© 2012 EMC Publishing, LLC

Slide 15

Chapter 9

The Graphics Drawing SurfaceChapter 9

The Graphics Drawing Surface

A form's drawing surface is reduced by the title bar and borders. Each square on the grid represents 10 pixels. Counting the grid marks show that although the form size is 300 by 300, the actual drawing surface is less, about 292 by 265.

© 2012 EMC Publishing, LLC

Slide 16

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

The Graphics class methods require a pen along with the shape position and size.

Graphics methods include:

DrawLine(pen,x1,y1,x2,y2)->line from(x1, y1) to (x2, y2)

DrawRectangle(x1,y1,width,height)-> draws rectangle with upper-left corner at (x1,y1) and size width x height

DrawEllipse(x1,y1,width,height)-> draws an ellipse within a rectangular area that has upper-left corner at (x1,y1) and size width x height

© 2012 EMC Publishing, LLC

Slide 17

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

DrawArc(pen,x1,y1,width,height,startAngle,sweepAngle) ->draws an arc tha starts at angle startAngle and continues clockwise sweepAngle degrees. The arc is within a rectangular area that has its upper-left corner at (x1,y1) on a Graphics object with width ang height

Clear(color)-> clears the drawing surface with “color”which can be a System.Drawing color constant or the current object color

© 2012 EMC Publishing, LLC

Slide 18

Chapter 9

The Graphics ClassChapter 9

The Graphics Class

The shapes in the TestGraphics application have been drawn using absolute coordinates, meaning that actual sizes were used to specify the shape sizes

Relative coordinates are another option. They are relative to the size of the form. If the form is resized the shapes maintain their relative size and position. See DrawShapes program and change Form’s size.

The Pen class contains the DashStyle property: dashed lines, dotted lines and dash-dot are a few examples.

© 2012 EMC Publishing, LLC

Slide 19

Chapter 9

Drawing Solid ShapesChapter 9

Drawing Solid Shapes

The Graphics class methods that fill shapes require a brush along with the shape position and size.:

Dim purpleBrush as New SolidBrush(Color.BlueViolet)

FillRectangle(brush,x1,y1,width,height)

FillEllipse(brush,x1,y1,width,height)

FillPie(brush,x1,y1,width,height,startAngle,sweepAngle)

The parrameters of the methods are the same as corresponding Draw methods.

© 2012 EMC Publishing, LLC

Slide 20

Chapter 9

The Point StructureChapter 9

The Point Structure

• A point has an x-coordinate a y-coordinate

• A Point structure can be defined with X and Y members:

Dim minPoint As Point

minPoint.X=0

minPoint.Y=0

Or

Dim minPoint as New Point(0, 0)

© 2012 EMC Publishing, LLC

Slide 21

Chapter 9

Drawing curves and PolygonsChapter 9

Drawing curves and Polygons

• DrawCurve(pen, points)

• DrawClosedCurve(pen, points)

• FillClosedCurve(brush, points)

• DrawPolygon(pen, points)

• FillPolygon(brush, points)

© 2012 EMC Publishing, LLC

Slide 22

Chapter 9

The MouseDown EventChapter 9

The MouseDown Event

Occurs when the user clicks the form or object associated with the MouseDown event.

Select Code window, click the object name in the Class Name list and then select MouseDown in the Method Name list

The e parameter includes X and Y properties specific to the mouse click that raised the event. The X and Y properties can be used to determine the coordinates of the mouse click.