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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class M extends JFrame { boolean[] K = new boolean[65535]; 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(); 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]; for(int i = 0; i < BALLS; i++) { balls[i] = new int[] { random.nextInt(WIDTH - BALL_SIZE), random.nextInt(HEIGHT - BALL_SIZE - PADDLE_HEIGHT), random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, random.nextBoolean() ? -BALL_VELOCITY : BALL_VELOCITY, }; } int paddleX = (WIDTH - PADDLE_WIDTH) / 2; JPanel panel = (JPanel)getContentPane(); panel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); show(); Image image = createImage(WIDTH, HEIGHT); Graphics imageGraphics = image.getGraphics(); long nextFrameStart = System.nanoTime(); while(true) { do { 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]; } if (K[KeyEvent.VK_LEFT] && paddleX > 0) { paddleX -= PADDLE_VELOCITY; } if (K[KeyEvent.VK_RIGHT] && paddleX < WIDTH - PADDLE_WIDTH) { paddleX += PADDLE_VELOCITY; } nextFrameStart += FRAME_PERIOD; } while(nextFrameStart < System.nanoTime()); imageGraphics.setColor(Color.BLACK); imageGraphics.fillRect(0, 0, WIDTH, HEIGHT); 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); } imageGraphics.setColor(Color.YELLOW); imageGraphics.fillRect( paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT); Graphics panelGraphics = panel.getGraphics(); if (panelGraphics != null) { panelGraphics.drawImage(image, 0, 0, null); panelGraphics.dispose(); } 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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> |