Results 1 to 4 of 4
  1. #1
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default troubles with CardLayout

    Alright so in the game that im making there is a JPanel that uses a CardLayout to hold a group of JPanels that are the game area what I am trying to implement is that when the user goes to move off the side of the screen either to the side or above or below it moves the gameArea to the next area of the game which is contained in another JPanel in another card of the CardLayout. Now the cards switch just fine but when i try and move the player around in the new game area it throws an null pointer error which means it is looking for info in the jPanel that just went away when it should be looking for it in the one on the screen. Below is the code for the actionPerformed which fires when you move the character off the screen:

    Java Code:
    public void actionPerformed(ActionEvent e) //called when key is pressed
    {
    							if(keyStroke.getKeyCode() == 38 && gameArea.getUserLocation().getRow() == 0)//true when the up arrow key is pressed and they are at the top of the gameArea
    							{
    								for(int i =0; i <gameAreas.length;i++)//loops through the double array of Grid's that form the game area
    								{
    									for(int j =0; j< gameAreas.length;j++)//trying to find the one you are in
    									{
    										if(gameArea.equals(gameAreas[i][j])) //we found it
    										{
    											if(i-1 >=0)//checks to see if there is a Grid above the one you are in
    											{
    												int y = 24; //used to place the player on the new Grid to be displayed
    												int x = gameArea.getUserLocation().getCol(); //same as above
    												gameAreas[i-1][j].setUserLocation(new Location(y,x));//puts him on the new Grid
    												gameAreas[i-1][j].setPerson(new Location(y,x), gameArea.getPerson(gameArea.getUserLocation())); //sets his location on the Grid
    												gameAreas[i-1][j].setColor(new Location(y,x), new Color(0,0,0));//displays him on the Grid
    												gameAreas[i-1][j].repaint();//refresh the Grid
    												gameArea.setPlayer(gameArea.getUserLocation(), null); //remove him from the Grid we are on
    												gameArea.setColor(gameArea.getUserLocation(), new Color(255,255,255));//remove him visually from the Grid we are on
    												gameArea.setUserLocation(null);// remove his location from the Grid we are on
    												gameArea.repaint();//refresh the Grid we are on
    												cardLay.show(gameAreaView, "area"+(i-1)+j); //update the cardLayout to display the new Grid we moved too
    												gameAreas[i-1][j].requestFocus();//set the focus to the new Grid
    												
    											}
    										}
    									}
    								}
    							}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: troubles with CardLayout

    Where does the NullPointerException occur?

    In general I would want a Game class quite separate from the gui panels. Moving a character would involve an instance of this class being made aware of players new position. The various card panels would hold a reference to the Game instance (so the player could be shown at the right place)

    With this separation of game (aka "model") and the panels (aka "views") the logic of up key at the top would be simplified: tell the game the player has moved, switch to the new panel withtn the layout. The point isthe panels wouldn't keep or have to update information about the player position - the game would do that and the panels (eg when they painted themselves) would ask the game.

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

    Default Re: troubles with CardLayout

    Whenever you ask a question about NPE's, you will want to show which line throw the exception.

  4. #4
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: troubles with CardLayout

    ahh my bad its here
    Java Code:
     if(keyStroke.getKeyCode() == 38 && gameArea.getUserLocation().getRow() == 0)
    when I try and move the character in the new JPanel it tries to grab the userLocation from gameArea (the old JPanel) but it has already set to null.

    In response to pbrockway2: i should elaborate the JPanels that contain the games area are from a class called Grid which extends JPanel. Grid contains all the information you described i.e. the players position and such.

Similar Threads

  1. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 02:57 AM
  2. CardLayout Trouble
    By eLancaster in forum AWT / Swing
    Replies: 2
    Last Post: 06-23-2011, 11:04 AM
  3. Refresh CardLayout
    By soulmed in forum AWT / Swing
    Replies: 2
    Last Post: 05-03-2011, 02:51 PM
  4. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 01:03 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM

Posting Permissions

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