Results 1 to 10 of 10
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Can't figure out how to use JFrame's add() method right

    I'm trying to make a game with multiple levels, but once I load the second level I can't move my character on screen. I know where the problem is, but I don't fully know what's going on or how to fix the problem.

    The problem is adding the classes "Inner" and "Inner2" to the JFrame. I can add the variable "inner" to the JFrame easily enough by putting it at the beginning of the constructor right after the call for super(); but if I put it anywhere else I am not able to move in my game.

    Everything was working just fine until I needed more than one level, so I added the Swing timer to constantly check for when the player went over a certain limit, where the game was then supposed to remove the first level from the JFrame and then add the second level.

    I tried this and I found myself not able to move in the game.


    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ProjectWindow extends JFrame implements ActionListener
    {
    
      public Inner inner;
      public Inner2 inner2;
      public boolean level1;
      public Timer timer;
      private int check = 0;
      private boolean level1unf = true;
    
      ProjectWindow()
      {
        super();
        inner = new Inner();
        add(inner);               // If I put this anywhere else, I can't move in the game
        setTitle("Civil War Game");
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(600, 500);
        setBounds(75,75,600,500);
        setVisible(true);
        level1 = true;
        // if I were to put add(inner); here, it would not let me move in game.
        timer = new Timer(100, this);
      }
    
      public void Level()
      {
        if(level1)
        {
          level1 = false;
          timer.start();
        }
      }
    
      public void actionPerformed(ActionEvent e)
      {
        check = inner.getInt();
        if(check > 1500 && level1unf)
        {
          level1unf = false;
          remove(inner);
          inner2 = new Inner2();
          add(inner2); // this is where the issue is. Because I can't add
                     //everything in the constructor, I have to add the second level here.
                     // The game will basically freeze and I won't be able to move.
        }
      }
    }
    Last edited by Fortu; 01-16-2011 at 10:43 PM.

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

    Default

    Whenever you add and remove components, you need to revalidate() and often repaint() the container that has had components changed. If this Container is the contentPane, you may need to cast it to a JPanel before calling revalidate() on it. To make things easier for you, I would suggest you use a CardLayout to allow you to more easily swap components (JPanels) in your JFrame. As for your specific problem, I'm not sure how we can guess what the problem is given the data at hand.

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Well, I tried to repaint() and validate() like you said, but all I can do is fire bullets when I remove inner and add inner2.

    I still can't move when inner2 is added, but mouseEvents are seen by Inner2, which extends JPanel, which means I can see the bullet as it is painted on the screen, but my keyPressed and keyReleased events in Inner2 don't seem to be doing anything.

    In short, I can shoot bullets, see them on screen, kill enemies successfully with them, and hear the audio that plays when I shoot, but I can not move.


    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ProjectWindow extends JFrame implements ActionListener
    {
    
      public Inner inner;
      public Inner2 inner2;
      public boolean level1;
      public Timer timer;
      private int check = 0;
      private boolean level1unf = true;
    
      ProjectWindow()
      {
        super();
        inner = new Inner();
        getContentPane().add(inner);
        inner2 = new Inner2();
        setTitle("Civil War Game");
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(600, 500);
        setBounds(75,75,600,500);
        setVisible(true);
        level1 = true;
        timer = new Timer(100, this);
      }
    
      public void Level()
      {
        if(level1)
        {
          level1 = false;
          timer.start();
        }
      }
    
      public void actionPerformed(ActionEvent e)
      {
        check = inner.getInt();
        if(check > 1500 && level1unf)
        {
          level1unf = false;
          getContentPane().remove(inner);
          inner2 = new Inner2();
          getContentPane().add(inner2);
          getContentPane().validate();
          getContentPane().repaint();
        }
      }
    }

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

    Default

    I'm stumped by a lack of understanding of the workings of your program. You say "you can't move", but we see no code that makes you move. You're not using KeyListeners by chance are you? If so, you may have a problem of lack of focus since KeyListeners only work on the component that has focus. To get around this, you may try to use key binding instead. Please note that this recommendation is just a guess since we're only privy to a partial information about the problem.

  5. #5
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Yes, I am using KeyListeners, and I will check out key binding right now.

    (Sidenote: I thought using a KeyListener was the only way to do this. o_o)

  6. #6
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Here's a small example of what Inner and Inner2 have for KeyListeners and MouseListeners:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    public class Inner extends JPanel implements ActionListener
    {
    
      public Player playuh;
    
      Inner()
      {
        super();
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(new TAdapter());
        addMouseListener(new RAdapter());
        playuh = new Player();
        setDoubleBuffered(true);
        setSize(600,465);
      }
    ...
      class TAdapter extends KeyAdapter
      {
        public void keyReleased(KeyEvent e)
        {
          playuh.keyReleased(e);
        }
    
        public void keyPressed(KeyEvent e)
        {
          playuh.keyPressed(e);
        }
      }
    
      class RAdapter extends MouseAdapter
      {
        public void mousePressed(MouseEvent e)
        {
          playuh.mousePressed(e);
        }
             
        public void mouseReleased(MouseEvent e)
        {
        }
      }

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

    Default

    So if you click on your GUI after the view changes, do the key listeners work? I assume that they do. If so, again consider using key binding instead.

  8. #8
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So if you click on your GUI after the view changes, do the key listeners work? I assume that they do. If so, again consider using key binding instead.
    Hmmmm, well, I just tried my game again, and after clicking on a different window or minimizing and then maximizing the game window again, I am able to move my character on screen. I think I had best go with the key bindings, but I'm not fully sure how to do so..

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

    Default

    Quote Originally Posted by Fortu View Post
    Hmmmm, well, I just tried my game again, and after clicking on a different window or minimizing and then maximizing the game window again, I am able to move my character on screen. I think I had best go with the key bindings, but I'm not fully sure how to do so..
    There's a bit of a learning curve involved, but once you get used to it, it's not that hard. In addition, we can help you through any rough spots.

  10. #10
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Alright, I'll try it out and see how it goes. Thanks Furarable!

Similar Threads

  1. disabling JFrame's response to spacebar
    By gib65 in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 05:38 PM
  2. Cant figure out where went wrong.
    By leviathan in forum New To Java
    Replies: 15
    Last Post: 06-06-2010, 07:55 PM
  3. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 11:12 PM
  4. Need Help in BlueJ... Cannot figure this out.
    By ERICAMAUVE4 in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 01:34 AM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 05:38 AM

Tags for this Thread

Posting Permissions

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