Results 1 to 4 of 4
  1. #1
    Misryma is offline The New Guy
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default KeyPressed doesn't end when key is released

    When you press Left/Right/Up/Down, the release of that key is not noticed.
    //
    JavaGame - Pastebin.com
    //
    package javaapplication1;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;

    public class JavaGame extends JFrame implements Runnable{

    int x, y, xDirection, yDirection;
    private Image dbImage;
    private Graphics dbg;
    Image face;

    Font font = new Font("Arial", Font.BOLD | Font.CENTER_BASELINE, 30);

    public void run(){
    try{
    while(true){

    move();

    Thread.sleep(10);

    }

    }
    catch(Exception e){
    System.out.println("ERROR");
    }
    }
    public void move(){
    x += xDirection;
    y += yDirection;
    if(x <= 10)
    x = 10;
    if(x >= 200)
    x = 200;
    if(y <= 25)
    y = 25;
    if(y >= 250)
    y = 250;
    }
    public void setXDirection(int xdir){
    xDirection = xdir;
    }
    public void setYDirection(int ydir){
    yDirection = ydir;
    }

    public class AL extends KeyAdapter {
    private int keyCode;
    public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();
    if(keyCode == e.VK_LEFT){
    setXDirection(-1);
    }
    if(keyCode == e.VK_RIGHT){
    setXDirection(+1);
    }
    if(keyCode == e.VK_UP){
    setYDirection(-1);
    }
    if(keyCode == e.VK_DOWN){
    setYDirection(+1);
    }
    }
    public void keyReleased(KeyEvent e){
    if(keyCode == e.VK_LEFT){
    setXDirection(0);
    }
    if(keyCode == e.VK_RIGHT){
    setXDirection(0);
    }
    if(keyCode == e.VK_UP){
    setYDirection(0);
    }
    if(keyCode == e.VK_DOWN){
    setYDirection(0);
    }
    }
    }

    public JavaGame(){
    //Load Image
    ImageIcon i = new ImageIcon("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/javagame/src/javaapplication1/this.gif");
    face = i.getImage();
    //Game Properties
    addKeyListener(new AL());
    setTitle("JavaGame");
    setSize(250, 250);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
    getContentPane().setBackground(Color.cyan);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    x = 150;
    y = 150;
    }

    public void paint(Graphics g){
    dbImage = createImage(getWidth(),getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g){
    super.paint(g);
    g.setFont(font);
    g.setColor(Color.BLUE);
    g.drawString("Is this a game?", 50, 50);
    g.setColor(Color.GREEN);
    g.drawImage(face, x, y, this);

    repaint();
    }

    public static void main(String[] args) {
    JavaGame jg = new JavaGame();
    //Threads
    Thread t1 = new Thread(jg);
    t1.start();

    }

    }
    Last edited by Misryma; 10-25-2013 at 07:39 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: KeyPressed doesn't end when key is released

    Did you put in a print statement at the top of the keyReleased method to verify? And from where are you getting your
    keyCode in that method?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Misryma is offline The New Guy
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: KeyPressed doesn't end when key is released

    I was following the instructions of the guy from "TheJavaHub" on Youtube, and I only got to video 7, so I never thought to add a print statement.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: KeyPressed doesn't end when key is released

    Well, you need to retrieve the keyCode from the KeyEvent object.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Change Image when Keypressed
    By neilq5 in forum AWT / Swing
    Replies: 0
    Last Post: 10-13-2012, 08:42 PM
  2. keyPressed method doesn't work as intended
    By JohnPringle83 in forum New To Java
    Replies: 6
    Last Post: 05-31-2011, 09:22 PM
  3. KeyPressed trouble with KeyListeners
    By CuddlyKittens11 in forum Advanced Java
    Replies: 8
    Last Post: 05-28-2011, 09:31 PM
  4. Problem with KeyPressed any Help is appreciated
    By gryd00 in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 05:15 PM
  5. KeyBinding key released doesn't work so well -.-
    By Addez in forum New To Java
    Replies: 1
    Last Post: 09-22-2010, 12:09 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
  •