Results 1 to 6 of 6
  1. #1
    jgezau is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default 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:

    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)
    	{}
    }
    The following are the actual panels declarations:

    Java Code:
    	BurningMeUp bmu;
    	Hero h;
    Thanks

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

    Default

    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.

  3. #3
    jgezau is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    That is exactly what I am trying to accomplish. :)
    I don't know anything about CardLayout, but I'll look into that.

    Thanks

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

    Default

    Have a look here: CardLayout Tutorial

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

    Default

    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?

  6. #6
    jgezau is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks Fubarable, I'm using CardLayout now, and everything is working fine :)

Similar Threads

  1. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 03:11 PM
  2. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 12:46 PM
  3. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  4. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 05:22 PM
  5. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 06:54 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
  •