Results 1 to 20 of 22
- 10-07-2011, 09:01 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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(); } } } }
-
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.
- 10-08-2011, 12:54 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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!
-
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.
- 10-08-2011, 08:28 PM #5
- Join Date
- Aug 2011
- Location
- Pretoria, South Africa
- Posts
- 27
- Blog Entries
- 4
- Rep Power
- 0
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.
-
Re: Issue with adding a JPanel and add a mouselistener to it
- 10-08-2011, 09:05 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: Issue with adding a JPanel and add a mouselistener to it
http://www.filedropper.com/spacesscce
Here´s another link
-
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...
- 10-08-2011, 10:36 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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).
-
Re: Issue with adding a JPanel and add a mouselistener to it
Last edited by Fubarable; 10-08-2011 at 10:48 PM.
- 10-09-2011, 01:09 AM #11
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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.
-
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.
- 10-09-2011, 04:19 AM #13
- Join Date
- Aug 2011
- Location
- Pretoria, South Africa
- Posts
- 27
- Blog Entries
- 4
- Rep Power
- 0
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(); } }
-
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.
- 10-09-2011, 10:15 AM #15
- Join Date
- Aug 2011
- Location
- Pretoria, South Africa
- Posts
- 27
- Blog Entries
- 4
- Rep Power
- 0
Re: Issue with adding a JPanel and add a mouselistener to it
HeHe.. sorted then... next homework assignment please....
-
Re: Issue with adding a JPanel and add a mouselistener to it
- 10-09-2011, 04:25 PM #17
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: Issue with adding a JPanel and add a mouselistener to it
Too bad I had the time to copy it...
.gif)
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.
- 10-09-2011, 07:52 PM #18
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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?
-
Re: Issue with adding a JPanel and add a mouselistener to it
My advice remains: Don't use a KeyListener. Period. End of story.
- 10-09-2011, 08:27 PM #20
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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.
Similar Threads
-
extending JPanel issue
By p.pavlov in forum Java 2DReplies: 3Last Post: 04-29-2011, 01:27 PM -
Adding a jpanel to a customized Jpanel Class
By trishtren in forum AWT / SwingReplies: 7Last Post: 04-05-2011, 06:52 PM -
Adding Jpanel ontop of another Jpanel
By Manfizy in forum AWT / SwingReplies: 4Last Post: 03-05-2011, 10:34 PM -
JPanel/JScrollPane issue
By Moncleared in forum New To JavaReplies: 1Last Post: 09-23-2010, 04:02 AM -
Mouselistener for a Jscrollpane which contains Jpanel within it.
By bikashg in forum New To JavaReplies: 1Last Post: 05-09-2010, 06:34 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks