Java Game Issue

Started by
2 comments, last by rip-off 12 years, 10 months ago
Hello!

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)
frame.setTitle(title);
}

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.pack();
frame.setBounds(0, 0, width+6, height+28);
frame.setTitle(title);
frame.setVisible(true);
keyboard = new Keyboard();
mouse = new Mouse();

/*
frame = new JFrame();
setSize(width, height);
frame.getContentPane().add(this);
//frame.setUndecorated(true);
frame.pack();
//frame.setSize(width, height);
frame.setTitle(title);
frame.setVisible(true);
keyboard = new Keyboard();
mouse = new Mouse();
*/

// tell java "i got this frame, lemme paint it meself"
setIgnoreRepaint(true);
frame.setResizable(false);

// plug in key listener
frame.addKeyListener(keyboard);
addKeyListener(keyboard);
// add window listener
frame.addWindowListener(new GameWindowListener());
// add mouse listener
this.addMouseListener(mouse);
this.addMouseMotionListener(mouse);

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

requestFocus();

createBufferStrategy(2);
strategy = getBufferStrategy();

if (callback != null)
callback.initialize();

gameLoop();
}

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())
continue;
// Get hold of a graphics context for the accelerated
// surface and blank it out
g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, width, height);

if (callback != null) {
callback.frameRendering();
}

// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
}

}

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);
}
}



Thanks!
Skoob

Also, sorry if this post really belongs elsewhere, I really can't tell.
Advertisement
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.
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?
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.

This topic is closed to new replies.

Advertisement