Results 1 to 6 of 6
Thread: Multiple JPanels Not Working
- 04-14-2009, 03:40 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Multiple JPanels Not Working
Hi,
I am making dance game (DDR type). So I am trying to use multiple panels, I have a Menu panel, and 3 different panels for 3 different songs (only implemented two right now). In the Menu panel I have a implemented KeyListener, when pressing 1, it takes me to the first song, when pressing 2, to the second song. That is how is supposed to work. I also have three different backgrounds for all the panels.
The following Menu appear when I run the application.
1. Song 1 (first panel)
2. Song 2 (second panel)
3. Song 3 (third panel, not implemented)
Now the problem. When I press 1, it does take me to that panel, but when pressing 2, I am assuming it takes me to that panel, but the background for the second panel does not appear, I can only see the background for the first panel; I am assuming it does take me to that panel, since I have a System.out.println() to show me when I press the appropriate keys (either 1 or 2).
It is probably a drawing or painting problem, but idk. Now, my code looks like this:
The following are the actual panels declarations:Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GameMenu extends JPanel implements KeyListener { String imageFilePath = "imgs/menuBg.png"; ImageIcon icon; Image img; JLabel song1, song2, song3, dummy; JLabel menuBg; BurningMeUp bmu; Hero h; int phase; String filename = "sound/32a5_mp3.mp3"; Mp3Player mp3 = new Mp3Player(filename); //game constructor GameMenu() { phase = 0; setSize(640,480); setLayout(new BorderLayout()); setBackground(Color.black); song1 = new JLabel(); song2 = new JLabel(); song3 = new JLabel(); dummy = new JLabel(); //if(phase==0) menu(); icon = new ImageIcon(imageFilePath); img = icon.getImage(); bmu = new BurningMeUp(); h = new Hero(); mp3.play(); //bmu.stop(); //play(); } //displays the game menu public void menu() { song1.setText("1. Burning Me Up!"); song1.setBounds(200, 300, 200, 25); //(x,y,width,height) song1.setForeground(Color.WHITE); song1.setFont(new Font("",Font.BOLD,20)); add(song1); song2.setText("2. Hero"); song2.setBounds(200, 325, 200, 25); //(x,y,width,height) song2.setForeground(Color.WHITE); song2.setFont(new Font("",Font.BOLD,20)); add(song2); song3.setText("3. Burning Me Up!"); song3.setBounds(200, 350, 200, 25); //(x,y,width,height) song3.setForeground(Color.WHITE); song3.setFont(new Font("",Font.BOLD,20)); add(song3); dummy.setText(""); add(dummy); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, this); } public void play() { new WavPlayer("sound/32a5.wav").start(); } public void keyPressed(KeyEvent k) { char option = k.getKeyChar(); if(phase == 0) { if (option == '1') { System.out.println("Go to song number one"); //stop(); mp3.close(); bmu.playMp3(); this.setVisible(false); bmu.setVisible(true); phase++; } if (option == '2') { /*icon = new ImageIcon("imgs/heroBg.png"); img = icon.getImage();*/ System.out.println("Go to song number two"); mp3.close(); h.playMp3(); /*remove(song1); remove(song2); remove(song3); repaint();*/ this.setVisible(false); bmu.setVisible(false); h.setVisible(true); //bmu.remove(); phase++; } if (option == '3') { System.out.println("Go to song number three"); this.setVisible(false); phase++; } } } public void keyReleased(KeyEvent k) {} public void keyTyped(KeyEvent k) {} }
ThanksJava Code:BurningMeUp bmu; Hero h;
-
You have posted quite a bit of code, and I honestly can't say that I've gone through it all, but I suspect that you want to swap out JPanels depending on some type of menu selection. If so, you should consider having a Container that uses the CardLayout hold your three song panels and then swapping the song panels via this layout. It simplifies things greatly. Also, I recommend against use of a key listener here. If you have a JMenu, use a mnemonic. If you must use a key listener-type construct, then consider using key bindings instead. Their cleaner and self-documenting.
- 04-14-2009, 03:58 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
That is exactly what I am trying to accomplish. :)
I don't know anything about CardLayout, but I'll look into that.
Thanks
-
Have a look here: CardLayout Tutorial
-
I think I see a basic problem with your OOP design here. You seem to be creating a different class for each song/display which doesn't make sense. The Song is the data, not the code. You should create a Song class that can subclass JPanel (though I myself wouldn't subclass JPanel since I prefer extend classes via composition, not inheritance -- you'll learn about this later) has a String field for name, a playMp3 method, etc...
This way, you can add as many Songs to your program as you like and not have to create a new class each time. Makes sense, right?
- 04-17-2009, 04:52 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
JList and JPanels
By JetsYanks in forum New To JavaReplies: 8Last Post: 12-25-2009, 02:11 PM -
HELP with jPanels
By maverik_vz in forum AWT / SwingReplies: 1Last Post: 03-12-2009, 11:46 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Can't synchronize multiple JPanels in a JFrame
By vassil_zorev in forum AWT / SwingReplies: 0Last Post: 12-30-2007, 04:22 PM -
How to add Images to JPanels?
By Soda in forum New To JavaReplies: 3Last Post: 12-08-2007, 05:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks