JavaFX/Eclipse: Java(TM) Platform SE binary not responding

Hi guys

This is my first post. I’ve recently(1 year ago) started with java and am moving on into simple game development. JavaFX is the windowing tool to go with right now, so i decided to use it. Sadly, when I run my code in Eclipse, the program is not responding . After a few clicks, the message “Java™ Platform SE binary not responing”. It works if I comment out the whole while(isRunning)-part… Any ideas where i went wrong?


import java.util.ArrayList;
import java.util.Iterator;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

public class Game extends Application 
{
	private Stage window = new Stage();
	
	private static int width = 0;
	private static int height = 0;
	
	private static int FRAMES_PER_SECOND = 30;
	private static double TIME_PER_FRAME = 1000 / FRAMES_PER_SECOND;
	
	private boolean isRunning = true;
	
	ArrayList<String> input = new ArrayList<String>();
	ArrayList<Sprite> moneybagList = new ArrayList<Sprite>();
	Sprite briefcase;
	

	
	public Game()
	{
		//setScreen(width,height);
	}
	
	public static void setScreen(int mwidth, int mheight)
	{
		width = mwidth;
		height = mheight;
	}
	
    public void start(Stage stage) 
    {
    	this.window = stage;
        window.setTitle("Window Demo");
        window.setResizable(false);
        
        Group root = new Group();
        Scene scene = new Scene(root, width, height);
        window.setScene(scene);
               
        Canvas canvas = new Canvas(width, height);
        root.getChildren().add(canvas);
             
        GraphicsContext gc = canvas.getGraphicsContext2D();
        
        briefcase = new Sprite("briefcase.png");
        
        //scene.setOnKeyPressed(new KeyboardHandler());
                
        while(isRunning)
        {
	        long startMiliTime = System.nanoTime() / 1000000;
	    	
	    	//HANDLE EVENTS
	    	handleEvents();
	    	
	    	//UPDATE OBJECTS
	    	update();
	    	
	    	Iterator<Sprite> moneybagIter = moneybagList.iterator();
	        while (moneybagIter.hasNext())
	        {
	            Sprite moneybag = moneybagIter.next();
	            if (briefcase.intersects(moneybag))
	            {
	                moneybagIter.remove();
	            }
	        }
	        
	        //moneybag.setOnAction()
	        
	        //CLEAR CANVAS
	    	gc.clearRect(0, 0, width, height);
	    	
	    	//RENDER OBJECTS	    	 	    	
	    	render(gc);	    	
	    	
	    	//CAP FRAME RATE IF NECESSARY
	    	long endMiliTime = System.nanoTime() / 1000000;
	    	double delta = endMiliTime - startMiliTime;	
	    	double miliTimeToSleep = (TIME_PER_FRAME - delta);
	    	
	    	int currentFPS = (int) Math.round(1000 / delta);
	    	String sFPS = "";
	    	
	    	if (miliTimeToSleep <= 0) //FPS lower than 60
	    	{
	    		continue;
	    	}
	    	else
	    	{
	    		try 
		    	{
					Thread.sleep((long)miliTimeToSleep);
				} 
		    	catch (InterruptedException e) 
		    	{
					e.printStackTrace();
				}
	    	}
	    	sFPS = Integer.toString(currentFPS);
	    	gc.fillText("FPS " + sFPS,10.0,10.0);
            gc.strokeText( "FPS " + sFPS, 10, 10);
            
            if (delta >= 10000)
            {
            	isRunning = false;	
            }
	    	
        	window.show();

        }
        
        
        
        //DELETE ALL SPRITES AND CLOSE WINDOW
        cleanUp();
        	
    }
    
    public void handleEvents(){}

    public void update()
    {
    	for (int i = 0; i < 15; i++)
    	{
    	    Sprite moneybag = new Sprite();
    	    moneybag.setImage("moneybag.png");
    	    double px = 350 * Math.random() + 50;
    	    double py = 350 * Math.random() + 50;          
    	    moneybag.setPosX(px);
    	    moneybag.setPosY(py);
    	    moneybagList.add(moneybag);
    	}    	
    }
    
    public void render(GraphicsContext gc)
    {
    	briefcase.render(gc);
    	
    	for (Sprite moneybag : moneybagList)
    	{
            moneybag.render(gc);
    	}    	
    }
    
    public void cleanUp()
    {
    	for(int i = 0; i < moneybagList.size(); i++)
    	{
    		moneybagList.remove(i);
    	}
    	window.close();
    	Platform.exit();
    }
}

class KeyboardHandler implements EventHandler<KeyEvent>
{
	ArrayList<String> input = new ArrayList<String>();
	
	KeyboardHandler()
	{
		System.out.println("Created a KeyboardHandler...");
	}

	@Override
	public void handle(KeyEvent e) 
	{
	    if (e.getEventType() == KeyEvent.KEY_PRESSED)
		{
		    String code = e.getCode().toString();
			if (!input.contains(code))
			{
	            input.add(code);
			}
		}
	    else if (e.getEventType() == KeyEvent.KEY_RELEASED)
	    {
	    	String code = e.getCode().toString();
            input.remove(code);
	    }
		
	}
} 

If you need the Sprite/Main-file just tell me. The game is executed in main as

Game.launch(Game.class);

Any help appreciated.

Greez Half_NO_oB ;D

I think what is going on is that the start() method wants to complete before it will display. Having your game loop as a while() construction delays that moment indefinitely.

The easiest way to handle a game loop is to use an AnimationTimer(). You can define and start the timer prior to the end of the start() method. You can check out this Java-Gaming tutorial , Section III “Basic Game Loop” for an example.

Hi philfrei

As you can see, im very new on this website :smiley:

This really seems to be a really interesting place. Thanks for the tip, this was all I was looking for. If i don’t repost, it’s fixed 8)

EDIT: It’s working now, i just replaced the whole while(isRunning)-Loop in the start() with an AnimationTimer as suggested in your example. It has few other bugs that I have to fix though. :wink: