Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to make like a sort of main menu (in GUI).
When the user clicks the start button, the screen transition into another "screen" (JPanel). This image will make it easier to understand.
http://i.imgur.com/Cfdry.png

Currently, I have a MainMenu extends JPanel and that gets added into a driver class with a JFrame. I can't figure how to switch to another class like Game extends JPanel.
So when the user clicks the start button in MainMenu, I want it to somehow hide itself and the Game to show itself.
Thanks.

share|improve this question
add comment

3 Answers

Something like that?

enter image description here enter image description here

I made the SlidingLayout library for that purpose. It's a very small and easy-to-use library which aim is to create smooth transitions between two layouts of some components. Thus, making a transition between two screens is very easy to do.

The difference between the two transitions shown in these gifs relies on two lines of code. You can also create more fancy transitions by applying a different delay on each component, so they appear not all at once but with some timing variations between them.

I hope it may be useful to you :)

share|improve this answer
add comment

If you just want a different JPanel to be displayed when the user clicks on the menu then you should give your JFrame's content pane a java.awt.CardLayout. You then add your MainMenu and Game as layout components. Whenever you want to switch between the two, you can just call the next method of your CardLayout after you detected a click.

MainMenu mainMenu = new MainMenu();
Game game = new Game();

JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
CardLayout cardLayout = new CardLayout();

contentPane.setLayout(cardLayout);
contentPane.add(mainMenu, "MainMenu");
contentPane.add(game, "Game");

frame.setSize(600, 400);
frame.setVisible(true);

while (true) {
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }

    cardLayout.next(contentPane);
}

However, this will not give you a smooth animated transition, it just flips from one JPanel to another one.

share|improve this answer
add comment

Fancy animations between panels isn't really recommended for Swing layouts, as you'll have to run all the offsets and positions yourself, running an extra thread to update them and send repaint requests.

I'd recommend writing your own UI, as that would provide you with the most flexibility for designing your interface.

If you'd rather use Swing, I highly recommend writing your own layout manager.
(it's not that hard, really, though you'll have to make sure that it interacts with a thread somewhere for the animations to work)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.