Results 1 to 5 of 5
  1. #1
    Kaizo is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Help with keylistener?

    Problem: My square won't move.
    Honesty: I fell asleep on my keyboard and probably deleted something.
    Also, this is not my program, as I am still learning Java, and my mentor has moved.

    Solution: Hopefully, all of you.


    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.Timer;

    public class InputExample extends JPanel implements ActionListener, KeyListener{

    boolean keyUp_DOWN;
    boolean keyDown_DOWN;
    boolean keyLeft_DOWN;
    boolean keyRight_DOWN;
    boolean keyDown;

    int x = 150;
    int y = 300;
    int winX = 600;
    int winY = 600;
    int timerCount = 0;


    Rectangle player1; //the "player" object
    Rectangle bgRectangle = new Rectangle(20,20,winX,winY); //the background color/image

    Rectangle big, small, player2;

    int vx, vy; //velocity(speed) the player will move at
    Color bgColor = new Color (0,0,0);
    Color playerColor = new Color(255,19,6);
    JTextArea inputzone; //This is what actually processes the input from the keyboard



    public InputExample(){
    setSize(winX, winY); //set the size of the window using the variables above


    /*
    * The next five lines set up the inputzone to read keyboard input
    * and add it into the game.
    */
    inputzone = new JTextArea();
    inputzone.setEditable(false);
    inputzone.addKeyListener(this);
    inputzone.setSize(0, 0);
    this.add(inputzone);


    player1 = new Rectangle(50,50,20,20); //set the dimensions of the player rectangle


    keyUp_DOWN = false;
    keyDown_DOWN = false;
    keyLeft_DOWN = false;
    keyRight_DOWN = false;


    Action updateTimer = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
    System.out.println(timerCount);
    timerCount++;
    }
    };
    Timer t = new Timer(1000, updateTimer);
    t.start();


    //The default values for the velocity is 0
    vx = 0;
    vy = 0;

    }

    public void updateTimer(){
    timerCount++;
    }



    public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D)g;



    g2.setColor(bgColor); //these two lines paint the background.
    g2.fill(bgRectangle);

    g2.setColor(playerColor); //these two lines create the player
    g2.fill(player1);




    }

    //this handles the movement of the player object.
    //Modify these values based on the input below

    //player1.translate(vx, vy); <!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***!*!!**!*!**!* !*!**!**!*!

    //??????????????????^^^^^^************************** *********()()())()()()()()()()()()()()()()()()()** *******<<<>
    //Am I missing my translate method or... -_-' ???
    //If so, what do I do o.O ???

    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {

    }

    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
    keyDown = true;
    if(e.getKeyChar()=='a')
    keyLeft_DOWN = true;
    else if(e.getKeyChar()=='d')
    keyRight_DOWN = true;
    else if(e.getKeyChar()=='w')
    keyUp_DOWN = true;
    else if(e.getKeyChar()=='s')
    keyDown_DOWN = true;
    handleInput(e);
    }


    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
    keyDown = false;
    if(e.getKeyChar()=='a')
    keyLeft_DOWN = false;
    else if(e.getKeyChar()=='d')
    keyRight_DOWN = false;
    else if(e.getKeyChar()=='w')
    keyUp_DOWN = false;
    else if(e.getKeyChar()=='s')
    keyDown_DOWN = false;

    handleInput(e);
    }

    /** Handle the button click. */
    public void actionPerformed(ActionEvent e) {
    inputzone.requestFocusInWindow();
    repaint();
    }



    /*
    * This is where the input is handled. It modifies the velocities.
    */
    public void handleInput(KeyEvent e){

    if(keyDown)
    {
    if(keyDown_DOWN)
    vy = 9;
    if(keyUp_DOWN)
    vy = -9;
    if(keyLeft_DOWN)
    vx = -9;
    if(keyRight_DOWN)
    vx = 9;
    }
    else
    {
    if(!keyDown_DOWN)
    vy = 0;
    if(!keyUp_DOWN)
    vy = 0;
    if(!keyLeft_DOWN)
    vx = 0;
    if(!keyRight_DOWN)
    vx = 0;
    }
    }



    public static void main(String[] args){
    JFrame testFrame = new JFrame();
    testFrame.setSize(600,600);
    InputExample gd = new InputExample();

    testFrame.add(gd);
    testFrame.setVisible(true);
    gd.setVisible(true);

    Timer t = new Timer(10, gd);
    t.start();
    }

    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default

    When posting code, it should be in the form of an SSCCE. Eliminate everything except the lines that cause the problem. Don't forget the code tags to preserve formatting, or else nobody is going to want to read it.

    What happens when you run the program? Does the square display, but not move? Do you get input from your keyboard, but nothing happens? Does the square move in the wrong direction? Doe something else happen?

  3. #3
    Kaizo is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Sorry

    Ok, I'll change the format, thanks. As to what happens - a window opens and siplays a white-gray background almost completely covered up by a black square (depending on how i size my window; i usually size it to fit the black square). There is a small red square in the upper left corner of the black square. When i press the right and left arrow keys, nothing happens. When I press the up and down arrow keys, I get an error message each time i press it (very long). I will copy the message and post it. There is also a timer running in the command window.

  4. #4
    Kaizo is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Only one problem - i don't know what causes the problem ( well, i'm assuming its due to not having a method where it needs to be)

  5. #5
    mariob316 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    you're missing quite a bit of code still for this to work, how are you getting the x and y position of the square? and your vx and vy, i dnt see how they are being applied to the square.

    look at this tutorial, it will help you
    Moving object with keyboard

Similar Threads

  1. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 12:25 AM
  2. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM

Tags for this Thread

Posting Permissions

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