.NET
  Home arrow .NET arrow Page 2 - Game Development of .Nettrix: GDI+ and Col...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Game Development of .Nettrix: GDI+ and Collision Detection
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2004-08-23

    Table of Contents:
  • Game Development of .Nettrix: GDI+ and Collision Detection
  • Performing Graphic Operations with a Graphics Object
  • Creating Gradients
  • Collision Detection
  • Proximity Algorithms
  • Optimizing the Number of Calculations
  • Extending the Algorithms to Add a Third Dimension
  • Develop a Real Game Proposal
  • Diagrams of Basic Game Objects
  • The Game Engine
  • The Coding Phase
  • Testing the Program
  • The Block Class
  • The Constructor
  • The Down, Right, and Left Methods
  • The Rotate Method
  • The Show and Hide Methods
  • Final Version: Coding the GameField Class and the Game Engine
  • The CheckLines Method
  • The Game Engine
  • Adding the Final Touches
  • Further Improvements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Game Development of .Nettrix: GDI+ and Collision Detection - Performing Graphic Operations with a Graphics Object


    (Page 2 of 22 )

    When using GDI+, the very first step always is to create a Graphics object, which will help you to perform graphics operations. The Graphics class provides methods for drawing in a specific device context.

    There are four ways to attain the correct Graphics object: with the e parameter received in the Paint event, from a window handle, from an image, or from a specified handle to a device context. There’s no real difference among these different approaches; you’ll use each one depending on your program needs. For example, if you are coding your drawing functions on the Paint event of the form, you’ll use the e parameter; but if you are coding a class to draw on a form, you’ll probably want to use a window handle to create the Graphics object. We discuss each method in the sections that follow.

    Creating a Graphics Object with the PaintEventArgs Parameter

    In this case, all drawing code must be associated with the Paint event of the destination image object. The following code shows how to draw a simple red rectangle at the 10, 20 position (in pixels) on the screen, 7 pixels high and 13 pixels long:

    private PicSourcePaint(Object sender, Windows.Forms.PaintEventArgs e)
    {
    e.Graphics.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
    }

    Note:  In these first few lines of code, you can see the event-handling features of .NET, as described here:

    Every event handler in C# receives at least two parameters, the sender object, which is the object that generates the event, and an object related to the event (the EventArgs object).

    The event handler procedure is now associated with the object by associating the method to the event, typically in the InitializeComponent method. The association is done with the += operator like this:

    this.PicSource.Paint += new 
       System.Windows.Forms.PaintEventHandler(this.picBackgroundPaint);

    The e parameter is of the type Windows.Forms.PaintEventArgs. You will notice that everything in .NET languages is organized into managed units of code, called namespaces. In this case, you use the System.Windows.Forms namespace, which contains classes for creat ing Windows-based applications using the features of the Windows operating system. Inside this namespace, you use the PaintEventArgs class, which basically gives the Paint event access to the rectangle structure that needs to be updated (ClipRectangle property), and the Graphics object used to update it.

    The Graphics and SolidBrush classes are defined in the System.Drawing namespace. This namespace has several classes that provide all the functionality you need to work with 2-D draw ings, imaging control, and typography. In the code sample, you create a SolidBrush object with red color (using the Color structure) to draw a filled rectangle using the FillRectangle method of the Graphics object.

    Creating Graphics Objects from a Window Handle

    In order to create any graphical images in GDI+, you must ask for a “handle” to the drawable part of a window. This handle, which is a Graphics object, can be obtained by the Graphics.FromHwnd method (Hwnd means “Handle from a window”). In the code shown here, Graphics.FromHwnd is a shortcut for the System.Drawing.Graphics.FromHwnd method, which creates a Graphics object used to draw in a specific window or control, given its handle. This code references a pictureBox control named picSource:

    Graphics graph = new Graphics();
    graph = Graphics.FromHwnd(picSource.Handle);
    graph.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);

    Creating Graphics Objects from an Image

    The FromImage method shown here creates a Graphics object from the specified image:

    Graphics graph = new Graphics();
    graph = Graphics.FromHwnd(picSource.image);
    graph.FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);

    Note that the previous code sample will work only if you have a valid bitmap image loaded on the pictureBox control. If you try to execute it against an empty picture box or using a picture box with an indexed pixel format image loaded (such as a JPEG image), you’ll get an error and the Graphics object won’t be created.

    Creating a Graphics Object from a Specified Handle to a Device Context

    Similar to the previously mentioned methods, the Graphics.FromHdc method creates a Graphics object that allows the program to draw over a specific device context, given its handle. You can acquire the device handle from another Graphics object, using the GetHdc method, as shown in the next code snippet:

    public void FromHdc(PaintEventArgs e) {
    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();
    // Create new graphics object using handle to device context.
    Graphics newGraphics = Graphics.FromHdc(hdc);
    newGraphics. FillRectangle(new SolidBrush(Color.Red), 10, 20, 13, 7);
    // Release handle to device context.
    e.Graphics.ReleaseHdc(hdc);

    This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Apress Publishing


       · Examples from the first page are invalid. You cannot do:Graphics g = new...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT