I have written the following little framework for a game I was hoping to write for Linux. This works fine with the same version of the JDK on Windows - a black screened window with a frames-per-second readout on the top left, but this fails to work under Fedora 10. I have installed Sun's official JDK and all I get is the Swing window which is the default grey and nothing else.
Little help?
Game.java
1 2 3 4 5 6 7
| public class Game { private static Universe theGame = null;
public static void main(String[] args) { theGame = new Universe(); } } |
Universe.java
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.Random; import javax.swing.JFrame;
public class Universe extends Canvas { private JFrame theFrame; private BufferStrategy bufferStrategy; private BufferedImage bufferedImage; private GraphicsEnvironment graphicsEnvironment; private GraphicsDevice graphicsDevice; private GraphicsConfiguration graphicsConfiguration; private Graphics graphics; private Graphics2D graphics2D; private Color colorBackground; private Random random; private static final int SCREEN_WIDTH = 640; private static final int SCREEN_HEIGHT = 480; private static final int SCREEN_DEPTH = 32; private static int fps; private static int frames; private static long totalTime; private static long currentTime; private static long lastTime; private boolean isRunning = false; private Font theFont = new Font("Courier New", Font.PLAIN, 12); private static boolean isFullScreen = false;
public Universe() { initUniverse(); }
private void initUniverse() { theFrame = new JFrame(); theFrame.setIgnoreRepaint(true); graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); graphicsDevice = graphicsEnvironment.getDefaultScreenDevice(); graphicsConfiguration = graphicsDevice.getDefaultConfiguration(); bufferedImage = graphicsConfiguration.createCompatibleImage(Universe.SCREEN_WIDTH, Universe.SCREEN_HEIGHT); if (!isFullScreen) { theFrame.add(this); theFrame.pack(); theFrame.setVisible(true); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theFrame.add(this); theFrame.pack(); theFrame.setVisible(true); theFrame.setSize(Universe.SCREEN_WIDTH, Universe.SCREEN_HEIGHT); } else { theFrame.setUndecorated(true); theFrame.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_ESCAPE ) isRunning = false; } }); graphicsDevice.setFullScreenWindow(theFrame); if(graphicsDevice.isDisplayChangeSupported()) { graphicsDevice.setDisplayMode(new DisplayMode(Universe.SCREEN_WIDTH, Universe.SCREEN_HEIGHT, Universe.SCREEN_DEPTH, DisplayMode.REFRESH_RATE_UNKNOWN));
} } theFrame.createBufferStrategy(2); bufferStrategy = theFrame.getBufferStrategy(); colorBackground = Color.BLACK; random = new Random(); setupFPS(); gameLoop(); }
private void setupFPS() { Universe.fps = 0; Universe.frames = 0; Universe.totalTime = 0; Universe.currentTime = 0; Universe.lastTime = 0; }
private void gameLoop() { isRunning = true; Universe.currentTime = System.currentTimeMillis(); while(isRunning) { try { Universe.lastTime = Universe.currentTime; Universe.currentTime = System.currentTimeMillis(); Universe.totalTime += Universe.currentTime - Universe.lastTime; if (Universe.totalTime > 1000) { Universe.totalTime -= 1000; Universe.fps = Universe.frames; Universe.frames = 0; } ++Universe.frames; clear(); updateInput(); updateLogic(); updateRender(); displayFPS(); flip(); } catch (Exception e) { System.out.println("ERROR:" + e.getMessage()); } finally { cleanUp(); } } isRunning = false; shutdownGame(); shutDownProgram(); }
private void shutDownProgram() { cleanUp(); if (isFullScreen) { graphicsDevice.setFullScreenWindow(null); } System.exit(0); }
private void shutdownGame() { }
private void updateInput() { }
private void updateLogic() { }
private void updateRender() { }
private void displayFPS() { graphics2D.setColor(Color.WHITE); graphics2D.setFont(theFont); graphics2D.drawString(String.format("FPS: %s", Universe.fps), 20, 20); }
private void clear() { graphics2D = bufferedImage.createGraphics(); graphics2D.setColor(colorBackground); graphics2D.fillRect(0, 0, Universe.SCREEN_WIDTH, Universe.SCREEN_HEIGHT); }
private void flip() { graphics = bufferStrategy.getDrawGraphics(); graphics.drawImage(bufferedImage, 0, 0, null); if (!bufferStrategy.contentsLost()) { bufferStrategy.show(); } Thread.yield(); }
private void cleanUp() { if (graphics != null) { graphics.dispose(); graphics = null; }
if (graphics2D != null) { graphics2D.dispose(); graphics2D = null; } }
public boolean isRunning() { return isRunning; } } |
The official Sun JDK is in the system path (/etc/profile) and I have compiled it using that and also uninstalled the OpenJDK stuff but to no avail. What the hell is going on? Any ideas? As I said, this code works perfectly under Windows with the same version of the JDK. I even ran "javac -version" and "java -version" to make sure no other Fedora-specific JDK was overriding the official one. But nothing helps.
