Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: Issue with adding a JPanel and add a mouselistener to it

  1. #1
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Question Issue with adding a JPanel and add a mouselistener to it

    Iīm making a simple application in which there are a menu-screen and a game-screen. In the menu-screen there are a go-button and if you click it with the mouse the menu-screen switches to the game-screen, in which there are some simple gameplay.

    The main class (it extends JFrame) manages the screen switching and sets up the screens through cardlayout. It sets up a JFrame and a JPanel called cardpanel on it. Cardpanel then adds:

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

    menu and game are references to their classes (the classes extends JPanel).

    The issue is that when I try to add the JPanel p, which is registered to a mouselistener, in the menu, nothing happens when clicking on it. I tried to add p directly into the main class and it worked since menu is the default visible screen, but when switching to game-room, the users keyboard inputs didnīt work. And when the game-screen was the default one, and p was added directly into main, the inputs did not work. So I really have to add p into the menu-screen, plus, it makes the code look better.

    Thankful for help and advices!

    Here is the menu class code, in which I have tried to add p: (deleted irrelevant drawing)

    Java Code:
    import some stuff
    
    public class Menu extends JPanel 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){
            add(p);
            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);
    
            setFocusable(true);
    
            p.addMouseListener(this);
                p.setVisible(true);
        }
    
        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.changeScreen();    
            }
            }
    
        }
    }

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    If you still need our help, I think that you'll need to post more code. How much? Just enough so that it will compile and run and demonstrate your problem but no code that's unrelated to the problem. This program should be a new program that you create just to demonstrate your problem and should not have all the bells and whistles of your complete program. In short, consider creating and posting an SSCCE. Please check the link for more information on this useful construct.

  3. #3
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Hereīs the SSCCE:
    MEGAUPLOAD - The leading online storage and file delivery service

    The problem is that I want the player to be able to move in the game-room using left and right. What I explained in my first post is what I think causes the game to not take any user inputs.

    Thankful for any help!

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    No, please post your SSCCE here in the forum. If you say you can't that the code is too big, it only means that you haven't fully read the link.

  5. #5
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    gargamel7 my man, I went through the trouble of downloading your code and for the first time in my life I have decided to kick my 4year old nicotine addicted dog... There is so much wrong in that code that there is no wonder you having so much troubles with it. I will try go trough it again and help with a simple solution, but for now... good luck buddy.

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Quote Originally Posted by DuvanSlabbert View Post
    gargamel7 my man, I went through the trouble of downloading your code and for the first time in my life I have decided to kick my 4year old nicotine addicted dog... There is so much wrong in that code that there is no wonder you having so much troubles with it. I will try go trough it again and help with a simple solution, but for now... good luck buddy.
    I couldn't even get the code as the site stated it was unavailable.

  7. #7
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Quote Originally Posted by Fubarable View Post
    I couldn't even get the code as the site stated it was unavailable.
    http://www.filedropper.com/spacesscce

    Hereīs another link

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    OK, got your code, but no, it's not an SSCCE (again, please look at the link). Regardless what exactly is "p"? Here it looks to be a plain JPanel, in your code a JLabel, and how are you adding it to the Menu JPanel, where is its MouseListener,... etc...

  9. #9
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Ok, will check what a SSCCE is again. I changed between JPanel and JLabel to see if there could be any difference so I guess I forgot about changing back, sorry for confusing. The adding part is where the problem is, so I know itīs wrong in the "SSCCE". What I want is to be able to click on the go-button so that the menu-screen switches into the game-screen where you can move the pink object left and right, and also not be able to click on the same area as where the go-button used to be (I want the p to not be noticeable/clickable).

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Quote Originally Posted by gargamel7 View Post
    Ok, will check what a SSCCE is again. I changed between JPanel and JLabel to see if there could be any difference so I guess I forgot about changing back, sorry for confusing. The adding part is where the problem is, so I know itīs wrong in the "SSCCE". What I want is to be able to click on the go-button so that the menu-screen switches into the game-screen where you can move the pink object left and right, and also not be able to click on the same area as where the go-button used to be (I want the p to not be noticeable/clickable).
    Sorry, probably it's me, but I'm still not getting it. Can you provide even more detail. Perhaps try explaining it as if I have the intelligence of an 11 year old (and you wouldn't be too far off!) and don't know anything at all about your program.
    Last edited by Fubarable; 10-08-2011 at 10:48 PM.

  11. #11
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Hehe ok. Itīs very simple. There are two screens: the menu-screen and the game-screen. I see a screen as a room, sort of. In the menu-screen there are a go-button and if you click it, you will go to the game-screen. And then, when you have clicked the go-button and you go from menu-room to game-room, you can see this pink character in the bottom of the screen. This character should be able to move left or right depending on what the user inputs, like a spaceinvaders-ship. So:

    -In the menu there are a go-button
    -Mouseclick the go-button
    -Now, youīre in the game-room.
    -Move the game-rooms pink character left and right.

    Thatīs how I want it.

    As it is now, Iīm having trouble adding the JPanel p, which catches mouseclicks, on the menus JPanel. I can add it in the main class but then I canīt move the pink character and also Iīm able to click the invisible go-button-area when in the game-room (the area where the go-button was in the menu-screen), which I donīt want to be able to.

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Suggestions:

    • Place your go ImageIcon in a JLabel (or JButton), add a MouseListener to the JLabel (ActionListener if a JButton), and display that JLabel in your Menu JPanel. Don't mess with custom painting when you don't have to.
    • Don't use a KeyListener to make your image move, but instead use key bindings. They're more flexible and can be written to work even if the component that holds the key bindings isn't focused (unlike KeyListeners).
    • If you do need to draw within a JComponent-derived class such as a JPanel, don't draw in its paint override, but instead draw within its paintComponent method override to be able to take full advantage of Swing graphics.
    Last edited by Fubarable; 10-09-2011 at 02:25 AM.
    DuvanSlabbert likes this.

  13. #13
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    I think this would be a good place to start... please note that this code is not the perfect solution to the problem but rather a guideline to what can be done and to spark creativity of your own work. if you need further help you are always welcome to ask.

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Main extends JFrame {
    	
    	private JPanel menuPnl = new JPanel(){
        	public void paint(Graphics g) {
        		g.drawImage(new ImageIcon("go.png").getImage(),130,480,this);
        	}
        };
    	
    	Main(){
    		setTitle("SpaceBanana");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(480,700);
    	    setLocationRelativeTo(null);
    	    setResizable(false);
    	    setVisible(true);
    	    add(menuPnl);
    	    this.addMouseListener(new MouseAdapter() {
    	    	public void mouseClicked(MouseEvent evt) {
    	    		getContentPane().remove(menuPnl);
    	    		getContentPane().repaint();
    	    		add(new GamePanel());
    	    		getContentPane().repaint();
    	    	}
    	    });
    	}
    	
    	class GamePanel extends JPanel {
    		GamePanel(){
    			setBounds(0,0,480,700);
    			this.setBackground(Color.BLUE);
    		}
    	}
    	
    	public static void main(String[] args) {
    		new Main();
    	}
    }

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Heck, if we're going to post code, this is my SSCCE (though more of a "long"-SCCE):

    <deleted>
    Last edited by Fubarable; 10-09-2011 at 02:45 PM. Reason: removed inappropriate statics.

  15. #15
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    HeHe.. sorted then... next homework assignment please....

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Quote Originally Posted by DuvanSlabbert View Post
    HeHe.. sorted then... next homework assignment please....
    I removed my code as it did too much.

  17. #17
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Quote Originally Posted by Fubarable View Post
    I removed my code as it did too much.
    Too bad I had the time to copy it...

    To be clear, this was not a homework, just a little project of mine.
    Thanks for taking the time to write the code which will help me to code these things better.
    And about the key bindings thing; if itīs a better way to do it, I donīt get why I always see the use of a KeyListener in Space Invaders examples and such. But thanks for the tip!
    Last edited by gargamel7; 10-09-2011 at 04:45 PM.

  18. #18
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    When I experimented a bit and changed the key bindings to use a keylistener instead, the scenario with your code is just as it was with my code. I can click the go-button but then I canīt move the character in the game-room. Iīm really wondering how to do so that I can use the keylistener (since it seems easier) and then also be able to let it have the focus so I can move the character. Any advice?

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

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    My advice remains: Don't use a KeyListener. Period. End of story.

  20. #20
    gargamel7 is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Issue with adding a JPanel and add a mouselistener to it

    Haha, but I want to. Key bindings seems more complicated to use and Iīm happy with keylisteners but for the focus thing. There have to be an easy way to let the keylistener to have the focus. Iīve experimented some with RequestFocusInWindow(); and setFocusable(true); but cannot solve it.

    Iīm probably going to start using key bindings when Iīm more comfortable with coding java, but not yet.

Page 1 of 2 12 LastLast

Similar Threads

  1. extending JPanel issue
    By p.pavlov in forum Java 2D
    Replies: 3
    Last Post: 04-29-2011, 01:27 PM
  2. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  3. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  4. JPanel/JScrollPane issue
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 04:02 AM
  5. Replies: 1
    Last Post: 05-09-2010, 06:34 PM

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
  •