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 4 of 4

Thread: Keylistener in a loop?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Keylistener in a loop?

    Hi, I am trying to program a version of the "Worlds Hardest Game" using ready to program and applets for a final class project. Unfortunately I cannot figure out how to get my keylistener to work because I am using a loop for the enemies to go back and forth. Can anyone help?

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class HardestGame extends Applet
        implements KeyListener, MouseListener
    {
        final int WIDTH = 400;
        final int HEIGHT = 123;
        int myX = 400;
        int myY = 400;
        int PlayerX = 40; // These are beginning coordinates for the
        int PlayerY = 80; // Player's circle
     
        int squareLeft = 250;
        int SQUARE_SIZE = 250;
        int squareTop = 250;
        Button startGame = new Button ("Start Game");
        Canvas display = new Canvas ();
     
     
     
        public void init ()
        {
            super.init ();
            setLayout (new BorderLayout ());
            int windowWidth = getSize ().width;
            int windowHeight = getSize ().height;
            display.resize (windowWidth, windowHeight);
            setBackground (Color.white);
            addKeyListener (this);
            addMouseListener (this);
            add ("North", startGame);
            add ("Center", display);
        }
     
     
     
     
     
        public boolean action (Event e, Object o)
        {
     
            final int RADIUS = 7;
            final int RADIUS2 = 7;
            final int RADIUS3 = 7;
            final int RADIUS4 = 7;
            Graphics displayG;
            int x1, y1, dx, dy, x2, y2, x3, y3, x4, y4, dx2, dy2, dx3, dy3, dx4, dy4;
            displayG = display.getGraphics ();
     
            x1 = 195;
            y1 = 67;
            x2 = 195;
            y2 = 107;
            x3 = 195;
            y3 = 127;
            x4 = 195;
            y4 = 167;
            drawBall (displayG, x1, y1, RADIUS, Color.blue);
            drawBall2 (displayG, x2, y2, RADIUS2, Color.blue);
            drawBall3 (displayG, x3, y3, RADIUS3, Color.blue);
            drawBall4 (displayG, x4, y4, RADIUS4, Color.blue);
            dx = 1;
            dy = 0;
            dx2 = -1;
            dy2 = 0;
            dx3 = 1;
            dy3 = 0;
            dx4 = -1;
            dy4 = 0;
     
            for (int i = 0 ; i < 1000000 ; i++) //loops for how long it will move
            {
                drawBall (displayG, x1, y1, RADIUS, getBackground ());
                x1 += dx; //movement of the ball
                y1 += dy; //movement of the ball
                if (x1 <= RADIUS || x1 >= WIDTH - RADIUS)
                    dx = -dx; //determine which direction
                if (y1 <= RADIUS || y1 >= HEIGHT - RADIUS)
                    dy = -dy; //determine which direction
                drawBall (displayG, x1, y1, RADIUS, Color.blue); //draw the ball
     
                drawBall2 (displayG, x2, y2, RADIUS2, getBackground ());
                x2 += dx2; //movement of the ball
                y2 += dy2; //movement of the ball
                if (x2 <= RADIUS2 || x2 >= WIDTH - RADIUS2)
                    dx2 = -dx2; //determine which direction
                if (y2 <= RADIUS2 || y2 >= HEIGHT - RADIUS2)
                    dy2 = -dy2; //determine which direction
                drawBall2 (displayG, x2, y2, RADIUS2, Color.blue); //draw the ball
     
                drawBall3 (displayG, x3, y3, RADIUS3, getBackground ());
                x3 += dx3; //movement of the ball
                y3 += dy3; //movement of the ball
                if (x3 <= RADIUS2 || x3 >= WIDTH - RADIUS3)
                    dx3 = -dx3; //determine which direction
                if (y3 <= RADIUS2 || y3 >= HEIGHT - RADIUS3)
                    dy3 = -dy3; //determine which direction
                drawBall3 (displayG, x3, y3, RADIUS3, Color.blue); //draw the ball
     
                drawBall4 (displayG, x4, y4, RADIUS4, getBackground ());
                x4 += dx4; //movement of the ball
                y4 += dy4; //movement of the ball
                if (x4 <= RADIUS || x4 >= WIDTH - RADIUS4)
                    dx4 = -dx4; //determine which direction
                if (y4 <= RADIUS2 || y4 >= HEIGHT - RADIUS3)
                    dy4 = -dy4; //determine which direction
                drawBall4 (displayG, x4, y4, RADIUS4, Color.blue); //draw the ball
     
     
                ballDelay (3000000); // Wastes time to produce delay.
     
                remove (startGame);
            }
            return false;
        } // action method
     
     
        public void keyPressed (KeyEvent e)
        {
            int key = e.getKeyCode ();
     
            if (key == KeyEvent.VK_LEFT)
            {
                squareLeft -= 5;
                repaint ();
                PlayerX -= 5;
            }
            else if (key == KeyEvent.VK_RIGHT)
            {
                PlayerX += 5;
                repaint ();
                squareLeft += 5;
                repaint ();
            }
     
     
            else if (key == KeyEvent.VK_UP)
            {
                PlayerY -= 5;
                squareTop -= 5;
                repaint ();
            }
     
     
            else if (key == KeyEvent.VK_DOWN)
            {
                PlayerY += 5;
                squareTop += 5;
                repaint ();
            }
     
     
        }
     
     
        public void keyReleased (KeyEvent e)
        {
        }
     
     
        public void keyTyped (KeyEvent e)
        {
        }
     
     
        public void mouseEntered (MouseEvent e)
        {
        }
     
     
        public void mouseExited (MouseEvent e)
        {
        }
     
     
        public void mousePressed (MouseEvent e)
        {
        }
     
     
        public void mouseReleased (MouseEvent e)
        {
        }
     
     
        public void mouseClicked (MouseEvent e)
        {
        }
     
     
        public void paint (Graphics g)
        {
     
     
            if (PlayerX < 90 && PlayerX >= 8 && PlayerY > 50 && PlayerY < 220)
            {
                if (PlayerY < 60)
                    PlayerY = 60;
                if (PlayerX < 14)
                    PlayerX = 14;
                if (PlayerX > 70)
                    PlayerX = 70;
            }
            if (PlayerX > 10 && PlayerX < 170 && PlayerY > 200 && PlayerY < 260)
            {
                if (PlayerY > 240)
                    PlayerY = 240;
                if (PlayerX < 16)
                    PlayerX = 16;
                if (PlayerX > 150)
                    PlayerX = 150;
            }
            if (PlayerX > 89 && PlayerX < 130 && PlayerY > 210)
            {
                if (PlayerY < 220)
                    PlayerY = 220;
            }
            if (PlayerX > 122 && PlayerX < 170 && PlayerY < 220)
            {
                if (PlayerX < 130)
                    PlayerX = 130;
                if (PlayerY < 100)
                    PlayerY = 100;
            }
            if (PlayerX >= 170 && PlayerX < 490 && PlayerY > 92 && PlayerY < 210)
            {
                if (PlayerY > 200)
                    PlayerY = 200;
                if (PlayerY < 100)
                    PlayerY = 100;
            }
            if (PlayerX > 460 && PlayerX < 518 && PlayerY > 80)
            {
                if (PlayerX > 510)
                    PlayerX = 510;
                if (PlayerY > 200)
                    PlayerY = 200;
            }
            if (PlayerX > 480 && PlayerX < 650 && PlayerY > 40 && PlayerY < 98)
            {
                if (PlayerX < 490)
                    PlayerX = 490;
                if (PlayerY < 60)
                    PlayerY = 60;
            }
            if (PlayerX > 510 && PlayerX < 570 && PlayerY > 60 && PlayerY < 110)
            {
                if (PlayerY > 80)
                    PlayerY = 80;
            }
     
     
     
     
        }
     
     
        public void drawBall (Graphics g, int x, int y, int radius, Color clr)
        {
            g.setColor (clr); //clears where the ball has moved
            g.fillOval (x - radius + 130, y - radius + 50, 2 * radius, 2 * radius); //determines the boundaries of the ball
     
     
     
            g.setColor (Color.black);
            g.fillRect (10, 60, 2, 200);
            g.fillRect (10, 60, 80, 2);
            g.fillRect (90, 60, 2, 160);
            g.fillRect (10, 260, 160, 2);
            g.fillRect (90, 220, 40, 2);
            g.fillRect (130, 100, 2, 122);
            g.fillRect (130, 100, 360, 2);
            g.fillRect (490, 60, 2, 42);
            g.fillRect (170, 220, 2, 42);
            g.fillRect (170, 220, 360, 2);
            g.fillRect (530, 100, 2, 122);
            g.fillRect (530, 100, 40, 2);
            g.fillRect (570, 100, 2, 160);
            g.fillRect (570, 260, 80, 2);
            g.fillRect (650, 60, 2, 202);
            g.fillRect (490, 60, 160, 2);
     
            g.setColor (new Color (190, 255, 190));
            g.fillRect (12, 62, 78, 198);
            g.fillRect (572, 62, 78, 198);
     
            g.setColor (Color.RED);
            g.fillRect (PlayerX, PlayerY, 20, 20);
        }
     
     
        public void drawBall2 (Graphics g, int x2, int y2, int radius2, Color clr)
        {
            g.setColor (clr); //clears where the ball has moved
            g.fillOval (x2 - radius2 + 130, y2 - radius2 + 40, 2 * radius2, 2 * radius2); //determines the boundaries of the ball
        } // drawBall method
     
     
        public void drawBall3 (Graphics g, int x3, int y3, int radius3, Color clr)
        {
            g.setColor (clr); //clears where the ball has moved
            g.fillOval (x3 - radius3 + 130, y3 - radius3 + 50, 2 * radius3, 2 * radius3); //determines the boundaries of the ball
     
        } // drawBall method
     
     
        public void drawBall4 (Graphics g, int x4, int y4, int radius4, Color clr)
        {
            g.setColor (clr); //clears where the ball has moved
            g.fillOval (x4 - radius4 + 130, y4 - radius4 + 40, 2 * radius4, 2 * radius4); //determines the boundaries of the ball
        } // drawBall method
     
     
        public void ballDelay (int howLong)
        {
            // This wastes time.
            for (int i = 1 ; i <= howLong ; i++)
            {
                double garbage = Math.PI * Math.PI;
            }
        } // delay method
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Keylistener in a loop?

    What do you mean when you say you can't get it to work? Does the code compile? Does it run? Does it throw an Exception? Does it result in some strange behavior? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Keylistener in a loop?

    Sorry I should have clarified. The program runs and the enemy objects move back and forth like they're suppose, but the keylistener isn't being reached I don't think because my user object can't move.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Keylistener in a loop?

    Does the component with the KeyListener have focus? Are the KeyListener methods never called at all, or are they called but then the if statements aren't entered?

    I recommend creating an MCVE to debug instead of trying to debug your whole project.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. KeyListener Problem
    By jibby in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2014, 09:28 PM
  2. KeyListener
    By tim8w in forum AWT / Java Swing
    Replies: 9
    Last Post: January 25th, 2013, 01:40 AM
  3. implementing KeyListener
    By FARGORE in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2012, 08:32 AM
  4. KeyListener bug
    By nivangerow in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2011, 06:10 AM
  5. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM