LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2009, 02:47 PM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
Why does this simple Java program work correctly under Windows but not Fedora 10?


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
Code:
public class Game {
        private static Universe theGame = null;

        public static void main(String[] args) {
                theGame = new Universe();
        }
}
Universe.java
Code:
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. :(
 
Old 07-29-2009, 04:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Now if only you told people in what way it is failing ... maybe
you'd get some response?
 
Old 07-29-2009, 05:16 PM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by Tinkster View Post
Now if only you told people in what way it is failing ... maybe
you'd get some response?
If you re-read my post, you'll see that I did: -

"I have installed Sun's official JDK and all I get is the Swing window which is the default grey and nothing else."

Thanks for your reply. The above is exactly what happens as opposed to the black background with a frames-per-second readout on the top left like I get on the Windows version. No Exceptions occur.
 
Old 07-29-2009, 06:54 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Ooops ... i apologise.

Oddly enough I can't reproduce the issue, or rather: I get the same
issue in Linux and windows.


Cheers,
Tink
Attached Thumbnails
Click image for larger version

Name:	windows.jpg
Views:	23
Size:	142.3 KB
ID:	1103   Click image for larger version

Name:	game_linux.jpg
Views:	22
Size:	63.2 KB
ID:	1104  
 
Old 07-29-2009, 08:07 PM   #5
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,989

Rep: Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447
is there a SELinux warning ????

did you give the java program the correct SE context ??
what is in the error / se log ?\
look in
/var/log/setroubleshoot/ ???
/var/log/messages ?

and WHAT is the error from the terminal when you try to run it ?
 
Old 07-29-2009, 09:42 PM   #6
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by John VV View Post
is there a SELinux warning ????

did you give the java program the correct SE context ??
what is in the error / se log ?\
look in
/var/log/setroubleshoot/ ???
/var/log/messages ?

and WHAT is the error from the terminal when you try to run it ?
No errors in either of those logs, no exceptions thrown in the console and no SELinux warnings.

The program should simply display a Swing window with a black background and loop continuously while giving the frames per second read out of the double-buffering in the top left of the window.However, under Fedora, it simply diplays a default Swing window with the default grey background and no FPS readout.

What do you mean by giving the correct SE context?

Curious that the other poster could replicate the error on both operating systems, works fine on my Windows box.

Any other ideas? I'm truly stumped - it's not exactly a complex Java program.
 
Old 09-05-2014, 05:29 PM   #7
rjaduthie
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
From: fivedots.coe.psu.ac.th/~ad/jg/index.html [Scroll down to Java3D section.]

Quote:
Important Note: If you're using Java 3D with Java 7, then you need to need to set the following property at the start of your application:

System.setProperty("sun.awt.noerasebackground", "true");

This prevents the mixing of heavyweight (Canvas3D) and lightweight Swing components in JRE 7 from causing redraw problems (i.e. the Java 3D canvas is sometimes drawn as a blank gray rectangle).
You might have already sorted the problem. If so, this might help someone else! [It might not be the solution, however...]

Last edited by rjaduthie; 09-05-2014 at 05:29 PM. Reason: Formatting clarity
 
1 members found this post helpful.
Old 09-05-2014, 09:22 PM   #8
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,989

Rep: Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447Reputation: 2447
rjaduthie
seeing as this OLD thread is from 2009 , and for fedora 10 .
a version that is 10 versions and 5 YEARS dead and out of date....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unrecognized class file version : when running simple Java program yuubouna Linux - Newbie 1 11-28-2008 12:25 AM
Java programs either don't work correctly or not at all Firefox Latest Old_Fogie Linux - Software 2 05-09-2006 04:30 PM
Compiling a simple C++ program for Windows Baix Programming 2 03-05-2005 08:17 PM
java, simple program help needed.. marlor Programming 1 11-04-2004 03:40 PM
How does the gcc-java program work? vdogvictor Programming 4 06-23-2004 12:40 AM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration