2

I want to integrate slick in JFrame. But I am facing following problem: 1. When I try to close frame, it stop slick game but not frame.

Please find my code as below:

CanvasGameContainer canvasGameContainer1 = new CanvasGameContainer(new MainGame("Game 1"));
canvasGameContainer1.setBounds(20, 20, 400, 400);
newFrame = new JFrame("With JFrame");
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
newFrame.add(canvasGameContainer1);
newFrame.setVisible(true);
canvasGameContainer1.start();

I have posted same on slick forum but no solution. here is a link for same...

2 Answers2

1

I know this is an old post, but in case some people are still struggling with the frame not closing, here is a solution that works on Windows:

  • set your frame defaultCloseOperation to DISPOSE_ON_CLOSE
  • in the WindowClosing event of your frame, dispose the GameContainer
  • in the WindowClosed event of your frame, calll System.exit(0)
ppetrov
  • 3,077
  • 2
  • 15
  • 27
0

I don't know why you need to wrap it in a JFrame. Are you also adding menus to the app (instead of rendering them inside the game container, for example)? I guess my question is, why are you wrapping it in a JFrame to start with - maybe there's a specific use case I'm not thinking of? Can you post a screenshot by chance?

I think mixing the windowing kits might be problematic. I just create an instance of an AppGameContainer, and set the fullscreen option to false. As a result, Slick just gives me a window with a close button. When I click the close button, the app closes normally.

I've only officially tested this on Mac OS X 10.6, with JDK 6, but theoretically it should work elsewhere.

Here's the file and code where I initialize my container:

https://github.com/normalocity/pedestrians/blob/master/src/StartPedestrians.java

AppGameContainer container = new AppGameContainer(pedSim);
container.setDisplayMode(800, 600, false);
jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Actually I have ready made Swing application. I have to optimize 2D rendering part of it. That is why I want it to render inside JFrame. No I don't have to render menus inside the game container. I am using mouse and keyboard listeners of game container. – Mahendra Korat Feb 23 '12 at 16:26
  • Ah, I see. Well, I would just abandon the Swing interface, if I were you, for games. Slick pretty much has everything you need to just get a simple surface to draw in, inside or outside of a window. While it's possible to build a game in Swing, I don't tend to recommend it for a few reasons, as outlined in the answer on this question: http://stackoverflow.com/questions/7781040/java-graphics-stages-of-a-game – jefflunt Feb 23 '12 at 17:59
  • @normalocity, I work with Mahendra, We are having an enterprise application, and we are currently using core Java2D for a navigational top view of XYZ facility, which very simple (just have panning and zooming) but for making it more interactive (clicks on objects / optimized images) we selected Slick and did a prototype, Jframe containing this app is having lot of functionality. like showing information, forms etc, as any enterprise (also boring) app. – Nachiket Feb 24 '12 at 04:46
  • 1
    Hmm. that's the first time I've heard of anyone using slick or enterprise. cool. – jefflunt Feb 24 '12 at 12:37