Results 1 to 10 of 10
  1. #1
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default Trouble understanding CardPanel

    Basically I think I understand what I am doing wrong here, however I do not understand the correct way to do what I want to do. I am trying to create a simple game and I have already created a little bit of a working game with a character I can move around with simple collision detection and whatnot, however when I tried to create a main screen before the game it stopped working.

    I am getting
    Exception in thread "main" java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(Unknown Source)
    at java.awt.CardLayout.show(Unknown Source)
    at CastleGame.MainGame.<init>(MainGame.java:34)
    at CastleGame.MainGame.main(MainGame.java:21)


    Now, I am pretty sure it is because I am trying to add a Screen element to the Card Panel. Basically I am trying to have a main screen on my game with basic options such as "start", "options", "load" ect, and then when you hit start it goes into the actually game. Any coding suggestions would be appreciated, if I am simply doing this in too complicated of manor then telling me a better way to do this altogether would be appreciated. I do appreciated lamens terms and people talking to me like I don't know what I am doing as well to make it simple for my mind to understand. :)

    Here is the code I believe to be relevant, I can post the rest if requested.

    Java Code:
    package CastleGame;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MainGame implements ActionListener{
    	
    	private int currentCard = 1;
    	private JPanel cardPanel = new JPanel();
    	private CardLayout cardLayout = new CardLayout();
    	private JFrame window;
    	
    	Screen panel;
    	JButton startBtn = new JButton("START");
    	
    	public static void main(String[] args){
    		MainGame game = new MainGame();
    	}	
    	
    	public MainGame(){
    		window = new JFrame("Collistion detection, movement, double buffering, and a game loop!");
    		Screen MG = new Screen();
    		cardPanel.add(MG, "main");
    		window.setSize(800,600);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.getContentPane().add(cardPanel);
    		window.setVisible(false);
    		window.add(startBtn);	
    		startBtn.addActionListener(this);
    		cardLayout.show(cardPanel, "main");
    	}
    Java Code:
    package CastleGame;
    
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.*;
    import java.awt.image.*;
    
    import javax.swing.*;
    
    public class Screen extends JPanel implements KeyListener{
    	private static final long serialVersionUID = 1L;
    	
    	private BufferedImage buffer;
    	private Entity player, enemy, ground;
    	private Sprite backGround;
    	private Image playerimg, enemyimg, groundimg, backGroundimg;
    	private int scrollSpeed;
    	
    	public Screen(){
    		setIgnoreRepaint(true);
    		addKeyListener(this);
    		setFocusable(true);
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see anywhere in the code above where you set a container's layout to use the card layout object that you've created. In sum, where do you call cardPanel.setLayout(cardLayout); ? Just creating a CardLayout doesn't mathemagically make your GUI use it.

  3. #3
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    Ok, so I added cardPanel.setLayout(cardLayout); at the top of the MainGame constructor, however now no errors are coming up and nothing is also happening.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ThrashingBoy View Post
    Ok, so I added cardPanel.setLayout(cardLayout); at the top of the MainGame constructor, however now no errors are coming up and nothing is also happening.
    None of the code you've posted shows anything happening. If you still need help with this problem, consider posting the relevant code and giving a more detailed description of your problem.

  5. #5
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Take a look at this: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Why do you need a CardLayout by the way? CardLayout is used to have JPanels stack on each other and by doing some action it brings the JPanel you want in front (like a wizard menu for example and clicking back/next).

  6. #6
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    I will look at that tutorial, but I was wanting to make a game with a main screen and then through that screen be able to go into other menu screens and also start the main game. I figure each of those would have their own JPanel so I was wanting to use CardLayout. Of course I am open to other options.

  7. #7
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Doing that with multiple JPanels doesn't seem efficient because everything will be loaded to memory but they won't be painted. I don't know what the game your are trying to do is like so I can think of a way to make it. Maybe share some info on that too. Is it like a platform game that when you reach the end of the screen it pops the next one (like old prince of Persia)? Is it platform that continuously moves (like Mario)?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    The OP is talking about a start menu (say, New Game, Load Game, Game Editor options). That menu screen and the sub options are cards.
    The game itself will also (possibly) be a card.

    Seems reasonable.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by santeron View Post
    Doing that with multiple JPanels doesn't seem efficient because everything will be loaded to memory but they won't be painted.
    What are you talking about? Please clarify because this advice seems possibly misguided at best.

  10. #10
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    The OP is talking about a start menu (say, New Game, Load Game, Game Editor options). That menu screen and the sub options are cards.
    The game itself will also (possibly) be a card.

    Seems reasonable.
    Oh sorry I thought he meant to have different panel for every game screen.

    Quote Originally Posted by Fubarable View Post
    What are you talking about? Please clarify because this advice seems possibly misguided at best.
    Just for clarification I didn't understand what the OP wanted to do and I thought he wanted to create let's say five cards and when the player reaches the right side of the screen, it shows the next one. So he possibly had to load those cards and move between them. So that wouldn't be efficient but I didn't get it right so there is no point... :P
    Last edited by santeron; 05-23-2011 at 01:55 PM.

Similar Threads

  1. Having trouble understanding how to use Hash Tables
    By theBurgh22 in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2011, 11:19 PM
  2. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 PM
  3. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 02:55 AM
  4. I have trouble understanding this program!
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 05:34 AM
  5. [SOLVED] Trouble understanding or expressions
    By hungdukie in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 01:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •