Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Changing from a screen to another!

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing from a screen to another!

    Hello!

    Iīm making a simple game which consists of a Menu-screen with some images and a go-button. If you click on the go-button with the mouse you will be taken to the Game-room where there are some simple gameplay (a simple take on Space Invaders).

    My question is how to go from the Menu-screen to the Game-screen (and vice versa) in an efficient and rather easy way. The rooms are painted on JPanels which are placed on a JFrame.

    Thankful for help!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Sounds like a job for CardLayout!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Changing from a screen to another!

    You can keep JPanels in memory and switch them when you want to.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by codesmuggler View Post
    You can keep JPanels in memory and switch them when you want to.
    Or you can have CardLayout do that for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Changing from a screen to another!

    You can place one button on both screens. By pressing that button, hide the current screen and open up the other one.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by Mr.777 View Post
    You can place one button on both screens. By pressing that button, hide the current screen and open up the other one.
    That's what the OP is asking how to do. The answer is to use CardLayout.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Every tutorial Iīve found about CardLayout just demonstrates its use within one class that has some JPanels set up there. I donīt get how I do to integrate my room-objects with CardLayout. Please Help!

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by gargamel7 View Post
    Every tutorial Iīve found about CardLayout just demonstrates its use within one class that has some JPanels set up there. I donīt get how I do to integrate my room-objects with CardLayout. Please Help!
    I don't know what your Room Objects are, so I can't really help you. But the general idea is that you'd have one main class that sets up the CardLayout, and from there you'd instantiate whatever other classes you want and add their JPanels to the CardLayout.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    The room-objects are a menu class and a game class, both are extending JPanel.


    In the Menu-room there are a go-button (think of the go-button as a JPanel). I need to add this JPanel called p to the Main class or else it wonīt work to click the go-button, how do I add it?

    And should I instantiate it like this:
    Menu menu = new Menu();

    or like this:

    JPanel menu = new Menu();

    ?

    Here is the Main-class:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
     
    public class Main extends JFrame implements Commons {
     
    	public Menu menu;
    	public Game game;
     
    	private JPanel cardPanel;
            private CardLayout cl;
     
     
     
    	public Main(){
    		Menu menu = new Menu(this);
    		JPanel game = new Game();
    		cardPanel = new JPanel();
     
    		setTitle("SpaceBanana");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(BOARD_WIDTH, BOARD_HEIGTH);
    	    setLocationRelativeTo(null);
    	    setVisible(true);
    	    setResizable(false);
     
    		cl = new CardLayout();
            cardPanel.setLayout(cl);
     
            cardPanel.add(menu, "menuscreen");
            cardPanel.add(game, "gamescreen");
     
     
     
            getContentPane().add(cardPanel);
     
     
    	}
     
    	public void change() {
    		System.out.println("d");
    		cl.show(cardPanel, "" + (game));
     
    	}
     
     
     
    	public static void main(String[] args){
    		Main main = new Main();
     
     
     
     
    	}
    }

  10. #10

    Default Re: Changing from a screen to another!

    If you don't want to hide the other rooms while the program is running, then a TabbedPane is a good solution, too.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by kenster421 View Post
    If you don't want to hide the other rooms while the program is running, then a TabbedPane is a good solution, too.
    Thanks for the tip, but this is just about common screenswitching thatīs seen in most games.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by gargamel7 View Post
    The room-objects are a menu class and a game class, both are extending JPanel.


    In the Menu-room there are a go-button (think of the go-button as a JPanel). I need to add this JPanel called p to the Main class or else it wonīt work to click the go-button, how do I add it?
    The code you have seems to add it just fine. What isn't working, exactly?


    Quote Originally Posted by gargamel7 View Post
    And should I instantiate it like this:
    Menu menu = new Menu();

    or like this:

    JPanel menu = new Menu();

    ?
    What happened when you tried both?


    Quote Originally Posted by gargamel7 View Post
    Here is the Main-class:
    Althought that's not an SSCCE, it does seem reasonable to me. What's your question now?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    The code you have seems to add it just fine. What isn't working, exactly?
    The code combiles without errors so itīs working, but it isnīt done. I wanted to know if everything was correctly done, and not full of code-bits that was unnecessary.

    Now my next question is how to add the JPanel p (which enables clicking the go-nutton in the menu). I thought that it would be best to add it in the menu class but I couldnīt make it work, so I added add(menu.p); in the constructor of main class.

    public Main(){
    Menu menu = new Menu(this);
    Game game = new Game();
    cardPanel = new JPanel();

    add(menu.p);
    setTitle("SpaceBanana");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(BOARD_WIDTH, BOARD_HEIGTH);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    cl = new CardLayout();
    cardPanel.setLayout(cl);



    cardPanel.add(menu, "menuscreen");
    cardPanel.add(game, "gamescreen");



    getContentPane().add(cardPanel);


    }

    So now it works to click the button but I would like to add the JPanel p right into the menu class itself, since it seems to be the best choice.

    Here is the menu class (in which Iīve tried to add p):

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
     
    public class Menu extends Screen implements ActionListener, MouseListener, Commons{
     
    	public static int points = 0;
    	public int xMouse;
    	public int yMouse;
        private Timer timer;
        private Image image;
        private String backMenu = "backMenu.png";
        private Go go;
        private Logo logo;
        JPanel p = new JPanel();
        public Main main;
     
     
     
     
    	public Menu(Main main){
     
    		this.main = main;
     
            timer = new Timer(5, this);
            timer.start();
     
            ImageIcon ii = new ImageIcon(getClass().getResource(backMenu));
    		image = ii.getImage();
    		go = new Go();
    		logo = new Logo();
    		setVisible(true);
    		add(p);
     
    		p.addMouseListener(this);
    	    p.setVisible(true);
     
     
     
     
     
    	}
     
    	public void paint(Graphics g) {
     
    	    super.paint(g);
    	    drawBackGame(g);
    	    drawOther(g);
     
    	}
     
     
    	public void drawBackGame(Graphics g) {
    		g.drawImage(image,0,0, this);	
    	}
     
    	public void drawOther(Graphics g) {
    		g.drawImage(go.getImage(),go.getX(),go.getY(), this);
    		g.drawImage(logo.getImage(),logo.getX(),logo.getY(), this);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    	    repaint();;
    	}
     
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		if ((e.getX() > go.getX()) && (e.getX() < go.getX()+go.getWidth())){
    		if ((e.getY() > go.getY()) && (e.getY() < go.getY()+go.getHeight()))
     
    		{
    			main.change();
     
     
     
    		}
    		}
     
     
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    }

    What happened when you tried both?
    The only difference i could catch was that when using "JPanel menu = new Menu()" instead of "Menu menu = new Menu();" was that I couldnīt add "add(menu.p);.




    Althought that's not an SSCCE, it does seem reasonable to me. What's your question now?
    How do I add p in the right way so that the change() can be called and how do I switch screens using the CardLayout ( is this really enough: cl.show(cardPanel, "" + game); )?

    And, when I add p through add(menu.p); in the main class and the application is running and I click on the go-button, only the println is executed of:

    public void change() {
    		System.out.println("fd");
    		cl.show(cardPanel, "" + game);
    	}

    No screen is changing at least!

    Thankful for an answer!
    Last edited by gargamel7; October 6th, 2011 at 08:17 AM.

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    JPanel cards are shown by Strings. Each JPanel has to have a unique String identifier (that can be "panel1", "panel2", or "startPanel", "gamePanel", etc, whatever you want). I'm not sure what you're doing by trying to add the JPanel to the String.

    If you want more help, you'll have to post an SSCCE and get rid of all that extra stuff. Just a program that has two JPanels that you can switch back and forth between. No painting code or game logic. Follow the tutorial I gave you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    What tutorial did you give me?

    The cl.show(cardPanel, "" + game); came from cl.show(cardPanel, "" + (currentCard)); in this tutorial:
    Java Tips - How to use AWT CardLayout

    Since we are very close to solve this case I think itīs a bit unnecessary to make an SSCCE. I just want two questions answered (quite easy questions, i presume). How to add the JPanel p and how to make the switching in the change() method.

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Ah, sorry if I didn't post it before, but it's the first result for googling "Java CardLayout": How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Thanks! I followed the tutorial and made the switching work. But. When the screen is switched from menu-room to game-room, user inputs donīt work in game-room. Everything is painted correctly, rotating things also, but again, user inputs dont work. Do you see what problem it may be? Like focus-loss or something?

  18. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Hmm I'm not sure without seeing the SSCCE. You could try calling requestFocusInWindow() on the JPanel in question. Does it work without the CardLayout, if you just add it to another JPanel directly? Does it work if you start it out as the default visible card?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Nothing happened when adding requestFocusInWindow() in the game-class-constructor. Yes it works without the CardLayout and yes, it works as the default visible card.

    Hereīs the game-class. Itīs modified though, I deleted everything irrelevant.

    import a lot of stuff
     
    public class Game extends Screen implements ActionListener, Commons{
     
    	public static int points = 0;
        private Timer timer;
        private Banana banana;
        private Player player;
        private Apple apple;
        private Image image;
        private String backGame = "backGame.png";
        private ArrayList apples;
     
     
    	public Game(){
            addKeyListener(new TAdapter());
            setFocusable(true);
            player = new Player();
            apple = new Apple(-500,0);
            banana = new Banana();
            timer = new Timer(5, this);
            timer.start();
     
            ImageIcon ii = new ImageIcon(getClass().getResource(backGame));
    	image = ii.getImage();
    	}
     
     
     
    	public void actionPerformed(ActionEvent e) {
    	    Player.move();
    	    repaint();
    	    bort();
    	    checkCollisions();
    	}
     
     
     
    	private class TAdapter extends KeyAdapter {
     
     
            public void keyReleased(KeyEvent e) {
                player.keyReleased(e);
            }
     
            public void keyPressed(KeyEvent e) {
                player.keyPressed(e);
            }
        }
     
    }
    Last edited by gargamel7; October 6th, 2011 at 12:56 PM.

  20. #20
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Changing from a screen to another!

    I have a work in progress a game with the the same functionality you are looking for. What I am doing is just working with images. So I have fore example, a method

    public BufferedImage makeFrame()
    {
        BufferedImage finalImg = new BufferedImage(mapWidth*16, mapHeight*16, 1);
        finalImg.createGraphics().drawImage(iT.toImage(gameImage), 0, 0, observer);//gameImage is a static Buffered Image of the background
        //more finalImg.createGraphics() for anything that changed
        return finalImg;
    }

    I then make finalImg an icon, resize it as necessary, and set it as my gamePanel icon, and do gamePanel.revalidate().

    note: I also have a makeHud() method, and after calling makeFrame() and makeHud(), I paint both to another image that I set as the panel icon.

    I hope this helps.

  21. #21
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Thanks for the tips Spidey! Actually, I have fixed the screen-switching issue. The thing left is that the game-room donīt listen to user inputs. Anyone who may know what the problem is?

  22. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by gargamel7 View Post
    Thanks for the tips Spidey! Actually, I have fixed the screen-switching issue. The thing left is that the game-room donīt listen to user inputs. Anyone who may know what the problem is?
    If you post an SSCCE, I'll play around with it. The code you posted contains classes that I don't have, which might be irrelevant to the problem anyway.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #23
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing from a screen to another!

    Hereīs an SSCCE:
    http://www.filedropper.com/spacesscce

    Thanks for helping!
    Last edited by gargamel7; October 8th, 2011 at 07:37 AM.

  24. #24
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Changing from a screen to another!

    Quote Originally Posted by gargamel7 View Post
    Hereīs an SSCCE:
    http://www.filedropper.com/spacesscce

    Thanks for helping!
    An SSCCE should be something you can copy and paste into (and out of) the forums. A lot of people (myself included) are accessing the site from work or school, so they might be behind a firewall that blocks sites like that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  2. Changing my GUI help
    By Xrrak in forum AWT / Java Swing
    Replies: 13
    Last Post: August 10th, 2011, 04:11 PM
  3. changing GUI
    By timmin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 3rd, 2011, 09:16 AM
  4. Screen Resolution
    By bartonn in forum Member Introductions
    Replies: 2
    Last Post: December 16th, 2010, 06:49 AM
  5. clear screen.
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 5th, 2009, 07:27 AM