Monday, September 14, 2009

Programming java applet games part 1 - Short intro to applets

Java applets is one of the best  ways to start teach yourself about game programming and they are related to my previous article about browser games.


We will start to discuss the basics about Java applets and step by step we will built a complete game. If you are an experienced programmer probably you will learn nothing new here but you can still read this in case you will find something interesting or to propose improvements.

To understand this tutorial i except that you are already a little bit familiar with the computer language  java but i need you to know only the basics (what is the jdk a class etc) and you have to know how to use a text editor like notepad or kate. Of course the best approach is to use an IDE (integrated development environment) such as Netbeans (god bless these developers).

OK its time to write the first snippets of code. Lets build the first applet

import java.applet.Applet;
import java.awt.Graphics;

public class HelloGamers extends Applet {
    public void paint (Graphics g ){
         g.drawString("Hello gamers", 10, 10 );
    }
}

The above code does not do anything special. It just prints a text on the screen of your web browser. Save the above code in a file named HelloGamers.java as java demands. In this example we use classes from the packages java.applet and java.awt to implement our simple applet. The class applet is the base that must be extended by every class that needs to be an applet.
So every  applet needs to be written like that

public class SomeApplet extends Applet

The class Graphics contains methods to drow objects (like Strings) on the web browser Screen

If you are not familiar with the term method do not panic, they are just the functions of a class. Some years ago in the Object oriented programming languages (like Java) we have stopped to use the term function and member variable and we say method and attributes. So lets proceed with the next important thing

  public void paint (Graphics g)

Every Applet has a paint method. This is the method that is called by the jvm to draw things on the screen. So every applet we create must override this method to display what our applet needs to display. Paint takes only one parameter, the Graphics object what can print with.


To print string Graphics has the method drawString so

  g.drawString("Hello gamers", 10, 10 );

prints a string on the coordinates 10, 10, Coordinates 0,0 point to the upper left corner of the area that is reserved for the applet.

So lets embed out applet into an html file to see it with the web browser.  Compile the java class with the compiler

javac HelloGamers.java

as you know the above command creates a .class file actually this command will create the HelloGamers.class. To embed the applet open a text editor and type the html snippet

Save the file with a name that you want and close the file

<HTML>
<BODY>
<APPLET CODE="HelloGamers.class" WIDTH=200 HEIGHT=200>
</BODY>
</HTML>

So that was the first step, now you can read the javadoc of class Graphics to see the functions that provide  and play with.
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

Have fun and wait for the next tutorial

No comments:

Post a Comment