Dvole Dvole - 1 year ago 101
Java Question

Java keylistener and action on object, what's wrong?

Here is my code, what i'm trying to do is to move that rectangle with a key press. Questions - how do i specify it on arrow keys and why it won't allow me to work it that way? It underlines

my paddle object in red in KeyPressed event and won't run.

import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
public class BreakOut extends GraphicsProgram {
/** Runs the program */
public void run() {

GRect paddle = new GRect(200, 400, 100, 20);


public void keyPressed(KeyEvent e){


Answer Source

paddle is a local variable in the run() method. It is not accessible from the keyPressed(KeyEvent e) method.

You probably want to turn paddle into a field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download