Wednesday, July 11, 2007

Drawing An Image

Back To Basics

On of the first things that you need to be able to do when creating a game is to simply draw an image to a particular location within a window. Its relatively simple to draw the image to the screen, for that you would just use the following call:
//this call draws the image on the JFrame.
g.drawImage(imgToRender, 0, 50, null);
Well now that is if you already have created a valid image and assigned it to 'imgToRender'. Well unfortunately in Java there is no one accepted way to load an image. And of course you are probably thinking about loading an image from a file so that anyone who has Paint or Photoshop skills can go in an create or update art for your game. Well here is one way:
// This is one method to load images
imgToRender = (new ImageIcon("pictures/redX.png")).getImage();
You can replace the "pictures/redX.png" with the path to your own image. In the path string if you do not specify a drive or protocol type meaning file:///, C:/ or http://) then you can put in the relative path from the execution directory.

I will discuss loading resources such as image and sound files from different location in another post but essentially the basics of drawing an image are really all you need to do tons of cool things in your game.

Seeing Is Believing

No comments:

Post a Comment