Computer Science 196 Introductory Programming with Visual Basic 'NET PowerPoint PPT Presentation

presentation player overlay
1 / 14
About This Presentation
Transcript and Presenter's Notes

Title: Computer Science 196 Introductory Programming with Visual Basic 'NET


1
Computer Science 196Introductory
Programmingwith Visual Basic .NET
  • Drawing

2
System.Drawing Namespace
  • Rectangle structure defines size and position for
    rectangle on form.
  • SolidBrush class used to paint interior of shape
    with single color.
  • Pen class used to draw lines and curves.
  • Font class defines typeface, size, etc. for text.
  • Color structure for predefined or programmer
    designed colors.
  • Graphics class has methods for drawing shapes,
    lines, etc.

3
Rough idea of how things work
  • To paint a rectangle we need
  • A rectangle
  • A color to paint
  • A brush dipped into the paint
  • A painter (graphics object) to use the brush to
    paint the rectangle

4
The graphics coordinate system
  • A graphics coordinate system is a system for
    identifying points on a form (or other object)
  • The points are referred to as picture elements or
    pixels
  • The points within the form all fall within one
    quadrant of the coordinate system - other points
    are not visible.
  • The origin is in the upper left corner
  • The x-coordinate increases to the right.
  • The y-coordinate increases downward.

5
Coordinate system
(0,0)
(-50,0)
(0,0)
(50,0)
(50,50)
(0,50)
Form 150 x 100
6
Constructors revisited
  • Recall that a constructor is a method called New
    in the given class.
  • It is used to create a new instance of the class
  • Typically a class will have more than one
    constructor (different argument lists).

7
Defining a Rectangle
  • One constructor for the Rectangle class has four
    Integer parameters.
  • The first two give the x,y coordinates for the
    upper left corner of the rectangle.
  • The last two give the width and height of the
    rectangle (in pixels).
  • Dim recBigRect As New Rectangle(10,20,100,75)
  • Dim recLittleRect As Rectangle
    recLittleRect New Rectangle (50,200,15,10)

8
Color structure
  • Colors use the RGB system
  • Red component has value of 0-255
  • Same for Green and Blue
  • There is also an alpha value for the brightness,
    also 0-255
  • To create a color
  • Declare a Color variable
  • Use Colors FromArgb method to create the color
  • Dim colMyColor As Color colMyColor
    Color.FromArgb(125,222,105,200)
  • The parameters are Alpha, Red, Green, Blue

9
Color structure
  • The Color structure has many built in colors
  • Dim colMyColor as Color Color.Red

10
SolidBrush
  • The SolidBrush class has a constructor with a
    single parameter, a color.
  • This creates a SolidBrush with the specified
    color (dipped in the color)
  • Dim sbMyBrush As New SolidBrush(Color.Red)

11
Pen
  • The Pen class has a constructor with a Color
    parameter and a Single parameter for the width
  • Dim penMyPen As New Pen(Color.Blue,3)

12
The Paint event
  • Occurs
  • Right after form is loaded
  • Form resized
  • Form moved, etc
  • Refresh method called
  • The event handler for this event is one place
    where we can do drawing

13
The Paint event - event handler
  • The event handler has a parameter that is of type
    System.Windows.Forms.PaintEvenArgs
  • This parameter has a Graphics property that can
    be used to draw instances of shapes, etc.
  • Typical start

14
Graphics methods
  • FillRectangle with parameters a brush and a
    rectangle
  • graCurrent.FillRectangle(sbMyBrush,recMyRect)
  • DrawRectangle with parameters a pen and a
    rectangle
  • graCurrent.DrawRectangle(penMyPen,recMyRect)
  • DrawLine with parameters a pen and four integers
    for x,y coordinates of two points
  • graCurrent.DrawLine(penMyPen, 30,40,6,80)
Write a Comment
User Comments (0)
About PowerShow.com