Hi, I'm trying to program Tetris. The thing is, I can't seem to get the Keylistener to work in the JApplet, and I have no idea why :(
Here's my code:

import java.util.Random;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * Class Tetris - write a description of the class here
 * 
 * @author (Neil Semmel) 
 * @version (Nov. 7, 2008)
 */
public class Tetris extends JApplet implements KeyListener
{
    Random randy = new Random();
    shapes test = new shapes();
    
    public int x_pos = 220;
    public int y_pos = 0;
    
    public void init()
    {
        this.addKeyListener(this);
    }
    
    public void paint(Graphics g)
    {
        getPiece(g);
    }
    
    // ----- Key Events -----
    public void keyPressed(KeyEvent e) 
    {
        char code = e.getKeyChar();
        if(code == 'a')
        x_pos = x_pos-20;
        if(code == 'd')
        x_pos = x_pos+20;
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    // ----- End Key Events -----
    
    public void reDraw(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,500,500);
    }
    
    public void sleep(int time)
    {
        long now = System.currentTimeMillis();
        while(now+time > System.currentTimeMillis());
    }
    
    public void getPiece(Graphics g)
    {
        int pieceNum = randy.nextInt(7);
        switch(pieceNum)
        {
            case 0:
            test.drawPiece1(g,x_pos,y_pos);
            break;
            
            case 1:
            test.drawPiece2(g,x_pos,y_pos);
            break;
            
            case 2:
            test.drawPiece3(g,x_pos,y_pos);
            break;
            
            case 3:
            test.drawPiece4(g,x_pos,y_pos);
            break;
            
            case 4:
            test.drawPiece5(g,x_pos,y_pos);
            break;
            
            case 5:
            test.drawPiece6(g,x_pos,y_pos);
            break;
            
            case 6:
            test.drawPiece7(g,x_pos,y_pos);
            break;
        }
    }
        
}

Recommended Answers

All 7 Replies

What do you mean by "can't get it to work"? Because your key listener does respond as long as you make sure it has focus (i.e. click in the panel). You can add a call to requestFocusInWindow() in init() if you want to make sure it gets focus without the click.

I couldn't say if any of the other parts work because they reference variables and classes that do not exist in the code you posted. I just commented those out when I ran it.

Ok, so, I just looked back through my code, and pressing a or d wouldn't do anything, the way i set it up >.< So, I replaced the getPiece() method in the paint method with drawRect(x_pos,y_pos, 25,25); , but the KeyListener still didn;t change anything. (and yes, i did add the requestFocusInWindow(), it didn't do anything :( )

I just added System.out.println() calls in your KeyListener and ran it in both the applet viewer and a web page and they worked fine as long as it had the focus.

You need to add a call to repaint() after you update your coordinates in the KeyListener.

Hmm, neither work for me, even after adding in repaint(). I normally use BlueJ as my compiler, but i used NetBeans also, and it didn't work on that either....

I was looking for a similar solution as am upgrading an old applet, and came across your question.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Hi, I'm trying to program Tetris. The thing is, I can't seem to get the Keylistener to work in the JApplet, and I have no idea why :(
Here's my code:
... Snipped ...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
As no one has yet answered this properly here, I will document what I did:

1) Implement your applet with KeyEventDispatcher interface. public class SnakeApplet extends JApplet implements java.awt.KeyEventDispatcher { 2) The compiler will insist that you implement method: public boolean dispatchKeyEvent(java.awt.event.KeyEvent evt) There you put your processing on the key events.

3) The dispatcher has to be registered with the KeyboardFocusManager KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); which would normally be done in the applet init() method.
In our case the applet is the dispatcher so use "this".

Hope this helps. I might be posting his solution on other sites which I came across.

Thanks! I found a different way to do it, but i lost the jump drive the file was on... and cant remember how i did it...
But again, thanks for letting me know how to do it! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.