Jump to content
  • Advertisement
Sign in to follow this  

Java Game Issue

This topic is 2570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


So I have this game I made for a game design class, and I would like some help troubleshooting a bug that is not uncommon but quite nebulous to me.

The game is programmed in java using an engine I built from a number of different tutorials around a year back. The issue I am experiencing is that on certain machines, nothing will display. The code is running, and if you know where the buttons are, you can even get into the first level and play through it, but nothing displays (you just get a black window). I don't have direct access right now to any of the machines that I have found to experience this, so here I am doing some theorybuilding and asking for help.

It seems to me that its likely my sloppy Exception handling is causing this, as all/nearly all of my catch blocks catch (Exception e). If sometimes the graphics for whatever reason cannot be initialized, then nothing would ever get drawn, though everything else would still run as intended. The thing is I can't really see where such an exception might be getting caught.

Ignoring that, there must likely be something going on in my GameWindow class that I'm not catching. At any rate, any comments would be appreciated, both on the nature of the issue I'm having, and just on the game in general. You can find a video of the game here and a mediafire download link here.

Here is the previously mentioned GameWindow class (sorry for length, i can't find the equivalent of spoiler tags to hide this):
public class GameWindow extends Canvas {
private static final long serialVersionUID = 1L;
private GameWindowCallback callback;
private int width;
private int height;
private String title;
private Keyboard keyboard;
private Mouse mouse;
public boolean gameRunning = true;
private JFrame frame;
private BufferStrategy strategy;
private Graphics2D g;
public long lastRender;

public GameWindow() {

public void setTitle(String newTitle) {
title = newTitle;
if (frame != null)

public void setResolution(int x, int y) {
width = x;
height = y;
setSize(width, height);

public void startRendering() {
frame = new JFrame();
Container pane = frame.getContentPane();
//setPreferredSize(new Dimension(width, height));
pane.add(this, FlowLayout.LEFT);
//pane.setBounds(0, 0, width, height);
frame.setBounds(0, 0, width+6, height+28);
keyboard = new Keyboard();
mouse = new Mouse();

frame = new JFrame();
setSize(width, height);
//frame.setSize(width, height);
keyboard = new Keyboard();
mouse = new Mouse();

// tell java "i got this frame, lemme paint it meself"

// plug in key listener
// add window listener
frame.addWindowListener(new GameWindowListener());
// add mouse listener

// hide the mouse
// windowXORthis.setCursor(GameCursor.getTransparentCursor() );


strategy = getBufferStrategy();

if (callback != null)


public Graphics2D getDrawGraphics() {
return g;

public GameWindowCallback getGame() {
return callback;

public void setGameWindowCallback(GameWindowCallback newCallBack) {
callback = newCallBack;

public boolean isKeyPressed(int KeyCode) {
return keyboard.isKeyPressed(KeyCode);

public boolean isButtonPressed(int ButtonCode) {
return mouse.isPressed(ButtonCode);

public int mouseX() {
return mouse.mouseX();

public int mouseY() {
return mouse.mouseY();

private void gameLoop() {
while (gameRunning) {
if(lastRender + 10 > System.currentTimeMillis())
// Get hold of a graphics context for the accelerated
// surface and blank it out
g = (Graphics2D) strategy.getDrawGraphics();
g.fillRect(0, 0, width, height);

if (callback != null) {

// finally, we've completed drawing so clear up the graphics
// and flip the buffer over


public Rectangle getBounds() {
return new Rectangle(0,0,width, height);

public boolean hasButtonPressed(int button1) {
return mouse.hasKeyBeenPressed(button1);

public boolean hasKeyPressed(int button1) {
return keyboard.hasKeyBeenPressed(button1);


Also, sorry if this post really belongs elsewhere, I really can't tell.

Share this post

Link to post
Share on other sites
Add (at the very least) logging to your exception handlers. Use exception.printStackTrace() to the log (or use something like log4j which has supports logging exceptions). Send such a version to someone who has reported this error. Get them to give you the log file.

Share this post

Link to post
Share on other sites
That's a very good point. Currently my catch blocks are something like "e.printstacktrace(); System.exit(-1);". I will be adding your suggestion in the near future.

On that note, though, is there any way that someone running a jar can see System.out text?

Share this post

Link to post
Share on other sites
Starting the program from the console I imagine. Copying output from a windows console is a little annoying though.

You could redirect this to a file. E.g:

> cd C:\path\to\game
> java -jar Game.jar /* other params */ > log.txt 2>&1

Now log.txt (in the current directory) will contain anything printed to System.out or System.err.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!