Gw Temp

Menu

Tutorial - 'Making Games in Java Part 1' by ThePastyJuggernaut

An item about Programming Languages posted on

Blurb

A good tutorial showing the basics of Java, and geared towards making games.

Body

Welcome. My name’s Pasty, and I’ll be guiding you through one of the newer versatile languages available: Java. First, I’m going to get you started in Java, by developing a Hello World program

Episode I: Java isn’t just for internet

Don’t listen to anyone who tells you otherwise. In fact, you need to punch them in the face before they infect someone else with their personal brand of ignorance. Differences aside, Java and C++ are quite similar, which makes someone who learns Java able to pick up C++ easily. Java allows for one to make versatile applications that can be ported from OS to OS, and still remain super-sport. Of course, one can also make applets, but that isn’t for this article. While Java is very nice for internet, it’s also good for lots more.

Episode II: Java’s inherent library support is without equal

Let’s get that out of the way right now. I’ve used some very decent C++ libraries, and unless someone can point me to some all-purpose behemoth C++ library, J2SE’s library/support is unmatched. 2000+ classes, each explicitly defined in the Java documentation, found at java.sun.com. Unmatched, I say.

Episode III: Getting the Goods

Firstly, you’re going to need to pick up J2SE at java.sun.com. Once you’re there, download "Java 2 Standard Edition" (if you’re reading this tutorial with the intent to learn something about Java, you probably aren’t going to want Java 2 Enterprise Edition or Mobile Edition). J2 is the meat of this sandwich, and it’s what this tutorial is designed for. Now, go to www.jcreator.com. JCreator is one of the best Java compilers for "newbies". Hell, I still haven’t graduated from it, and I could be using much better things right now, like Borland JCreator. XD

Open up the JCreator program once it is installed, and do one thing first: go to the menubar, click "Configure" >> "Options." Once you’ve opened that, go to "JDK Profiles" in the tree, and click "New" if there isn’t anything in the listbox you see. Under "Home Path," point the program to your J2ME installation. There, you’re all ready to start making programs!

Episode IV: You’re first program. Hooray.

Congratulations, you’ve made it this far. Now it’s time to honor a time-honored tradition in the programming world…said tradition is honored by time already, but we’ll honor it regardless. It’s called Hello World. In order to get a feel for the language, you’re going to want to make a Hello World program.

Here is a general make-up of a Java application:

public class
{
public static void main (String[] args)
{

}
}

public - An access modifier for Java, of which there are four: public, private, protected, package. All a public modifier says is that any class can access the current class for which you are writing.

- Your class name goes here, and if the class is public, it needs to have the same name as the title of your Java source file.

Static - Your class/subroutine is a class variable, meaning it does not create multiple instances of itself when running. It exists for all instances of the class.

Void - this subroutine doesn’t have a return type.

Main - this is your ‘main’ subroutine. ‘Main’ is what the program will run before anything else. Think of your program is a tree, and ‘Main’ is the trunk. It can then call different ‘branches’ of the tree as needed.

- any syntactically legal statements can be added here, as well as conditions.

Episode V: Actually writing the ‘Hello World’ program

We are now to the point where we can start writing our "hello world" program. Go to "File" >> New > File. Name your file HelloWorld.java. Finish.

What is our class name?

Our class name is the exact same as the filename of our Java source. In other words, HelloWorld.

public class HelloWorld //this is what you should have so far
{
}

Where’s our main subroutine? Here it is.

public class HelloWorld
{
public static void main (String[] args) //this is what you should have so far
{
}
}

Here’s a new command. It derives itself from the java.lang package, which is fundamental to Java’s operation; therefore it is included for you. Not all packages will be this way, by the way.

System.out.println("Hello this is text welcome to my program");

That’s a semicolon at the end of your statement. It tells the compiler that the statement you’ve just written is complete, and it’s OK to move on to the next line.

So, now that we have output, what do we do?

public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello world!"); //There! The program is complete!
}
}

This is the end of this tutorial. Don’t worry, in part II we’ll cover input, and once we’ve covered input, we’ll cover applets and eventually make our own complete game! These things are serialized for a reason. :P

Until next time,

The Pasty Juggernaut.