graphics

Post on 08-May-2015

520 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

C# Graphics

TRANSCRIPT

C# .NET Graphics

Extra – not tested

Required for assignment 1

What will we be learning?

Surface, pen and brush

Draw Lines

Draw Rectangles

Draw Polygons

Draw Text

Basic - Graphics

(1) we need to have a Graphics instance (similar

to a surface)

Basic

Declare

Graphics mySurface;

Basic

Create

Graphics mySurface;mySurface = this.CreateGraphics();

Basic - Pen

(2) To draw lines or outlines of

shapes, we need a Pen

Basic - Pen

Declare

Pen pen1;

Basic - Pen

Create

2 parameters- Color

- thickness

Pen pen1;pen1 = new Pen(Color.Blue, 1.0f);

Basic - Brush

(3) To fill inside shapes, we need

a Brush

Basic - Brush

Declare

Types of brushSolidBrushHatchBrush

LinearGradientBrushPathGradientBrush

TextureBrush

SolidBrush brush1;

Basic - Brush

Create

SolidBrush brush1;brush1 = new SolidBrush(Color.Green);

Basic - BrushTypes of brush

SolidBrushHatchBrush

LinearGradientBrushPathGradientBrush

TextureBrush

Draw Lines> New Solution: SpfGraphics

> Select Form1.cs, select design view

> select events and double click “Paint” event

Draw Lines

Draw Lines

Output

Using points & DrawLinesContinue in Form1_Paint

Output

surface1.DrawLine(pen1, pt1, pt3);

surface1.DrawLines(pen1, points);

Draw RectangleContinue in Form1_Paint

Draw Rectangle

Fill Shape

Continue in Form1_Paint

Fill Shape

Draw Polygon

Continue in Form1_Paint

Draw Polygon

Draw Text

Continue in Form1_Paint

Draw Text

Try it yourself: drawing cross for mousedown

VID

EO

Try it yourself!

Hint: use the mousedown event

Hint: use points (e.X-5, e.Y-5) , (e.X+5, e.Y+5), (e.X+5, e.Y-5) , (e.X-5, e.Y+5)

Possible solution

Draw fix line follow mouse

Draw fix line follow mouse

New project “lineFollowMouse”

Draw fix line follow mouse

// Get the mouse position

Demo: flexi line follow mouse

VID

EO

Flexi line follow mouse

// Get the mouse position

Demo: Simple pixel drawing

VID

EO

Simple pixel drawing

New project: SimplePixelDrawing

Simple pixel drawing

Simple pixel drawing

Simple pixel drawing

to continue next page

Simple pixel drawing

continue from previous page

Summary

Surface, pen and brush

Draw Lines

Draw Rectangles

Draw Polygons

Draw Text

top related