0

This is my first time using KeyListener so excuse me if I made an obvious mistake. My problem is that whenever I press the arrow keys or WASD keys, the ellipses doesn't react. I know there are other flaws in my code but I want to just focus on KeyListener issue.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.KeyListener;

public class idkdude extends JPanel implements ActionListener, KeyListener {

    Timer t = new Timer(5, this);
     int rx = 395, ry = 395, rvelx = 0, rvely = -2, bx = 195, by = 195, bvelx =     0, bvely = 2;
     boolean redWins = false;
     boolean blueWins = false;

     public void paint(Graphics g)
        {
            super.paint(g);
             drawSurface(g);
            Graphics2D g2 = (Graphics2D) g;             
            g.setColor(Color.red);
            Ellipse2D rcircle = new Ellipse2D.Double(rx, ry, 10, 10);
            g2.fill(rcircle);
            g2.drawLine(400, 400, rx+5, ry+5);
            g.setColor(Color.blue);
            Ellipse2D bcircle = new Ellipse2D.Double(bx, by, 10, 10);
            g2.fill(bcircle);
            g2.drawLine(200, 200, bx+5, by+5);
            if (redWins == true)
            {
                redWins(g);
            }
            if (blueWins == true)
            {
                blueWins(g);
            }
            t.start();
        }

     public void init()
     {
         addKeyListener(this);
     }
     public void keyReleased(KeyEvent e){}
        public void keyTyped(KeyEvent e) {}
        public void keyPressed(KeyEvent e)
        { //make sure to put
            //rx+=rvelx;
            //ry+=rvely;
            // and draw a new line
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_LEFT) 
            {rvelx = -2;rvely = 0;}
            if (key == KeyEvent.VK_RIGHT)
            {rvelx = 2; rvely = 0;}
            if (key == KeyEvent.VK_UP)
            {rvelx = 0; rvely = -2;}
            if (key == KeyEvent.VK_DOWN)
            {rvelx = 0; rvely = 2;}
            if (key == KeyEvent.VK_W)
            {bvelx = 0; rvely = -2;}
            if (key == KeyEvent.VK_A)
            {bvelx = -2; rvely = 0;}
            if (key == KeyEvent.VK_S)
            {bvelx = 0; rvely = 2;}
            if (key == KeyEvent.VK_D)
            {bvelx = 2; rvely = 0;}
        }
     public void actionPerformed(ActionEvent e)
        {
            rx+=rvelx;
            ry+=rvely;
            bx+=bvelx;
            by+=bvely;
            if (rx> 600 || rx <0)
            {
                blueWins = true;
            }

            if (ry<0 || ry>600)
            {
                blueWins = true;
            }
            if (bx>600 || bx<0)
            {
                redWins = true;
            }
            if (by>600 || by<0)
            {
                redWins = true;             
            }                
            repaint();              
        }

     public void redWins(Graphics g)
     {
         g.setColor(Color.black);
            g.fillRect(0,0,600,600);
            g.setColor(Color.white);
            g.drawString("RED HAS WON!!!!!", 250, 250);
            g.drawString("Click Anywhere to Start Another Game", 250, 350);  
     }
     public void blueWins(Graphics g)
     {
         g.setColor(Color.black);
            g.fillRect(0,0,600,600);
            g.setColor(Color.white);
            g.drawString("BLUE HAS WON!!!!!", 250, 250);
            g.drawString("Click Anywhere to Start Another Game", 250, 350);
     }
     public void drawSurface(Graphics g)
     {
        g.setColor(Color.black);
        g.fillRect(0,0,600,600);
        g.setColor(Color.white);
        for (int x = 0; x <= 600; x+=25)
            g.drawLine(x,0,x,600);
        for (int y= 0; y <=600; y+=25)
            g.drawLine(0,y,600,y);
     }
}
3
  • 2
    Do you ever call the init method? Also, since you're using a JPanel, you should use Key Bindings instead.
    – TNT
    May 21, 2015 at 16:56
  • 1
    Maybe at the end: repaint(100L); to force a screen update a bit later.
    – Joop Eggen
    May 21, 2015 at 17:00
  • See this. May 22, 2015 at 1:31

1 Answer 1

0

Key bindings over KeyListener

IMHO using Key bindings is a more flexible and reliable approach that brings with these benefits:

Having said that, you can create KeyStrokes like follows:

KeyStroke upKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
KeyStroke leftKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
KeyStroke downKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
KeyStroke rightKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);

You can also specify that the key stroke will be triggered on a key release event:

KeyStroke upReleasedKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);

Note: 0 means no modifiers and true is a flag indicating the key stroke represents a key release event. See the API for more details.

Finally you should attach the different actions to the panel's input map using WHEN_IN_FOCUSED_WINDOW condition (a panel is not focusable).

See also this topic: Key bindings vs. key listeners in Java, and How to Use Key Bindings tutorial

Doing custom painting

Please note that you should not override paint(...) method but paintComponent(...) instead. See A Closer Look at the Paint Mechanism

Not the answer you're looking for? Browse other questions tagged or ask your own question.