0

I started learning java as a hobby a little while back because I wanted to make a little game. I learned the basics of java, and I decided that I would try to tackle game-dev. My JFrame and all is fine and I technically have no bugs, but my little rectangle guy won't move around the screen. Here's my code:

package main;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Race extends JFrame {

public int speed=5;
public int up=1;
public int left=2;
public int down=3;
public int right=4;
public int direction=0;

Rectangle p1 = new Rectangle (500,400,20,40);

public Race()
{
    super("Race");
    setSize(1000,800);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cont=getContentPane();
    cont.setBackground(Color.orange);
    setResizable(false);
}

public void paint(Graphics g)
{
    super.paint(g);
    g.setColor(Color.red);
    g.fillRect(p1.x, p1.y, p1.width, p1.height);
}


public class Move extends Thread implements KeyListener
{
    public void run()
    {
        addKeyListener(this);
        
        while(true)
        {
            try
            {
                repaint();
                
                if(direction==up)
                {
                    p1.y-=(int)speed;
                }
                if(direction==down)
                {
                    p1.y+=(int)speed;
                }
                if(direction==right)
                {
                    p1.x+=(int)speed;
                }
                if(direction==left)
                {
                    p1.x-=(int)speed;
                }   
                    Thread.sleep(75);
            }
                catch (Exception e)
                {
                    break;
                }
            }
        }

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

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

    @Override
    public void keyTyped(KeyEvent event) {
        // TODO Auto-generated method stub
        if(event.getKeyChar()=='w')
        {
            direction = up;
        }
        if(event.getKeyChar()=='d')
        {
            direction = left;
        }
        if(event.getKeyChar()=='s')
        {
        direction = down;   
        }
        if(event.getKeyChar()=='a')
        {
            direction = left;
        }
    }
    
    }
    




public static void main(String[] args)
{
    new Race();
}

}
Zoe
  • 27,060
  • 21
  • 118
  • 148
user2642853
  • 1
  • 1
  • 1
  • 1) put `setVisible(true);` at end of constructor, 2) `repaing()` is marely suggeston so it will not guarantee repainting, 3) where do you use/start `Move` thread? – Pshemo Aug 22 '13 at 15:48
  • First off it's a really good idea to start getting in the habit of commenting your code. Every couple lines as necessary should have some clarification of what you're doing/want to do. Makes easier for others to understand. Is your program calling Paint again after adjusting the x and y coordinates based on the input? If not that would likely be the problem. – ObjectNameDisplay Aug 22 '13 at 15:52

2 Answers2

1

There are a couple of problems:

  1. You aren't creating a Move instance so nothing is listening to keys.
  2. I don't think you even need a Move class that runs in a background thread. Take the KeyListener logic and put it in your Race class, then "enable" the keyListener by calling addKeyListener(this); so it starts listening to key presses.

    Race extends JFrame implements KeyListener{
    
        Race(){
            ...
            addKeyListener(this);
        }
    
    @Override
    public void keyTyped(KeyEvent event) {
    
    if(event.getKeyChar()=='w')
       {
            direction = up;
        }
        if(event.getKeyChar()=='d')
        {
            direction = left;
        }
        if(event.getKeyChar()=='s')
        {
        direction = down;   
        }
        if(event.getKeyChar()=='a')
        {
            direction = left;
        }
        repaint();
    }
    

Also see this SO question : Unresponsive KeyListener for JFrame for more help.

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • I know that my KeyListener is working because I made it print to the console. I think that the problem is in the Move class, but I can't see what it is, so more suggestions would help! – user2642853 Aug 22 '13 at 18:33
0

If you call the Move class like this in the Race class:

public Move move;

and then call it under you Race constructor:

move = new Move();

in you Move class you make a constructor like this:

public Move()
    {
        run();
    }

This should make it move :)

And for heads up, you need to change the keypress d to go to the right instead of left.

NickyBoy
  • 5
  • 3