1 / 22

Graphics and Multimedia

Graphics and Multimedia. Lecture 7. Outline. Introduction    Graphics Contexts and Graphics Objects    Color Control. Introduction.

mirit
Download Presentation

Graphics and Multimedia

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphics and Multimedia Lecture 7

  2. Outline Introduction   Graphics Contexts and Graphics Objects   Color Control

  3. Introduction • The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other namespaces that make up the .NET resource GDI+. • GDI+, an extension of the Graphical Device Interface, is an application programming interface (API) that provides classes for 2D drawing. • The most commonly used components of GDI+ reside in the System.Drawing and System.Drawing.Drawing2D namespaces.

  4. System.Drawing System.Drawing Key Key Class class Bitmap Bitmap Structure Structure Font Font FontFamily FontFamily Graphics Graphics Color Color Icon Icon Point Point Pen Pen Rectangle Rectangle Region Region Size Size SolidBrush SolidBrush TextureBrush TextureBrush Image Image SolidBrush SolidBrush HatchBrush HatchBrush LinearGradient LinearGradient PathGradient PathGradient SolidBrush SolidBrush TextureBrush TextureBrush Fig. 16.1 System.Drawing namespace’s Classes and Structures.

  5. Introduction • Class Graphicscontains methods used for drawing Strings, lines, rectangles and other shapes on a Control. • The drawing methods of class Graphics usually require a Pen or Brush object to render a specified shape. • The Pen draws shape outlines; • the Brush draws solid objects

  6. Introduction • StructureColor has: • properties to set color of various graphical components. • Methods to create new colors. • Class Fonthas: • Properties to define unique fonts • Class FontFamily has • Methods for obtaining font information.

  7. Introduction To begin drawing in Visual Basic, we first must understand GDI+’s coordinate system: • Upper-left corner of component has coordinates (0, 0) • Coordinate pairs: • Allow positioning of text and shapes • Horizontal coordinate (x-coordinate) • Distance to the right from upper-left corner • Vertical coordinate (y-coordinate) • Distance down from upper-left corner • Coordinate units measured in pixels • Used with structures Rectangle and Pointthat are provided bySystem.Drawing namespace

  8. Introduction (0, 0) +x x-axis +y (x, y) y-axis • Rectanglestructuredefinesrectangular shapes with ( width & height ) dimension. • Pointstructurerepresents the x-y coordinates of a point on a two-dimensional plane.

  9. Graphics Contexts and Graphics Objects • Graphics objects contain methods for drawing, font manipulation, color manipulation and other graphics-related actions. • Every Windows application that derives from class System.Windows.Forms.Form inherits an Overridable OnPaint method where most graphics operations are performed. • The arguments to the OnPaint method include a PaintEventArgs object from which we can obtain a Graphics object for the control. • The OnPaint method triggers the Control’s Paint event.

  10. Graphics Contexts and Graphics Objects • When displaying graphical information on a control, we need to: • Access Graphics object of the control. • Then, use Graphics object to draw shapes and strings on the control. • Graphics object Can be accessed in 2 ways: • By overriding OnPaint() method to retrieve a Graphics object from argument PaintEventArgs • Create a new Graphics object associated with the appropriate surface.

  11. Graphics Contexts and Graphics Objects 1-Overriding OnPaint() : Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ‘ extract the Graphics object from the PaintEventArgs argument: Dim graphicsObject As Graphics = e.Graphics • Calling the OnPaint method raises the Paint event. • Instead of overriding the OnPaint method, programmers can add an event handler for the Paint event because OnPaint method is rarely called directly. Public Sub MyEventHandler_Paint( _ ByVal sender As Object, ByVal e As PaintEventArgs) _ Handles Me.Paint

  12. Graphics Contexts and Graphics Objects • OnPaint is called automatically by system when events occur such as moving or resizing of windows. Similarly, when controls( such as Label or Button) are displayed the program calls that controls paint method. • Programmers can invoke OnPaint explicitly by calling Invalidate method. • This method refreshes a control’s client area and repaints all graphical components.

  13. Graphics Contexts and Graphics Objects 2-Creating a new Graphics: By invoking CreateGraphics method. Dim graphicsObject As Graphics = label1.CreateGraphics() ‘Then, you can use the methods provided in class Graphics to draw on the controlfor example we can draw a circle on label1 graphicsObjects.DrawCircle(……)

  14. Color Control • Structure Color • ARGB values • Alpha, red, green and blue values, respectively • Each value represented as a Byte • Alpha value determines intensity • 0 = transparent, 255 = opaque color. • The first number in the RGB value defines the amount of red in the color, the second defines the amount of green and the third defines the amount of blue. • The larger the value, the greater the amount of that particular color.

  15. Color Control

  16. Color Control The overloaded version takes four arguments and allows the user to specify alpha; the three-argument version defaults the alpha to 255.

  17. Color Control • Programmers draw shapes and Strings using Brushes and Pens. • Penobjects • functions similarly to an ordinary pen, is used to draw lines. • constructors allow programmers to specify the colors and widths of the lines that they wish to draw. • Pens collection (System.Drawing) contains predefined Pens. • Brush objects • Used to color interiors of shapes • In most Fillmethods, Brushes fill a space with a color, pattern or image. • Upcoming example: Color value and alpha demonstration

  18. Color Control

  19. When the application begins its execution, it calls class ShowColors’OnPaint method to paint the window. ShowColors.vb Gets a reference to PaintEventArgse’s Graphics object and assigns it to Graphics object graphicsObject Creates a black and white SolidBrush for drawing on the form Graphics method FillRectangle draws a solid white rectangle with the Brush supplied as a parameter. 1 ' Fig. 16.6: ShowColors.vb 2 ' Using different colors in Visual Basic. 3 4 PublicClass FrmColorForm 5 Inherits System.Windows.Forms.Form 6 30 ' color for back rectangle 31 Private mBehindColor As Color = Color.Wheat 32 33 ' color for front rectangle 34 Private mFrontColor As Color = Color.FromArgb(100, 0, 0, 255) 35 ' overrides Form OnPaint method 37 ProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs) 38 Dim graphicsObject As Graphics = e.Graphics ' get graphics 39 40Dim textBrush As SolidBrush = New SolidBrush(Color.Black) ' create text brush 42 43 Dim brush As SolidBrush = New SolidBrush(Color.White) ' create solid brush 45 46 ' draw white background 47 graphicsObject.FillRectangle(brush, 4, 4, 275, 180) 48 49 ' display name of behindColor 50 graphicsObject.DrawString(mBehindColor.Name, Me.Font, textBrush, 40, 5) 52 53 ' set brush color and display back rectangle 54 brush.Color = mBehindColor 55 56 graphicsObject.FillRectangle(brush, 45, 20, 150, 120) 57 Assigns the ColormBehindColor value to the Brush’s Color property and displays a rectangle

  20. 58 ' display Argb values of front color 59 graphicsObject.DrawString("Alpha: " & mFrontColor.A & _ 60 " Red: " & mFrontColor.R & " Green: " & mFrontColor.G _ 61 & " Blue: " & mFrontColor.B, Me.Font, textBrush, _ 62 55, 165) 63 64 ' set brush color and display front rectangle 65 brush.Color = mFrontColor 66 67 graphicsObject.FillRectangle(brush, 65, 35, 170, 130) 68 EndSub' OnPaint 69 70 ' handle cmdColorValue click event 71 PrivateSub cmdColorValue_Click(ByVal sender As _ 72 System.Object, ByVal e As System.EventArgs) _ 73 Handles cmdColorValue.Click 74 75 ' obtain new front color from text boxes 76 mFrontColor = Color.FromArgb(txtAlphaBox.Text, _ 77 txtRedBox.Text, txtGreenBox.Text, txtBlueBox.Text) 78 79 Invalidate() ' refresh Form 80 EndSub' cmdColorValue_Click

  21. 81 82 PrivateSub cmdColorName_Click(ByVal sender As _ 83 System.Object, ByVal e As System.EventArgs) _ 84 Handles cmdColorName.Click 85 86 ' set behindColor to color specified in text box 87 mBehindColor = Color.FromName(txtColorName.Text) 88 89 Invalidate() ' refresh Form 90 EndSub' cmdColorName_Click 91 EndClass ' FrmColorForm

More Related