41
C# .NET Graphics Extra – not tested Required for assignment 1

Graphics

Embed Size (px)

DESCRIPTION

C# Graphics

Citation preview

Page 1: Graphics

C# .NET Graphics

Extra – not tested

Required for assignment 1

Page 2: Graphics

What will we be learning?

Surface, pen and brush

Draw Lines

Draw Rectangles

Draw Polygons

Draw Text

Page 3: Graphics

Basic - Graphics

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

to a surface)

Page 4: Graphics

Basic

Declare

Graphics mySurface;

Page 5: Graphics

Basic

Create

Graphics mySurface;mySurface = this.CreateGraphics();

Page 6: Graphics

Basic - Pen

(2) To draw lines or outlines of

shapes, we need a Pen

Page 7: Graphics

Basic - Pen

Declare

Pen pen1;

Page 8: Graphics

Basic - Pen

Create

2 parameters- Color

- thickness

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

Page 9: Graphics

Basic - Brush

(3) To fill inside shapes, we need

a Brush

Page 10: Graphics

Basic - Brush

Declare

Types of brushSolidBrushHatchBrush

LinearGradientBrushPathGradientBrush

TextureBrush

SolidBrush brush1;

Page 11: Graphics

Basic - Brush

Create

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

Page 12: Graphics

Basic - BrushTypes of brush

SolidBrushHatchBrush

LinearGradientBrushPathGradientBrush

TextureBrush

Page 13: Graphics

Draw Lines> New Solution: SpfGraphics

> Select Form1.cs, select design view

> select events and double click “Paint” event

Page 14: Graphics

Draw Lines

Page 15: Graphics

Draw Lines

Page 16: Graphics

Output

Page 17: Graphics

Using points & DrawLinesContinue in Form1_Paint

Page 18: Graphics

Output

surface1.DrawLine(pen1, pt1, pt3);

surface1.DrawLines(pen1, points);

Page 19: Graphics

Draw RectangleContinue in Form1_Paint

Page 20: Graphics

Draw Rectangle

Page 21: Graphics

Fill Shape

Continue in Form1_Paint

Page 22: Graphics

Fill Shape

Page 23: Graphics

Draw Polygon

Continue in Form1_Paint

Page 24: Graphics

Draw Polygon

Page 25: Graphics

Draw Text

Continue in Form1_Paint

Page 26: Graphics

Draw Text

Page 27: Graphics

Try it yourself: drawing cross for mousedown

VID

EO

Page 28: Graphics

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)

Page 29: Graphics

Possible solution

Page 30: Graphics

Draw fix line follow mouse

Page 31: Graphics

Draw fix line follow mouse

New project “lineFollowMouse”

Page 32: Graphics

Draw fix line follow mouse

// Get the mouse position

Page 33: Graphics

Demo: flexi line follow mouse

VID

EO

Page 34: Graphics

Flexi line follow mouse

// Get the mouse position

Page 35: Graphics

Demo: Simple pixel drawing

VID

EO

Page 36: Graphics

Simple pixel drawing

New project: SimplePixelDrawing

Page 37: Graphics

Simple pixel drawing

Page 38: Graphics

Simple pixel drawing

Page 39: Graphics

Simple pixel drawing

to continue next page

Page 40: Graphics

Simple pixel drawing

continue from previous page

Page 41: Graphics

Summary

Surface, pen and brush

Draw Lines

Draw Rectangles

Draw Polygons

Draw Text