I recently started learning Java and was super excited to build something fun a simple game window using Java’s Canvas
and JFrame
. It felt like I was stepping into the world of game development. Everything seemed to go well until I ran the program and saw this dreaded error:
Error Code
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
At first, I had no idea what this meant. It didn’t tell me much except that something was wrong in the code. After some digging (and a lot of trial and error), I discovered a tiny mistake that caused it all. Here’s how I fixed the error and used the experience to add cool new features that made the project more exciting.
The Working Java Code
package Happily.Insane.Rain;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9; // FIXED: Added 'int'
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
System.out.println("Game started.");
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
System.out.println("Stopping game...");
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.currentTimeMillis();
int frames = 0;
while (running) {
System.out.println("Running...");
frames++;
if (System.currentTimeMillis() - lastTime >= 1000) {
System.out.println("FPS: " + frames);
frames = 0;
lastTime += 1000;
}
try {
Thread.sleep(16); // ~60 FPS
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Game stopped.");
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Rain - " + width * scale + "x" + height * scale); // 🪟 Display dimensions
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
What Was the Error and How Did I Fix It?
The problem was with this line:
public static height = width / 16 * 9;
Java threw a compilation error because I forgot to declare the data type of the variable height
. Unlike Python or JavaScript, Java requires you to explicitly state types like int
, double
, or String
.
Fix Version:
public static int height = width / 16 * 9;
Once I made this small fix, the program compiled and launched the window as expected.
Adding More Practice Functionality
After fixing the error, I didn’t want to stop there. I figured I could learn more by improving the program:
- Added window dimensions to the title bar
- Simulated a frame rate counter (FPS)
- Printed clear start and stop messages
- Controlled the loop using
Thread.sleep()
to mimic real-time frame updates
This helped me understand how a basic game loop works running continuously, printing updates, and handling timing.
What I Learned Through This Experience
Pay attention to your syntax, especially when using a strongly typed language like Java. The compiler doesn’t always give helpful messages, but with patience and curiosity, I figured it out.
Here are my key takeaways:
- Always declare variable types in Java no shortcuts.
- Use
System.out.println
often to debug and observe behavior. Thread.sleep()
is your friend when simulating time and frames.- Keep building and experimenting. Every improvement deepens your understanding.
Final Thought
Starting with Java game development was intimidating at first, especially when cryptic errors popped up. But fixing that single line not only solved the problem it opened the door to understanding threads, game loops, and UI basics. The process of debugging, improving, and experimenting turned frustration into motivation.