1

can you see the problem. the thread runs fine but the brick does not want to respond to the key listener. I try to test if the keylistener was at last getting an event but it does not even do the system.out.println

import java.awt.*;//imports
import java.util.*;
import java.applet.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Start extends Applet implements Runnable, KeyListener// where i put the keylistener in
{

    DrawBackground dbg = new DrawBackground();
    Brick brick = new Brick();
    Thread gameLoop;

    public void init() {
        addKeyListener(this);// i add the key listener
    }

    public void start() {
        Thread gameLoop = new Thread(this);
        gameLoop.start();
    }

    public void run() {
        while (true) {

            brick.update(1);
            repaint();
            try {
                Thread.sleep(17);
            }
            catch (InterruptedException e) {
            }

        }
    }

    public void stop() {

    }

    public void paint(Graphics g)// with out any paint it works if im changing
                                 // somthing like a lable
    {
        dbg.paint(g, this);
        brick.paint(g, this);
    }

    public void keyPressed(KeyEvent e)// test to see if it works
    {
        System.out.println("why");
        if (e.getKeyCode() == 37) {
            brick.left();
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {

    }
}

this is the Brick class the thing i'm trying to move

import java.awt.*;
import java.util.*;
import java.applet.*;

public class Brick {

    public int dy = 40;
    public int yStart = -20;
    public int time = 0;
    public int dx = 0;
    public int xStart = 0;
    public int start = 1;

    public void paint(Graphics g, Start sp) {
        Dimension screenSize = sp.getSize();
        int sheight = screenSize.height;
        int swidth = screenSize.width;
        if (start == 1) {
            xStart = swidth - (int) (swidth / 2.5);
            start = 0;
        }
        int bWidth = swidth / 15;
        int bHeight = swidth / 15;
        int time = 0;
        g.setColor(Color.red);
        g.fillRect(xStart, yStart, bWidth, bHeight);
    }

    public void update(int x) {
        if (time == 60) {
            time = 0;
            yStart += dy;
        }
        else {
            time += x;
        }
    }

    public void left() {
        xStart -= dx;
    }
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
  • Is that thread necessary ? What does it do ? – An SO User Nov 26 '13 at 02:52
  • yes it makes the block move down – ZergRushJoe Nov 26 '13 at 03:04
  • its tetris the game im tring to make – ZergRushJoe Nov 26 '13 at 03:05
  • i can't find the problem – ZergRushJoe Nov 26 '13 at 03:34
  • I've taken your Start class and cut out some of the unnecessary stuff to see if the KeyListener was really working here: https://gist.github.com/dshields1/7653136. This runs fine for me and the KeyListener works. If it doesn't work for you then there might be a problem with the way you are running your applets. If it does work you can try adding back in your code piece by piece to it and see what part is causing trouble. – Dylan Shields Nov 26 '13 at 03:50
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 26 '13 at 05:12
  • ok so the key listener works as long a paint is not in the class – ZergRushJoe Dec 01 '13 at 00:51
  • and can you point to videos or lession on swing – ZergRushJoe Dec 01 '13 at 00:54

1 Answers1

1

It doesn't look like you have set the keyboard focus. See this question.

I've always used setFocusable(true) after adding the keyListener and its worked for me, but the answer to that question has a better solution.

Community
  • 1
  • 1
Dylan Shields
  • 158
  • 1
  • 8