Tuesday, October 27, 2009

Programming java applet games part 5 - Java 2d Graphics and Graphics2D class

After some days without any updated finally i have found some time to continue my tutorials. The next part is about the Java and 2d graphics. Here i will represent  the Java classes that you will need to implement the Asteroids java applet. First of all i must present you the Graphics2D class. Graphics2D extends the base class Graphics and provide methods to render enhanced graphics. So if this Graphics2D is so strong why it has not replace the standard Graphics class? The answer is simple; it is just for compatibility reasons with the Java 1.1 and previous version that use the standard Graphics class. So if you use this class for your games then they will run only on browsers that support versions of Java greater than 1.1.
Now lets see the first code snippet
public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D)g;  // make a reference
as you can see we just make a Graphics2D reference on the standard Graphics object that jvm passes automatically to paint method. The features that Graphics2D provides are Colors patterns like gradient fills, fill patterns from images, transparency, local fonts access, new Pen styles, 2d affine transformations.
Java provides classes that represent 2d Objects like triangles, ellipses, polygons etc. For out game everything will be represented by this games. The player object will be represented by a triangle, asteroids will be polygons, bullets will be lines. So we will use the classes that Java already implemented for us.

The first class that we will see is the Polygon. You can find full documentation about the class here http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Polygon.html. When you want to create a polygon you just define a Polygon variable and the add points into it.  
Polygon p = new Polygon();
for( int i = 0; i < NUM_OF_POINTS ; i ++ ) {
    p.addPoint( x[i], y[i] );

at this snippet of code i assume that there are two arrays x,y that contains the polygon coordinates and a NUM_OF_POINTS int variable with the size of these arrays. To draw this polygon just code
g.draw( p );
g.fill( p );
The first one will draw only the outer lines of the polygon will the second will draw a polygons and the inside area.
Ok, for the rendering phase of out game we can use the Polygon class to draw triangle for the user spaceship, polygons for the asteroids and lines for the bullets. What we can do for the update position of our objects?
Java provides classes that implements affine transformations . Ī¤hese transformations are  translate, rotation, scaling, and shearing. With combinations of these transformations we will implement the object movements on the screen. The meanings of translation is to move polygon points to certain coordinates , the  rotations rotates the points , and scaling change the size of the objects. Shearing stretches unevenly the points and will be not used for our game. When we want to move an object make translate transformations onto Graphics2D object and then make the draw for example
g.translate(15.0, 32.0);
g.draw( p );
Thats it for now, play with these classes and in the next tutorial (i hope that it will be soon) i will use these classes into Asteroids game to draw objects and move them.

As allways have nice coding time :-)

No comments:

Post a Comment