I’m sure there are many young and energetic Java programmers out there that are excited about J4K and want to develop games, but simply don’t know where to begin. Here’s a simple template I put together that may encourage those people to contribute an entry:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class M extends JFrame {
boolean[] K = new boolean[65535]; // pressed keys
public M() {
final int WIDTH = 640;
final int HEIGHT = 480;
final int FRAMES_PER_SECOND = 60;
final long FRAME_PERIOD = 1000000000L / FRAMES_PER_SECOND;
Random random = new Random();
// Define model
final int BALLS = 5;
final int BALL_SIZE = WIDTH / 10;
final int PADDLE_WIDTH = BALL_SIZE * 2;
final int PADDLE_HEIGHT = HEIGHT / 20;
final int BALL_VELOCITY = 2;
final int PADDLE_VELOCITY = 10;
int[][] balls = new int[BALLS][4]; // [ball index][{x, y, Vx, Vy}]
for(int i = 0; i < BALLS; i++) {
balls[i] = new int[] {
random.nextInt(WIDTH - BALL_SIZE), // x
random.nextInt(HEIGHT - BALL_SIZE - PADDLE_HEIGHT), // y
random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vx
random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, // Vy
};
}
int paddleX = (WIDTH - PADDLE_WIDTH) / 2;
// Setup frame
JPanel panel = (JPanel)getContentPane();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
show();
// Create off-screen image for rendering
Image image = createImage(WIDTH, HEIGHT);
Graphics imageGraphics = image.getGraphics();
long nextFrameStart = System.nanoTime();
while(true) {
do {
// -- UPDATE MODEL BEGIN -------------------------------------------------------
// Move the balls
for(int i = 0; i < BALLS; i++) {
int[] ball = balls[i];
if (ball[0] >= WIDTH - BALL_SIZE) {
ball[2] = -BALL_VELOCITY;
} else if (ball[0] <= 0) {
ball[2] = BALL_VELOCITY;
}
if (ball[1] >= HEIGHT - BALL_SIZE - PADDLE_HEIGHT) {
ball[3] = -BALL_VELOCITY;
} else if (ball[1] <= 0) {
ball[3] = BALL_VELOCITY;
}
ball[0] += ball[2];
ball[1] += ball[3];
}
// Move the paddle
if (K[KeyEvent.VK_LEFT] && paddleX > 0) {
paddleX -= PADDLE_VELOCITY;
}
if (K[KeyEvent.VK_RIGHT] && paddleX < WIDTH - PADDLE_WIDTH) {
paddleX += PADDLE_VELOCITY;
}
// -- UPDATE MODEL END ---------------------------------------------------------
nextFrameStart += FRAME_PERIOD;
} while(nextFrameStart < System.nanoTime());
// -- RENDER FRAME BEGIN -------------------------------------------------------
imageGraphics.setColor(Color.BLACK);
imageGraphics.fillRect(0, 0, WIDTH, HEIGHT);
// Render balls
imageGraphics.setColor(Color.RED);
for(int i = 0; i < BALLS; i++) {
int[] ball = balls[i];
imageGraphics.fillOval(ball[0], ball[1], BALL_SIZE, BALL_SIZE);
}
// Render paddle
imageGraphics.setColor(Color.YELLOW);
imageGraphics.fillRect(
paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
// Draw to screen
Graphics panelGraphics = panel.getGraphics();
if (panelGraphics != null) {
panelGraphics.drawImage(image, 0, 0, null);
panelGraphics.dispose();
}
// -- RENDER FRAME END ---------------------------------------------------------
long remaining = nextFrameStart - System.nanoTime();
if (remaining > 0) {
try {
Thread.sleep(remaining / 1000000);
} catch(Throwable t) {
}
}
}
}
public void processKeyEvent(KeyEvent e) {
K[e.getKeyCode()] = e.getID() == 401;
}
public static void main(String[] args) {
new M();
}
}
The template is not a complete game. It displays 5 bouncing balls and a paddle that can be moved with the arrow keys. There is no logic to enable the balls and the paddle to interact. The ball coordinates and velocities are stored in an int array. Other entries have used java.awt.Rectangle for grouping 4 values together, but I used an array since it can store an arbitrary number of values. See the Game loop thread (http://www.java-gaming.org/forums/index.php?topic=15138.0) for an explation of frame skipping.
Once you compile your game to a class file, compress the class file with kzip and change the extension of the file to “jar”. FTP the jar to a web accessible folder and then add a jnlp file to make it web start launchable:
<?xml version="1.0" encoding="utf-8"?>
<jnlp
spec="1.5+"
codebase="http://www.yourdomainname.com/gamedirectory"
href="thisfile.jnlp">
<information>
<title>Your Game</title>
<vendor>Your Name</vendor>
<icon href="icon.gif"/>
<icon kind="splash" href="splash.gif"/>
</information>
<resources>
<j2se version="1.5+"/>
<jar href="thegame.jar"/>
</resources>
<application-desc main-class="M"/>
</jnlp>