7 Replies - 425 Views - Last Post: 03 May 2012 - 02:49 PM Rate Topic: -----

#1 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 08:10 AM

hey there i am currently learning game programming and i did not want to use awt and go fullscreen so i made a jpanel which i am drawing on, but i can't get my keylistener to work i have tried several different methods but i can't get it to work and idk why, this is my current code:

package Debug;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

class Draw extends JPanel implements KeyListener {
    
    public static int Xpos = 200;
    public static int Ypos = 200;
    BufferedImage player = null;
    
    public Draw() {
        
        if (Xpos >= 700) {
            Xpos = 700;
        }
        if (Xpos <= -7) {
            Xpos = -7;
        }
        if (Ypos >= 480) {
            Ypos = 480;
        }
        if (Ypos <= -7) {
            Ypos = -7;
        }
        
        addKeyListener(this);
        try {
            player = ImageIO.read(new File("player.png"));
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        
        String key = e.getKeyText(e.getKeyCode());
        if (key.equals("w")) {
            Xpos++;
            repaint();
        }
        
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
    }
    
    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        
        Font arial = new Font("Arial",Font.BOLD,22);
        g2d.setFont(arial);
        
        g2d.drawImage(player, Xpos, Ypos, null, null);
        g2d.drawString("Xpos: " + Xpos, 50, 30);
        g2d.drawString("Ypos: " + Ypos, 50, 50);
    }
    
}


it might not be the best code :P but hey i just started like a week ago

Is This A Good Question/Topic? 0
  • +

Replies To: can't get keyListening to work :( tried for 3 days now

#2 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 08:29 AM

setFocusable(true);



(Put that in the panel constructor)

Hope I helped!


EDIT: By the panel constructor I mean the Draw() function, or rather the Draw constructor.

This post has been edited by Crockeo: 03 May 2012 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 08:34 AM

View PostCrockeo, on 03 May 2012 - 08:29 AM, said:

setFocusable(true);



(Put that in the panel constructor)

Hope I helped!


EDIT: By the panel constructor I mean the Draw() function, or rather the Draw constructor.


yea i forgot about that :S but still it does not help giving it the focus i also checked if it was in focus and it is

checked with:
if (setFocusable != true) {
     setBackground(Color.BLACK);
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 08:56 AM

Your panel requires the focus for the KeyListener to work
I don't know how your main() works but the listener works for me with

    public static void main(String[] args) {
    	JFrame f = new JFrame("Draw");
    	Draw draw = new Draw();
    	f.add(draw);
    	f.setSize(400,300);
    	f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	f.setVisible(true);
    	draw.requestFocus();   // <----------
    }



Also, if you use getKeyText() it always returns CAPITAL so it would be "W" not "w"
why not simply using getKeyChar() ?

View PostCrockeo, on 03 May 2012 - 11:29 AM, said:

setFocusable(true);



(Put that in the panel constructor)

JPanel are focusable by default. You actually want to requestFocus()
Was This Post Helpful? 0
  • +
  • -

#5 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 11:11 AM

yea i am already requestingFocus and it is not working :/ btw here's my main if that helps :) i am really stuck on this

package Debug;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;

public abstract class Main {
  public static void main(String[] args) {
      JFrame f = new JFrame("Draw");
      Draw draw = new Draw();
      f.add(draw);
      draw.requestFocus();
      
      f.setSize(800, 600);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setTitle("MR.Hairy - Version.Alpha 0.1");
      f.setResizable(false);
      f.setVisible(true);
      
  }
}



and this is the Draw class atm:
package Debug;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

class Draw extends JPanel implements KeyListener {
    
    public static int Xpos = 200;
    public static int Ypos = 200;
    BufferedImage player = null;
    
    public Draw() {
        
        if (Xpos >= 700) {
            Xpos = 700;
        }
        if (Xpos <= -7) {
            Xpos = -7;
        }
        if (Ypos >= 480) {
            Ypos = 480;
        }
        if (Ypos <= -7) {
            Ypos = -7;
        }
        
        setFocusable(true);
        addKeyListener(this);
        try {
            player = ImageIO.read(new File("player.png"));
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        
        String key = e.getKeyText(e.getKeyCode());
        if (key.equals("w")) {
            Xpos++;
            repaint();
        }
        
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
    }
    
    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        
        Font arial = new Font("Arial",Font.BOLD,22);
        g2d.setFont(arial);
        
        g2d.drawImage(player, Xpos, Ypos, null, null);
        g2d.drawString("Xpos: " + Xpos, 50, 30);
        g2d.drawString("Ypos: " + Ypos, 50, 50);
    }
    
}


i really hope u guys can troubleshoot this, because i can't get the player to move :/
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 11:22 AM

works for me. Add a
 
    String key = e.getKeyText(e.getKeyCode());
    System.out.println("Key = " + key); 


in your keyPressed() method to see what is trapped
and as I have already said:

Quote

Also, if you use getKeyText() it always returns CAPITAL so it would be "W" not "w"
why not simply using getKeyChar() ?

so your test on "w" lowercase will never work
Was This Post Helpful? 0
  • +
  • -

#7 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 11:33 AM

ahh great that fixed it :D i just needed a capital W...

thanks anyways u guys have been to great help
Was This Post Helpful? 0
  • +
  • -

#8 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: can't get keyListening to work :( tried for 3 days now

Posted 03 May 2012 - 02:49 PM

Occam's Razor, I suppose.

Glad to hear it's working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1