MegaVako
MegaVako

Reputation: 43

How to use ActionListener correctly?

I am trying to make a "simple tetras" game as a beginning coder. As the code shown below, the white block can move by pressing arrow keys while fails to do (y = y + 10) within the Timer. My guess is that the ActionListener is placed in the wrong position. All I want to do is to be able to move the block left and right as it descends.

Here's my code:

import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Experiment extends JFrame {
    int x = 100;
    int y = 100;
    ASD exp = new ASD();

    public Experiment() {
        add(exp);
        exp.setFocusable(true);
    }

    public class ASD extends JPanel {
        public ASD() {
            addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        //case KeyEvent.VK_DOWN: y += 10;break;
                        //case KeyEvent.VK_UP: y -= 10; break;
                        case KeyEvent.VK_LEFT:
                            x -= 10;
                            break;
                        case KeyEvent.VK_RIGHT:
                            x += 10;
                            break;
                    }
                    repaint();
                }
            });
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(Color.BLACK);

            g.setColor(Color.WHITE);
            g.fillRect(x, y, 30, 30);
        }

        public class Movement extends JPanel implements ActionListener {
            Timer tm = new Timer(5, this);

            public void actionPerformed(ActionEvent e) {
                y = y + 10;
                repaint();
            }
        }
    }

    public static void main(String[] args) {
        Experiment frame = new Experiment();
        frame.setTitle("ASD");
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Upvotes: 4

Views: 168

Answers (1)

Bill
Bill

Reputation: 4646

The main problems with the code were that

  1. The timer needs to be started with the call tm.start()
  2. After the block "falls", it needs to be repainted.

Here's a working example of the class ASD

   public class ASD extends JPanel implements ActionListener {

        private Timer tm;

        public ASD() {
            addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        //case KeyEvent.VK_DOWN: y += 10;break;
                        //case KeyEvent.VK_UP: y -= 10; break;
                        case KeyEvent.VK_LEFT:
                            x -= 10;
                            break;
                        case KeyEvent.VK_RIGHT:
                            x += 10;
                            break;
                    }
                    repaint();
                }
            });

            tm = new Timer(200, this);
            tm.setRepeats(true);
            tm.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            y = y + 10;
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(Color.BLACK);

            g.setColor(Color.WHITE);
            g.fillRect(x, y, 30, 30);
        }
    }

Upvotes: 3

Related Questions