220 likes | 369 Views
Graphics. Outline Introduction Graphics Contexts and Graphics Objects Color Control Font Control Drawing Lines, Rectangles and Ovals Drawing Polygons and Polylines Loading, Displaying and Scaling Images Animating a Series of Images Microsoft Agent. Introduction.
E N D
Graphics Outline Introduction Graphics Contexts and Graphics Objects Color Control Font Control Drawing Lines, Rectangles and Ovals Drawing Polygons and Polylines Loading, Displaying and Scaling Images Animating a Series of Images Microsoft Agent
Introduction • Graphical Device Interface • Two dimensional vector graphics • Drawing capabilities • Pen or Brush object • Structure Color • Positioning in x,y coordinate system
key System.Drawing class Font Color FontFamily structure Point Graphics Rectangle Icon Size Pen Region SolidBrush HatchBrush TextureBrush LinearGradientBrush Image PathGradientBrush Brush SolidBrush TextureBrush Introduction System.Drawing namespace’s classes and structures.
(0, 0) x-axis +x +y (x, y) y-axis Graphics Contexts and Graphics Objects GDI+ coordinate system. Units are measured in pixels.
Graphics Contexts and Graphics Objects • Graphics context • Drawing surface • Graphics object • control how information is drawn • Virtual OnPaint event handler • Method Invalidate • Refreshes and repaints
Color Control • Enhancing program appearance • Structure Color • ARGB values • Value range from 0 to 255 Run UsingColors.cs
ColorDialog // Create ColorDialog object privatestaticColorDialogmyColorChooser = newColorDialog(); //get chosen color DialogResult result = myColorChooser.ShowDialog(); if (result != DialogResult.Cancel) { // assign something’s color to result of dialog something’s color = myColorChooser.Color; } Demo UsingComplexColors
xy1Õ leading height ascent baseline descent Font Control • Methods and constants of font control • Creating Font • Font’s metrics: • height, decent, ascent and leading • Once created properties cannot be modified • Size property • Return font size measured in design units An illustration of font metrics.
(x, y) height width Drawing Shapes: Lines, Rectangles and Ovals • Graphics methods • Use for drawing lines, rectangles and ovals • Shape outlines take Pen • Solid shapes take Brush • Int argument represent coordinates • Last two int argument are for width and height Ellipse bounded by a rectangle.
Coordinates for bounding rectangle Rectangle’s width and height Drawing rectangle on the screen Drawing object LinesRectanglesOvals.cs // LinesRectanglesOvals.cs // Demonstrating lines, rectangles and ovals. using System; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; // draws shapes on the Form public partial classLinesRectanglesOvals : Form { public LinesRectanglesOvals () { InitializeComponent(); } protected override voidOnPaint(PaintEventArgspaintEvent ) { // get graphics object Graphics g = paintEvent.Graphics; SolidBrush brush = newSolidBrush( Color.Blue ); Pen pen = new Pen( Color.AliceBlue ); // create filled rectangle g.FillRectangle( brush, 90, 30, 150, 90 );
DrawLine takes a Pen and two pairs of ints Uses pen object to draw Start and end point of the line Specify drawing object Overloaded methods DrawEllipse and FillEllipse Coordinates of the bounding rectangle for the ellipse Bounding rectangle’s width and height LinesRectanglesOvals.csProgram Output // 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 ); // 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 } // end class LinesRectanglesOvals
Drawing Polygons and Polylines • Polygons • Multisided shapes • DrawLines • Series of connected points • DrawPolygon • Closed polygon • FillPolygon • Solid polygon
Loading, Displaying and Scaling Images • C#’s multimedia capabilities • Graphics • Images • Animations • video
image is assign through method FromFile Method CreateGraphics to create a Graphics object associated with the Form DisplayLogoForm.cs // DisplayLogoForm.cs // Displaying and resizing an image. using System; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; privateSystem.Windows.Forms.ButtonsetButton; privateSystem.Windows.Forms.TextBoxheightTextBox; privateSystem.Windows.Forms.LabelheightLabel; privateSystem.Windows.Forms.TextBoxwidthTextBox; privateSystem.Windows.Forms.LabelwidthLabel; // displays an image and allows the user to resize it public partial classDisplayLogoForm : Form { private Image image = Image.FromFile( "images/Logo.gif" ); private Graphics graphicsObject; publicDisplayLogoForm() { InitializeComponent(); graphicsObject = this.CreateGraphics(); }
When user select Set, test to validate they are in acceptable range Method Clear to paint entire Form in current background color Graphics method DrawImage is called to display image DisplayLogoForm.cs private voidsetButton_Click(object sender, System.EventArgs e ) { // get user input int width = Convert.ToInt32( widthTextBox.Text ); int height = Convert.ToInt32( heightTextBox.Text ); // if dimensions specified are too large // display problem if ( width > 375 || height > 225 ) { MessageBox.Show( "Height or Width too large" ); return; } // clear Windows Form graphicsObject.Clear( this.BackColor ); // draw image graphicsObject.DrawImage(image, 5, 5, width, height ); } // end method setButton_Click } // end class DisplayLogoForm
Animating a Series of Images • Animate series of images from an array • Collision detection • Regional invalidation
PictureBox contain images to animate Timer to cycle through the image Count keeps track of current image Load each of 30 images and store in an ArrayList Places first image in the PictureBox Modify size if PictureBox to equal size of Image LogoAnimator.cs // LogoAnimator.cs // Program that animates a series of images. using System; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; privateSystem.Windows.Forms.PictureBoxlogoPictureBox; privateSystem.Windows.Forms.Timer Timer; privateSystem.ComponentModel.IContainer components; // animates a series of 30 images public private classLogoAnimator : Form { privateArrayList images = newArrayList(); privateint count = -1; publicLogoAnimator() { InitializeComponent(); for ( inti = 0; i < 30; i++ ) images.Add( Image.FromFile( "images/deitel" + i + ".gif" ) ); // load first image logoPictureBox.Image = ( Image ) images[ 0 ]; // set PictureBox to be the same size as Image logoPictureBox.Size = logoPictureBox.Image.Size; } // end constructor
LogoAnimator.cs Program Output private void Timer_Tick( object sender, System.EventArgs e ) { // increment counter count = ( count + 1 ) % 30; // load next image logoPictureBox.Image = ( Image )images[count ]; } // end method Timer_Tick } // end class LogoAnimator