Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why does this simple Java program work correctly under Windows but not Fedora 10?  (Read 1613 times)
0 Members and 1 Guest are viewing this topic.
Offline RageMatrix

Senior Newbie




Java games rock!


« Posted 2009-07-29 18:55:42 »

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);
      //theFrame.setCursor(getToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB),new Point(0,0),null));
     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. Sad
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2009-07-31 01:27:27 »

Construct your UI on the event dispatch thread with SwingUtilities.invokeLater

Offline RageMatrix

Senior Newbie




Java games rock!


« Reply #2 - Posted 2009-08-07 23:36:29 »

Can you explain that in a little more detail, please? Sorry!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GKW

Senior Member




Revenge is mine!


« Reply #3 - Posted 2009-08-08 03:19:07 »

Changes to the properties of a visible component must occur on the event dispatch thread.  You are creating and changing the properties of your canvas outside of the EDT.  As swpalmer points out, if you create your Universe object in the invokelater() method it will be run on the EDT.  Google swing and thread safety.
Offline RageMatrix

Senior Newbie




Java games rock!


« Reply #4 - Posted 2009-08-08 08:28:11 »

Okay, so I have read and understand the principle, but still don't see how this applies to my program as in the examples I've seen create JFrames and such in the main() method. So would I have to change my Universe object to include the SwingUtilties.invokeLater() method and move my initUniverse() method to there or can I include the SwingUtilities.invokeLater() method in my Game object and create my Universe instance there and if so, how will main() know to call the invokeLater() method and thus start the game?

Thanks for your patience.
Offline pjt33
« Reply #5 - Posted 2009-08-08 11:58:59 »

Okay, so I have read and understand the principle, but still don't see how this applies to my program as in the examples I've seen create JFrames and such in the main() method.
Sometimes you can get away with it, but not always. It's not even always consistent for the same program. The only safe thing to do is to construct it in the event thread.
Offline GKW

Senior Member




Revenge is mine!


« Reply #6 - Posted 2009-08-08 13:49:33 »

You can create swing components off of the EDT.  You just can't modify them after they've been realized off of the EDT.  The way your class is set up right now it realizes the canvas and modifies it in the constructor.  If you wish to keep this functionality then you must create your class on the EDT with invokeLater.  If you remove the initUniverse method from the constructor then you can create your universe object in the main method and then call initUniverse on the EDT with invokeLater.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Game {
        private static Universe theGame = null;

        public static void main(String[] args) {
                SwingUtilities.invokeLater( new Runnable() {
                        public void run() {
                                theGame = new Universe();
                        }
                });
        }
}
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #7 - Posted 2009-08-11 00:59:55 »

You can create swing components off of the EDT.  You just can't modify them after they've been realized off of the EDT. 

It is considered a bug to create Swing components off the EDT.  It used to be considered okay (if they were un-"realized"), but things changed.   You just aren't allowed to do it at all now. The problem is that you don't know what sort of listeners and things get called..   For example, simply adding components to a Swing hierarchy will fire events... so the hierarchy must be built on the EDT.

Offline GKW

Senior Member




Revenge is mine!


« Reply #8 - Posted 2009-08-11 04:17:11 »

Slap my ass and call me Shirley I didn't know that had changed.  Thanks for the heads up.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!