[java] Focus and KeyListener problems (beginner)

Started by
1 comment, last by Paranoia_uk 21 years, 7 months ago
Hello people. I'm fiddling with some event handling stuff at the moment and seem to have got pretty stuck with KeyListener. I've got an application with two classes. The idea of the program is simply to have the shape move down the window on a timer event. And, if the user presses a direction key the shape should rotate. Yes it's a basic Tetris thingy. The first class is TestTimer with the follwing code:
// TestTimer.java: Test the timer object
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestTimer extends JFrame implements ActionListener, KeyListener
{
  private DrawingPanel drawingPanel;
  private Timer timer;

  public static void main(String[] args)
  {
    TestTimer frame = new TestTimer();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
    frame.requestFocus();
  }

  public TestTimer()
  {
    setTitle("Test Timer");

    drawingPanel = new DrawingPanel();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(drawingPanel, BorderLayout.CENTER);

    getContentPane().addKeyListener(this);

    timer = new Timer(1000, this);
    timer.start();

  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == timer)
    {
      int y = drawingPanel.getYCoordinate();
      drawingPanel.setYCoordinate(y+10);
      repaint();
    }
  }

  public void keyPressed(KeyEvent e)
  {
    switch (e.getKeyCode())
    {
      case KeyEvent.VK_UP:      drawingPanel.setDirection(1);
                                break;
      case KeyEvent.VK_DOWN:    drawingPanel.setDirection(2);
                                break;
      case KeyEvent.VK_LEFT:    drawingPanel.setDirection(3);
                                break;
      case KeyEvent.VK_RIGHT:   drawingPanel.setDirection(4);
                                break;
    }
    repaint();
  }

  public void keyReleased(KeyEvent e)
  {
  }

  public void keyTyped(KeyEvent e)
  {
  }
}  
The second class is the one to create the DrawingPanel and has the following code:
// DrawingPanel.java: Create a panel for drawing objects   on
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DrawingPanel extends JPanel
{
  // (x, y) coordinates where the message is displayed
  private int xCoordinate = 50;
  private int yCoordinate = 20;

  private String shape = "L1";
  private int direction = 3;

  // Default constructor
  public DrawingPanel()
  {
  }

  public void focus()
  {
    this.requestFocus();
  }

  public int getXCoordinate()
  {
    return xCoordinate;
  }

  public void setXCoordinate(int x)
  {
    this.xCoordinate = x;
  }

  public int getYCoordinate()
  {
    return yCoordinate;
  }

  public void setYCoordinate(int y)
  {
    this.yCoordinate = y;
  }

  public void setDirection(int dir)
  {
    this.direction = dir;
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    // draw shape
  }
}  
I'm unsure of the exact problem. I don't know whether it's because of problems getting focus or with the key events themselves. Can anyone help? Cheers in advance. [edited by - Paranoia_uk on August 26, 2002 9:27:07 AM]
Advertisement
Yeah, this is a problem with focus. You don''t actually say which version of the JRE you are running this under(have you tried running it on something before 1.4?), but I assume it''s version 1.4 of the JRE.

There are one of two things you can do here, either add your key listener to the main JFrame(as that by default will have the focus), or shift the focus to the content pane. The call for this will probably be requestFocus() (ie. getContentPane().requestFocus()).
Either of those should work!

Jiim Jonez.
--
www.javage.net
Yeah, I''m running 1.4
And after some more time fiddling this evening I''ve got it working at last

This topic is closed to new replies.

Advertisement