Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2014
    Posts
    25
    Rep Power
    0

    Default why is my KeyListener not working?

    I've look almost any where but I cant figure out how to get the keys to work for a snake type game i'm attempting to make.My code is kind of scattered so i'm going to put the whole class here
    Java Code:
    public class Screen extends JPanel implements Runnable {
    
    	private static final long serialVersionUID = 1L;
    
    	
    		final int WIDTH = 600;
        	final int HEIGHT = 600;
        	private Thread thread;
        	private boolean running = false;
        	
        	private snake_body sb;
        	private ArrayList<snake_body> snake;
        	
        	private int xCoor = 10, yCoor = 10;
        	private int size = 5;
        	
        	
            private int ticks = 0;
        	public Screen() {
        		
        		setPreferredSize(new Dimension(WIDTH, HEIGHT));
        		
        		snake = new ArrayList<snake_body>();
        		
        		start();
    	       
        	}
    		
        	public boolean right = true, left = false, up = false, down = false;
        	
        	public class Key implements KeyListener  {
            //KeyListener is right here
        	TextField a;
    
        	public void init() {
        		}
    
    
        	public void keyPressed(KeyEvent e) {
        	
             double key = e.getKeyCode();
        
             if (key == KeyEvent.VK_DOWN && !left) {
            
        	right = false;
        	up = false;
        	down = true;
        	
        	
        	
        	
        	}
        	}
    
    
        	public void keyReleased(KeyEvent e) {
    
        		
        	}
        	public void keyTyped(KeyEvent e) {
    
        		
        	}
        	}
        	public void tick() {
        	if(snake.size() == 0) {
        	sb = new snake_body(xCoor, yCoor, 10);
        	snake.add(sb);
        		
        	}
        	ticks++;
        	
        	
        	if(ticks > 750000) {
        		
        		if(right) {
        			xCoor++;
        		}
        		if(left) {
        			xCoor--;
        		}
        		if(up) {
        			yCoor--;
        		}
        		if(down) {
        			yCoor++;
        		}
        	
        		
        	ticks = 0;
        	
        	sb = new snake_body(xCoor, yCoor, 10);
        	snake.add(sb);
        	
        	if(snake.size() > size) {
        	snake.remove(0);
        	
        	}
        	}
        	}
        		
        	
        
        	
        	public void paint(Graphics g) {
        		g.clearRect(0, 0, WIDTH, HEIGHT);
        		
        		for(int i = 0; i < WIDTH / 10; i++) {
            	g.drawLine(i * 10, 0, i * 10, HEIGHT);
        		}
        		for(int i = 0; i < HEIGHT / 10; i++) {
                	g.drawLine(0, i * 10, WIDTH, i * 10);
        	
        		
        		}
        	
        		for(int i = 0; i < snake.size(); i++) {
        	    snake.get(i).draw(g);
        		}
        		}
        	public void start() {
    		running = true;
    		thread = new Thread(this, "Loop");
    		thread.start();
    		}
    		public void stop() {
    		}
    		
    		
    		
    		
    		
    		public void run() {
    		while(running) {
    		tick();
    		repaint();
    		
    }
    		
    }
    		
    }
    and my JFrame class-
    Java Code:
    public class Frame extends JFrame {
    	
    	
    
    	public static void main(String[] args) {
    
    		JFrame Frame = new JFrame();
    		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    Frame.setTitle("Snake");
    		Frame.setResizable(false);
    	    
    
    	    
    	    GridLayout Grid = new GridLayout();
    	    
    	    Grid.setRows(1);
    	    Grid.setColumns(1);
    	    Grid.setHgap(0);
    		Grid.setVgap(0);
    	
    	    Screen s = new Screen();
    	    Frame.add(s);
    	    Frame.pack();
    	    Frame.setLocationRelativeTo(null);
    	    Frame.setVisible(true);
    	    
    	    
    	
    	
    	}
    		
    	}
    Last edited by DeathByFire223; 03-16-2015 at 07:05 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why is my KeyListener not working?

    Does your JPanel have the focus?

    Either give the JPanel the focus, or put the KeyListener on the JFrame instead.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Join Date
    Aug 2014
    Posts
    25
    Rep Power
    0

    Default Re: why is my KeyListener not working?

    Quote Originally Posted by KevinWorkman View Post
    Does your JPanel have the focus?

    Either give the JPanel the focus, or put the KeyListener on the JFrame instead.
    I don't have a JPanel because I don't have a button on the program.A added my JFrame code in the main post and the game is 600 by 600
    Last edited by DeathByFire223; 03-16-2015 at 07:12 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why is my KeyListener not working?

    You're extending JPanel. You have a JPanel.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    Join Date
    Aug 2014
    Posts
    25
    Rep Power
    0

    Default Re: why is my KeyListener not working?

    even though I never did JPanel panel = new JPanel();?

    EDIT-if thats the case how do I give a JPanel the focus? is it the same as a JFrame?
    Last edited by DeathByFire223; 03-16-2015 at 07:09 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why is my KeyListener not working?

    Screen extends JPanel. In other words, Screen is a JPanel. So when you create an instance of Screen, you're creating a JPanel.

    It's like if you're holding an apple, and I tell you you're holding a piece of fruit. You wouldn't then say "I don't have any fruit, I only have an apple!"

    I'd recommend googling something like "JPanel focus" for a ton of results, and then posting an MCVE if you get stuck.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    Join Date
    Aug 2014
    Posts
    25
    Rep Power
    0

    Default Re: why is my KeyListener not working?

    ok thank you

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: why is my KeyListener not working?

    Moved from New to Java to AWT / Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. KeyListener is Not Working
    By Prime624 in forum New To Java
    Replies: 7
    Last Post: 04-16-2013, 08:00 AM
  2. KeyListener in JTextField not working
    By AndrewM16921 in forum AWT / Swing
    Replies: 5
    Last Post: 12-11-2012, 01:48 AM
  3. KeyListener not working
    By cc11rocks in forum AWT / Swing
    Replies: 3
    Last Post: 02-09-2011, 01:09 PM
  4. keylistener + applet isn't working
    By brandon95 in forum Java Applets
    Replies: 6
    Last Post: 01-03-2011, 02:10 PM
  5. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 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
  •