Results 1 to 3 of 3
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default ActionListener problem

    Solved, i had to by reading another thread on here. I had to set my KeyListner to be in focus, i did this when i repainted my board.


    Java Code:
    package maze;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class StatusPane extends JPanel implements ActionListener{
    
    
    	private Board b; 
    
    	private JLabel lives, time, difficulty;
    	private int levelTime;
    	private Timer gameTimer;
    	private JButton diffButton;
    	private String dlevel;
    
    
    
    	public StatusPane(Board board) {
    
    
    
    		b = board;
    		dlevel = "easy";
    		levelTime = 30;
    		gameTimer = new Timer(1000, this);
    		gameTimer.start();
    		this.setLayout(new GridLayout(1,4,10,15));
    		lives = new JLabel("Lives: " + b.getLives());
    		time = new JLabel("Time Left: " + levelTime);
    		difficulty = new JLabel("Difficulty:  easy");
    		diffButton = new JButton("Difficulty");
    		diffButton.addActionListener(this);
    		b.requestFocus();
    		this.add(lives);
    		this.add(time);
    		this.add(difficulty);
    		this.add(diffButton);
    
    
    
    	}
    
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		levelTime --;
    		time.setText("Time Left : " + levelTime);
    		lives.setText("Lives: " + b.getLives());
    		if(levelTime <= 0){
    			JOptionPane.showMessageDialog(null, "You ran out of Time\n game reset you lost a life \n Lives remaining: " + b.getLives(), "Times Up!", JOptionPane.WARNING_MESSAGE);
    			b.setTimeUp(true);
    			b.removeLife();
    			gameTimer.stop();
    			lives.setText("Lives: " + b.getLives());
    			b.reset();
    			levelTime = 30;
    			time.setText("Time Left : " + levelTime);
    			gameTimer.start();
    		}
    
    	}
    
    
    }
    
    package maze;
    
    import javax.swing.*;
    
    import java.awt.*; //graphics
    
    import java.awt.event.*; //action listener etc
    
    import javax.swing.*; //panel
    
    public class Board extends JPanel implements ActionListener {
    
    	private Timer timer;
    	private Map m;
    	private Player p;
    	private Font winnerText;
    	private Font loserText;
    	private boolean win;
    	private String difficulty;
    	private boolean timeUp;
    
    	public Board () {
    
    		m = new Map();
    		p = new Player();
    
    
    		winnerText= new Font("serif", Font.BOLD, 48);
    		loserText = new Font("serif", Font.BOLD, 72);
    
    		addKeyListener(new TAdapter()); // created new subcall in this class to read ket input
    		setFocusable(true); // allows it to add key listener to frame
    
    		timer = new Timer(10, this); // timer with 10ms delay, performs runsperformed
    		timer.start();
    
    
    
    
    	}
    
    
    	public void actionPerformed(ActionEvent e){
    
    
    		if(m.getMap(p.getTileX(), p.getTileY()).equals("f")){
    			win=true;
    		}
    		checkIfShark();
    		repaint();
    
    	}
    
    	public void paint(Graphics g){
    		super.paint(g);
    
    		if(checkGameOver() != true){
    			if(!win){
    				for(int y=0; y < 25; y++){ //14 = amount of tiles going down
    					for (int x =0; x < 25; x++) { // 14 - tiles going across
    						if(m.getMap( x , y ).equals("w")) {
    							g.drawImage(m.getWater(), x*32, y*32, null); //*32 so 32x32 tiles allign
    						}
    						if(m.getMap( x , y ).equals("b")) {
    							g.drawImage(m.getBuoy(), x*32, y*32, null); //*32 so 32x32 tiles allign
    						}
    						if(m.getMap( x , y).equals("f")) {
    							g.drawImage(m.getFinish(),  x*32, y*32, null);
    						}
    						if(m.getMap( x, y).equals("s")) {
    							g.drawImage(m.getShark(), x*32, y*32, null);
    						}
    					}
    				}
    
    				g.drawImage(p.getPlayerImg(),p.getTileX()*32, p.getTileY()*32, null); // paints player at its co ords
    			}
    
    			else {
    				g.setFont(winnerText);
    				g.drawString("Congratz you won", 100, 100);
    			}
    		}
    		else {
    			g.setFont(loserText);
    			g.drawString("You ran out of lives, Game Over!", 100, 100);
    		}
                        requestFocus(true); // < ------- Solution
    	}
    
    
    
    
    	public void checkIfShark() {
    
    		if(m.getMap(p.getTileX(), p.getTileY()).equals("s")){
    			reset();
    			p.setLives(p.getLives() - 1);
    
    			JOptionPane.showMessageDialog(null, "You were eaten by a shark \n game reset you lost a life \n Lives remaining: " + p.getLives(), "You died!", JOptionPane.WARNING_MESSAGE);
    
    		}
    
    
    
    
    	}
    
    	public boolean checkGameOver() {
    		boolean gameOver = false;
    		if(p.getLives() < 0) {
    			gameOver = true;
    
    		}
    		return gameOver;
    	}
    
    
    	public void reset() {
    		p.setTileX(1);
    		p.setTileY(1);
    		repaint();
    
    	}
    
    
    
    	/*
    	 * 
    	 * CLASS ISNIDE A CLASS FOR KEY PRESSES FOR MOVE PLAYER
    	 */
    
    
    
    	public class TAdapter extends KeyAdapter {
    
    		public void keyPressed(KeyEvent e) {
    
    			int key = e.getKeyCode();
    
    			if (key == KeyEvent.VK_W) {
    				if(!m.getMap(p.getTileX(), p.getTileY() -1).equals("b")) //if tile above player is not a buoy move -1 coz moving up
    					p.move(0, -1);
    
    
    			}
    
    			if (key == KeyEvent.VK_D) {
    				if(!m.getMap(p.getTileX() + 1, p.getTileY() ).equals("b")) // if tile to right player is not a buoy then move down
    					p.move(1, 0);
    
    			}
    
    			if (key == KeyEvent.VK_A)  {
    				if(!m.getMap(p.getTileX() -1 , p.getTileY() ).equals("b"))
    					p.move( -1, 0);
    
    			}
    
    			if (key == KeyEvent.VK_S)  {
    				if(!m.getMap(p.getTileX() , p.getTileY() +1).equals("b"))
    					p.move(0, 1); 
    
    			}
    		}
    	}
    
    
    	public int getLives() {
    		int l = 0;
    		if (!(p.getLives() <0)){
    			l = p.getLives();
    		}
    		return l;
    	}
    
    
    
    
    	public void setTimeUp(boolean b) {
    		timeUp = b;
    
    	}
    
    	public void TimesUp() {
    		if(timeUp != false){
    			p.setLives(p.getLives() - 1);
    			JOptionPane.showMessageDialog(null, "Times Up! \n game reset you lost a life \n Lives remaining: " + p.getLives(), "Times Up!", JOptionPane.WARNING_MESSAGE);
    
    		}
    
    		setTimeUp(false);
    
    	}
    
    
    	public void removeLife() {
    		if(p.getLives() < 0){
    			checkGameOver();
    		}
    
    		p.setLives(p.getLives() -1 );
    
    	}
    
    
    }
    Last edited by monkeyjr97; 05-03-2013 at 01:31 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: ActionListener problem

    Stick some simple debugging in there so you can see what gets called (and what values everything has) in different situations.
    There's too much code in there to follow, so you need to narrow down where the problem is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

Similar Threads

  1. ActionListener with for loop problem
    By pj6444 in forum AWT / Swing
    Replies: 2
    Last Post: 01-10-2013, 11:42 PM
  2. Have problem with add.ActionListener()
    By emily.m in forum AWT / Swing
    Replies: 5
    Last Post: 02-22-2012, 09:36 PM
  3. Problem with ActionListener?
    By Manish87 in forum AWT / Swing
    Replies: 5
    Last Post: 03-31-2011, 02:54 PM
  4. Problem Actionlistener
    By Questionmark in forum New To Java
    Replies: 18
    Last Post: 08-07-2010, 12:29 PM
  5. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •