Implicit Graphics Object Creation

The Bitmap and Metafile objects support implicit Graphics object creation through their getGraphics method. In the following code fragment, the Bitmap object’s getGraphics method is used to create a Graphics object that can be used to draw to the bitmap’s surface:

Bitmap bmp = new Bitmap("c:\\MyImage.bmp");
Graphics gr = bmp.getGraphics();

The first time that you call getGraphics through an object, the object creates a new Graphics object instance and returns it. Subsequent calls to getGraphics through the same object results in the return of the object created through the original call.

Additionally, when you call drawing methods through a Graphics object created through an object’s getGraphics method, the methods apply to the object through which they were created. For example, the x and y coordinates (0,0) specified in the call to drawText above is relative to the bitmap object, not to the control on which the bitmap is rendered.