Results 1 to 2 of 2
  1. #1
    syon is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Problem with Keylistener, some help pls

    Heya, would apreciate some help if possible.. to start with, im sry that i didnt display my code the right way (couldnt find the option/ thread about it), anyway... my problem is this:

    My school work is to make the Bomberman game, and i have 3 classes extending JPanel (menu, singleplayer and high scores), each of them take as parameter the same JFrame "myGame" wich i creat in the main class.
    My idea is that each time i start one of those 3 classes.. the JFrame will have as contentPane the corresponding JPanel.

    If i start with the SinglePlayerPanel, my keylistener is working fine and i can move my character.
    if im inside a SinglePlayerPanel and i restart it (make a new one) it still works fine.
    the problem comes if i change from MenuPanel into SinglePlayerPanel, the display changes and basicly everything works besides the Keylistener. (with everything else i mean TimerListeners, my "enemies" move fine aswell, etc).

    My code for both MenuPanel and SinglePlayerPanel is this:

    MenuPanel:

    Java Code:
    public class MenuFrame extends JPanel implements ActionListener {
    	private Container contentPane;
    	private JFrame myGame;
    	private Image img = new ImageIcon("bomberman/GameFrames/img/bomberman Menu.png").getImage();
    	public MenuFrame(JFrame myGame) {		
    		this.myGame = myGame;		
    		JButton SinglePlayer = new JButton("Start");
    		JButton MultiPlayer = new JButton("Multiplayer");
    		JButton Load = new JButton("Load");
    		JButton HighScore = new JButton("High Scores");
    		JButton exit = new JButton("Exit");
    		
    		SinglePlayer.setLocation(63, 443);
    		MultiPlayer.setLocation(186, 443);
    		Load.setLocation(340, 443);
    		HighScore.setLocation(464, 443);
    		exit.setLocation(629, 443);
    		
    		SinglePlayer.addActionListener(this);
    		MultiPlayer.addActionListener(this);
    		Load.addActionListener(this);
    		HighScore.addActionListener(this);
    		exit.addActionListener(this);
    		
    		add(SinglePlayer);
    		add(MultiPlayer);
    		add(Load);
    		add(HighScore);
    		add(exit);
    		
    		//myGame.setContentPane(contentPane);
    		myGame.setContentPane(this);
    		myGame.validate();
    		myGame.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("Start")) {
    			SinglePlayerFrame gui = new SinglePlayerFrame(myGame, 1, false);
    		}
    		else if (e.getActionCommand().equals("Multiplayer")) {
    			
    		}
    		else if (e.getActionCommand().equals("Load")) {
    			
    		}
    		else if (e.getActionCommand().equals("High Scores")) {
    	
    		}
    		else if (e.getActionCommand().equals("Exit")) {
    			
    		}
    		else System.out.println("Error in MenuFrame Listeners");
    	}
    	public void paintComponent(Graphics g)
    	{
    		g.drawImage(img, 3, 8, null);
    		super.paintComponents(g);
    	}
    }
    still have to complete the other actionEvents.


    my SinglePlayerPanel:
    Java Code:
    public class SinglePlayerFrame extends JPanel implements KeyListener {
    	private GameTimers gameTimer;
    	private JFrame myGame;
    	public static BackgroundPanel backgroundIngameAlive = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Fight.png").getImage());
    	public static BackgroundPanel backgroundIngameDead = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Dead.png").getImage());
    	public static BackgroundPanel backgroundIngameDoorAvaible = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Victorius.png").getImage());
    	
    	public SinglePlayerFrame(JFrame myGame, int level, boolean continuation) {		
    		this.myGame = myGame;
    		this.setLayout(new BorderLayout());
    		JPanel game = new JPanel();
    		JPanel rest = new JPanel();
    		//contentPane = myGame.getContentPane();
    		add(game, BorderLayout.CENTER);
    		add(rest, BorderLayout.EAST);
    		
    		rest.setLayout(new BorderLayout());
    		JPanel bomberman = new JPanel();
    		JPanel information = new JPanel();
    		rest.add(information, BorderLayout.NORTH);
    		rest.add(bomberman, BorderLayout.SOUTH);	
    		rest.add(backgroundIngameAlive, BorderLayout.CENTER);
    		backgroundIngameAlive.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("bomber"), null));
    		information.setLayout(new GridLayout(3, 1));
    		information.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("information"), BorderFactory.createEmptyBorder(5, 5, 5 ,5)));
    		information.add(time);
    		information.add(points);
    		information.add(enemysLeft);
    		
    		bomberman.setLayout(new GridLayout(6, 1));
    		bomberman.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Bomberman"), BorderFactory.createEmptyBorder(5, 5, 5 ,5)));
    		bomberman.add(remoteControl);
    		bomberman.add(bombs);
    		bomberman.add(flameLevel);		
    		bomberman.add(speed);
    		bomberman.add(totalLifes);
    		bomberman.add(lifesLeftPowerUp);
    		GameLevel gl = new GameLevel(Integer.toString(level), false, true, continuation);
    		gameTimer = new GameTimers(gl, this);
    		add(gameTimer, BorderLayout.CENTER);
    		gameTimer.addKeyListener(this);
    		gameTimer.setFocusable(true);
    		gameTimer.requestFocusInWindow();
    		rest.setFocusable(false);
    		myGame.setContentPane(this);
    		myGame.validate();
    		myGame.setVisible(true);
    		
    	}
    	public JFrame getMyGame() { return myGame; }
    	public void keyPressed(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    		if (GameLevel.bomber.isDead() == false) {	
    			if (keyCode == KeyEvent.VK_UP) {
    				GameLevel.bomber.setCurrentCommand("up");
    			}
    			else if(keyCode == KeyEvent.VK_DOWN) {
    				GameLevel.bomber.setCurrentCommand("down");
    			}
    			else if(keyCode == KeyEvent.VK_LEFT) {
    				GameLevel.bomber.setCurrentCommand("left");
    			}
    			else if(keyCode == KeyEvent.VK_RIGHT) {
    				GameLevel.bomber.setCurrentCommand("right");
    			}
    			else if(keyCode == KeyEvent.VK_SPACE) {
    				gameTimer.placeBomb(GameLevel.bomber);
    			}
    			else if(keyCode == KeyEvent.VK_CONTROL) {
    				
    			}
    		}
    		else e.consume();		
    	}
    	public void keyReleased(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    		if ((keyCode == KeyEvent.VK_UP) || (keyCode == KeyEvent.VK_DOWN) || (keyCode == KeyEvent.VK_LEFT) || (keyCode == KeyEvent.VK_RIGHT)) {
    			GameLevel.bomber.setCurrentCommand("idle");
    		}
    		else 
    			e.consume();
    	}
    	public void keyTyped(KeyEvent e) {
    		e.consume();		
    	}
    }

    those backgroundPanels are a class i made so i could changes images depending on some situation in the game.

    if i just run SinglePlayer i dont need gameTimer.requestFocusInWindow();, its there coz i was trying to find some solution.

    well, since i already made a post about this... might aswell one other doubt i have xD, wich is: any idea why the buttons in MenuPanel arent changing to the location i wrote? (the total size of myGame is : 730 x 580.


    thx in advance, sry for long post and probably confusing english (not my main language)
    cumps


    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-21-2011 at 02:28 AM. Reason: Moderator Edit: Code tags added

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

Similar Threads

  1. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 01:55 AM
  2. AWT KeyListener Problem
    By plm-pusik in forum New To Java
    Replies: 15
    Last Post: 11-10-2010, 04:38 PM
  3. KeyListener problem
    By siyi90 in forum AWT / Swing
    Replies: 7
    Last Post: 02-08-2010, 11:16 AM
  4. Problem with KeyListener in Jtable
    By sandeepsai17 in forum New To Java
    Replies: 0
    Last Post: 06-30-2009, 11:14 AM
  5. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 05:14 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
  •