0% found this document useful (0 votes)
13 views16 pages

W09 Graphics

Uploaded by

muproductions002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

W09 Graphics

Uploaded by

muproductions002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Fall 2014

Visual
Instructor: Saima Jawad
Programming

Graphics

BASICS
SYSTEM.DRAWING
PAINT EVENT
COLOR, FONT, PEN, BRUSH
LINES, RECTANGLES, ELLIPSES,
ARCS, POLYLINE AND POLYGON

Week-09 Outline

Visual Programming - Fall 2014


Instructor: Saima Jawad 1
Basic Graphics
3

(0, 0)
+x
x-axis
 A graphics context
is a drawing surface, +y

the drawing area of a (x, y)

form or a panel

y-axis

Graphics coordinate system. Units are measured in pixels.

Basic Graphics
4

 A Graphics object manages a graphics context

 Drawing actions are always like:

Graphics g;

// get g
g.DrawLine()…

Visual Programming - Fall 2014


Instructor: Saima Jawad 2
The System.Drawing Library
5

System.Drawing key

Font class
Color
FontFamily structure
Point
Graphics
Rectangle
Icon
Size
Pen

Region

Image
HatchBrush
Bitmap
LinearGradientBrush
ImageAnimator
PathGradientBrush
Brush
SolidBrush

TextureBrush

Creating Pens and Brushes


6

 To draw shape outlines; create a Pen:


Pen pen = new Pen (Color.Blue);
Pen pen2 = new Pen (Color.Blue, 10);

 To draw solid shapes; create a Brush:


Brush brush = new SolidBrush( Color.Blue);

Visual Programming - Fall 2014


Instructor: Saima Jawad 3
Creating Fonts
7

 To create new fonts:

Font arial = new Font(“Arial”, 24, FontStyle.Bold);


Font courier = new Font(“Courier New”, 18,
FontStyle.Strikeout);

12 overloaded Font constructors, the most common one takes three


arguments:
 FontFamily, represented as a string
 Size of the font, represented as an int
 FontStyle, could be Plain, Italic, Bold, Regular,
Strikeout, or Underline.

The Paint Event


8

 When a control becomes visible/resized etc,


it will receive a Paint event from the run
time system

 Invalidate() generates a Paint event


as well
 forexample you want to redraw graphs after you
update a computation

Visual Programming - Fall 2014


Instructor: Saima Jawad 4
Handling the Paint Event
9

 A form has a virtual event handler OnPaint() for


the Paint event; thus one way is to override the
virtual event handler OnPaint().

protected override void OnPaint( PaintEventArgs e )


{
Graphics g = e.Graphics;
Pen pen = new Pen( Color.Blue );
g.DrawLine(pen, 0, 0, 150, 150);
}

Handling the Paint Event


10

 Another possibility is to write your own event


handler to handle the Paint event.


private void myForm_Paint (object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen( Color.Blue );
g.DrawLine(pen, 0, 0, 150, 150);
}

Visual Programming - Fall 2014


Instructor: Saima Jawad 5
Using Color
11

 The easiest way to specify color is to use the system defined


color, e.g., Color.Blue, Color.White

 Another way is to call the Color.FromArgb() method:


Color itsRed = Color.FromArgb(255, 0, 0);
Color itsHtRed = Color.FromArgb(100, 255, 0, 0);

Constants in structure RGB value Constants in structure RGB value


Color (all are Color (all are
public static) public static)
Orange 255, 200, 0 White 255, 255, 255

Pink 255, 175, 175 Gray 128, 128, 128


Cyan 0, 255, 255 DarkGray 64, 64, 64
Magenta 255, 0, 255 Red 255, 0, 0
Yellow 255, 255, 0 Green 0, 255, 0
Black 0, 0, 0 Blue 0, 0, 255
Color structure static constants and their RGB values

Color Methods and Properties


12

Structure Color Description


methods and
properties
Common Methods

static FromArgb Creates a color based on red, green and blue values expressed as ints
from 0 to 255. Overloaded version allows specification of alpha, red,
green and blue values.
static FromName Creates a color from a name, passed as a string.
Common Properties
A byte between 0 and 255, representing the alpha component.
R byte between 0 and 255, representing the red component.
G byte between 0 and 255, representing the green component.

B byte between 0 and 255, representing the blue component.


Color structure members

Visual Programming - Fall 2014


Instructor: Saima Jawad 6
Set Color Example
13

Set Color Example


14

Color frontColor = Color.FromArgb(100, 0, 0, 255);


protected override void OnPaint( PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush(Color.Blue);
brush.Color = frontColor;
g.FillRectangle(brush, 50, 30, 200, 100);
}

Visual Programming - Fall 2014


Instructor: Saima Jawad 7
Set Color Example
15

void setColorButton_Click ( object sender, EventArgs e)


{
frontColor = Color.FromArgb(
Convert.ToInt32(alphaTextBox.Text),
Convert.ToInt32 (redTextBox.Text),
Convert.ToInt32 (greenTextBox.Text),
Convert.ToInt32(blueTextBox.Text) );
Invalidate();
}

Drawing Lines, Rectangles and Ovals


16

 Graphics Methods for drawing

 Outline shapes take Pen


 Solid shapes take Brush

 int argument represent coordinates

Visual Programming - Fall 2014


Instructor: Saima Jawad 8
Drawing Lines, Rectangles and Ellipses
17

Graphics Drawing Methods and Descriptions.


DrawLine( Pen p, int x1, int y1, int x2, int y2 )
Draws a line from (x1, y1) to (x2, y2). The Pen determines the color, style and width of the
line.
DrawRectangle( Pen p, int x, int y, int width, int height )
Draws a rectangle of the specified width and height. The top-left corner of the rectangle is at point
(x, y). The Pen determines the color, style, and border width of the rectangle.

FillRectangle( Brush b, int x, int y, int width, int height )


Draws a solid rectangle of the specified width and height. The top-left corner of the rectangle is at
point (x, y). The Brush determines the fill pattern inside the rectangle.

DrawEllipse( Pen p, int x, int y, int width, int height )


Draws an ellipse inside a rectangle. The width and height of the rectangle are as specified, and its
top-left corner is at point (x, y). The Pen determines the color, style and border width of the
ellipse.

FillEllipse( Brush b, int x, int y, int width, int height )


Draws a filled ellipse inside a rectangle. The width and height of the rectangle are as specified,
and its top-left corner is at point (x, y). The Brush determines the pattern inside the ellipse.

Drawing an Ellipse
18

Ellipse bounded by a Rectangle


(x, y)

height

width

DrawEllipse( Pen p, int x, int y, int width, int height );

Visual Programming - Fall 2014


Instructor: Saima Jawad 9
Example: Drawing Shapes

Example: Drawing Shapes

protected override void OnPaint( PaintEventArgs e )


{
// get graphics object
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush( Color.Blue );
Pen pen = new Pen( Color.AliceBlue );
// create filled rectangle
g.FillRectangle( brush, 90, 30, 150, 90 );
// draw lines to connect rectangles
g.DrawLine( pen, 90, 30, 110, 40 );
g.DrawLine( pen, 90, 120, 110, 130 );
g.DrawLine( pen, 240, 30, 260, 40 );
g.DrawLine( pen, 240, 120, 260, 130 );

Visual Programming - Fall 2014


Instructor: Saima Jawad 10
Example: Drawing Shapes

// draw top rectangle


g.DrawRectangle( pen, 110, 40, 150, 90 );
// set brush to red
brush.Color = Color.Red;
// draw base Ellipse
g.FillEllipse( brush, 280, 75, 100, 50 );
// draw connecting lines
g.DrawLine( pen, 380, 55, 380, 100 );
g.DrawLine( pen, 280, 55, 280, 100 );
// draw Ellipse outline
g.DrawEllipse( pen, 280, 30, 100, 50 );
} // end method OnPaint

Lines with different Pen Styles


22

Pen coloredPen = new Pen(Color.Blue);


// draw a green line
g.DrawLine(coloredPen, 50, 30, 250, 150);

// draw a dashed yellow line


coloredPen.Width = 4;
coloredPen.Color = Color.Yellow;
coloredPen.DashStyle = DashStyle.Dash;
g.DrawLine(coloredPen, 50, 30, 195, 150);

Visual Programming - Fall 2014


Instructor: Saima Jawad 11
Using Textured Brush
23

Bitmap texBitmap = new


Bitmap("C:\\Images\\picture.gif");

// create textured brush and display textured rectangle


TextureBrush texBrush = new
TextureBrush(texBitmap);
g.FillRectangle(texBrush, 0, 50, 250, 200);

Drawing Arcs
24

Positive Arc Negative Arc

270° 270°

180° 0° 180° 0°

90° 90°

Properties of an Arc
 Starting angle
 Arc angle
Positive and negative arc angles.
 Bounding rectangle

Visual Programming - Fall 2014


Instructor: Saima Jawad 12
Drawing Arcs
25

Graphics Methods And Descriptions


DrawArc( Pen p, int x, int y, int width, int height,
int startAngle, int sweepAngle )
Draws an arc of an ellipse, beginning from angle startAngle (in degrees) and sweeping
sweepAngle degrees. The ellipse is defined by a bounding rectangle of width w, height h and
upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

DrawPie( Pen p, int x, int y, int width, int height,


int startAngle, int sweepAngle )
Draws a pie section of an ellipse, beginning from angle startAngle (in degrees) and sweeping
sweepAngle degrees. The ellipse is defined by a bounding rectangle of width w, height h and
upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

FillPie( Brush b, int x, int y, int width, int height,


int startAngle, int sweepAngle )
Functions similarly to DrawPie, except draws a solid arc (i.e., a sector). The Brush determines
the fill pattern for the solid arc.

Graphics methods for drawing arcs

Drawing Pie and Arc


26

SolidBrush solidBrush = new SolidBrush(Color.Red);


Pen coloredPen = new Pen(Color.Red);

// draw Pie and Arc


coloredPen.Width = 2;
g.DrawArc(coloredPen, 40, 30, 75, 100, 0, 90);
g.DrawPie(coloredPen, 40, 30, 75, 100, 90, 90);
g.FillPie(solidBrush, 40, 30, 75, 100, 180, 90);

// clearing the background with a color


g.Clear(Color.Black);

Visual Programming - Fall 2014


Instructor: Saima Jawad 13
Drawing Polylines and Polygons
27

 DrawLines (Pen, Point [])


 DrawPolygon (Pen, Point[])
 FillPolygon (Pen, Point[])

Point [] pointsArray= {new Point(0,0),new


Point(100,0), new Point(100,500), new Point(0,200)};

g.DrawLines(coloredPen, pointsArray);
g.DrawPolygon(coloredPen, pointsArray);
g.FillPolygon(solidBrush, pointsArray);

Visual Programming - Fall 2014


Instructor: Saima Jawad 14
// Using ComboBox to select shape to draw

// get selected index, draw shape


private void imageComboBox_SelectedIndexChanged(
object sender, System.EventArgs e )
{
// create graphics object, pen and brush
Graphics myGraphics = base.CreateGraphics();

// create Pen using color DarkRed


Pen myPen = new Pen( Color.DarkRed );

// create SolidBrush using color DarkRed


SolidBrush mySolidBrush =
new SolidBrush( Color.DarkRed );

// clear drawing area setting it to color White


myGraphics.Clear( Color.White );

// find index, draw proper shape


switch ( imageComboBox.SelectedIndex )
{
case 0: // case circle is selected
myGraphics.DrawEllipse(
myPen, 50, 50, 150, 150 );
break;
case 1: // case rectangle is selected
myGraphics.DrawRectangle(
myPen, 50, 50, 150, 150 );
break;
case 2: // case ellipse is selected
myGraphics.DrawEllipse(
myPen, 50, 85, 150, 115 );
break;
case 3: // case pie is selected
myGraphics.DrawPie(
myPen, 50, 50, 150, 150, 0, 45 );
break;
case 4: // case filled circle is selected
myGraphics.FillEllipse(
mySolidBrush, 50, 50, 150, 150 );
break;
case 5: // case filled rectangle is selected
myGraphics.FillRectangle(
mySolidBrush, 50, 50, 150, 150 );
break;
case 6: // case filled ellipse is selected
myGraphics.FillEllipse(
mySolidBrush, 50, 85, 150, 115 );
break;

Visual Programming - Fall 2014


Instructor: Saima Jawad 15
case 7: // case filled pie is selected
myGraphics.FillPie(
mySolidBrush, 50, 50, 150, 150, 0, 45 );
break;

} // end switch
} // end method imageComboBox_SelectedIndexChanged

Visual Programming - Fall 2014


Instructor: Saima Jawad 16

You might also like