Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: event programming - controlling characters with arrow/keyboard keys

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default event programming - controlling characters with arrow/keyboard keys

    hi,
    i'm writing a user-controlled game for a project, but somehow I can't seem to enter anything (as in, when user presses a key nothing happens). is there anything that i'm missing/should learn to fix this? thanks in advance




     
    import java.util.Scanner;
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.util.ArrayList;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Game extends JFrame implements KeyListener {
        //public void init() {
          //  this.addKeyListener(this);
        // }
        public void keyPressed(KeyEvent e) {
            double hx = 100, hy = 150; // starting (x,y) coordinate position
            double px = 15.0, py = 150; // starting (x,y) coordinate position
            // hades controls
            if ((e.getKeyCode() == KeyEvent.VK_UP) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hy += 2; 
            if ((e.getKeyCode() == KeyEvent.VK_LEFT) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hx -= 2; 
            if ((e.getKeyCode() == KeyEvent.VK_DOWN) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hy -= 2;
            if ((e.getKeyCode() == KeyEvent.VK_RIGHT) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hx += 2;
            // persephone controls
            if (e.getKeyCode() == KeyEvent.VK_W) py += 2;
            if (e.getKeyCode() == KeyEvent.VK_A) px -= 2;
            if (e.getKeyCode() == KeyEvent.VK_S) py -= 2;
            if (e.getKeyCode() == KeyEvent.VK_D) px += 2;
            // draw stuff, REMEMBER EXCEPTIONS (WITH WALL)
            StdDraw.setPenColor(StdDraw.BLACK);
            StdDraw.filledSquare(hx,hy,15); // hades; pen radius is 0.5
            StdDraw.filledSquare(px,py,15); // persephone
            StdDraw.show();
        }
        public void keyReleased(KeyEvent e) {
        }
        public void keyTyped(KeyEvent e) { // if typed b, then quit
        }
        public void newGame(String command) { // ... code omitted
        }
        /**
         * empty constructor for Game class
         */
        public static void Game(String[] args)
        {
            //code
        } 
    }

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: event programming - controlling characters with arrow/keyboard keys

    Add some print statements to see if any of the listeners are called.

    To receive key presses, the component must have the focus.

    Note: A constructor does not return void.

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: event programming - controlling characters with arrow/keyboard keys

    thanks for the note! I was wondering if there should be some kind of wait method called when i draw the characters? (this is the case in python, and i'm unsure if it translates to java)

  4. #4
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: event programming - controlling characters with arrow/keyboard keys

    Sorry, I don't know anything about the StdDraw class.
    With the Java SE classes, you do not use any wait methods.

  5. #5
    Junior Member
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: event programming - controlling characters with arrow/keyboard keys

    ah ok thank you though, i'll go read up on JPanel/key listeners
    it (i.e. making the components focusable) should work even if it isn't a textbox, right? because i am using objects of my own designed classes to interact with each other

  6. #6
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: event programming - controlling characters with arrow/keyboard keys

    Yes most components can receive the focus. Some may require some enabling.

Similar Threads

  1. How can I rotate an image using arrow keys?
    By DPaul1994 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 15th, 2014, 12:03 PM
  2. [SOLVED] How to get the arrow keys to move rectangle automatically
    By Edmacelroy in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2013, 10:11 PM
  3. Please help with Arrow Keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:58 PM
  4. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  5. [SOLVED] Making an image move with arrow keys
    By Clex19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 6th, 2012, 09:45 AM

Tags for this Thread