0

I have a frame which as a rectangle in it. I want to know how can I move the rectangle in if I clicked the arrow keys. I searched, and find few examples but nothing worked (weird, as it should be a simple thing to do)

Here is my Rectangle class:

 public class PlayerOne implements KeyListener {

    int x,y;
    public PlayerOne(JPanel panel){
        this.x = panel.getWidth()/2;
        this.y = panel.getHeight()/2;
    }

    public void paint(Graphics g){
        g.setColor(Color.RED);
        g.fillRect(125, 480, 60, 10);
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        int keyCode = arg0.getKeyCode();
        if(keyCode == arg0.VK_KP_RIGHT){
            this.x+=5;
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub          
    }    
}

This is the main:

public class PingPong extends JPanel {

    private static final long serialVersionUID = -4170574729049260633L;

    //Initialize
    Table table = new Table();
    PlayerOne po = new PlayerOne(this);

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        table.paint(g);
        po.repaint(g);
    }

    public static void main(String[] args){
        JFrame frame = new JFrame();            
        frame.setTitle("Pong");
        frame.setSize(326, 533);
        frame.add(new PingPong()).setBackground(Color.DARK_GRAY);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        frame.setVisible(true);
    }       
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
John Doe
  • 93
  • 2
  • 8
  • You need a `KeyListener`. – Spikatrix May 14 '15 at 12:39
  • @CoolGuy I Added one, still, I have no idea how can I make the rectangle move... I edited my post, can you take a look please? – John Doe May 14 '15 at 12:41
  • by using KeyBindigs instead of KeyListener, paint should be paintComponent (with super. paintComponent) and not member of KeyListener or whatever but real JComponent, e.g. JPanel, painting should be initialized by repaint() from action not from itself (can causing endless loop) from paintComponent – mKorbel May 14 '15 at 12:44
  • Try changing the name of `paint` to `repaint` in `PlayerOne`. And here `this.x+=5;` you just increase the value of `x`. Nothing in your program uses it. How do you expect the rectangle to move? Perhaps you need to change the first argument of `fillRect` to `x`? – Spikatrix May 14 '15 at 12:47
  • @CoolGuy Okay, I did, but still, it won't move. Please consider that I'm very new to Java Graphics and Java in general. – John Doe May 14 '15 at 12:50
  • @CoolGuy Okay, I got it, How can I repaint the rectangle after the increasing of the value? – John Doe May 14 '15 at 12:51
  • I just did this with a circle. Replace the circle with a rectangle. http://stackoverflow.com/a/30111507/300257 – Gilbert Le Blanc May 14 '15 at 15:52

1 Answers1

2

There's a bunch of problems here:

The problem is that your rectangle drawing is hardcoded, evidenced here:

public void paint(Graphics g){
    g.setColor(Color.RED);
    g.fillRect(125, 480, 60, 10);
}

You need to use your x variable instead of 125

In order to accept key press events, your JPanel needs to accept focus, which can be achieved with the following lines:

setFocusable(true);
requestFocusInWindow();

You will now receive keyboard events and alter your x value. Unfortunately this won't trigger a repaint so your box still won't move.

You should really break apart your classes a bit more as your allocation of responsibilities is a bit strange. When a key event occurs, you need to tell your JPanel to repaint() itself to updates are reflected on screen.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • I changed it, It still won't move.. How can I repaint PlayerOne after increasing x? – John Doe May 14 '15 at 12:52
  • [for example](http://stackoverflow.com/a/7940227/714968) to the question (SwingTimer) about stepping painting for @CoolGuy too – mKorbel May 14 '15 at 12:55