Java Game Programming for Beginners

Creating a game using Java may look daunting at first, but when you get the hang of the basics, you'll realize that it's a programming language designed to make things easier for developers.

Before, triple A games were created either through C or C++. However, that changed when mobile gadgets took over as the most popular platform for gaming. Many games for the Android and iOS platform today are being developed using Java. Award-winning titles like Angry Birds and Temple Run were both developed using it. The first software to offer online casino entertainment back in 1996, InterCasino, has jumped on the bandwagon in using Java to develop its slot machine games. One of today's popular MMORPGs, Runescape, is also a product of Java programming.

With these basics, you will be able to set yourself in the right direction for developing games using Java. Credits to Edu4Java for this easy-to-follow game programming tutorial.

The Window

To create a game, you'll need to create a window to hold everything together. Here's a snippet that will give you a 300x400 window—something that you can easily find in many Java tutorials:

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class FrameDemo{
    public static void main(String args[]){
        JFrame myFrame = new JFrame("Sample Frame");
        myFrame.setSize(300,400);
        myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }
}

With this code, you will be able to control the window of your game. You can minimize, maximize, and change the size settings to your preference. Apart from keeping things together, the window serves as a command center for the code that will manage your application's user interface.

Animation

Of course, a game without animation would be pretty boring. Here’s an example of how you can code a simple moving object for your game. If you’re thinking about doing a sports game like tennis or a simple poker table game with card animation, this will get you started.

First, you need to determine the starting position of the ball (x and y). Then, to make it move, you have to modify the position with each shift.

package com.edu4java.samplegame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel {
    int x = 0;
    int y = 0;
    private void moveBall() {
        x = x + 1;
        y = y + 1;
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, 30, 30);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Sample Frame");
        Game game = new Game();
        frame.add(game);
        frame.setSize(300, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game.moveBall();
            game.repaint();
            Thread.sleep(10);
        }
    }
}

If you already know the basics of coding in Java, this should be cake for you. But if you’re new to Java programming, there are tons of lessons that you can learn from How to Do in Java’s tutorial pages.

Author avatar

About the author

New Hampshirite building web apps in Florida. Creator of Surreal CMS, Postleaf, and DirtyMarkup.

Need to get in touch? Catch me on Twitter.