Click to See Complete Forum and Search --> : Keyboard input and keyDown


Paddy Notemaker
01-08-2005, 07:16 PM
Dear all,

I have recently come to terms with java animations with a view to making (at this stage) basic 2D games. I've found out how to use the keyDown method in applets and have started to use it in my code. The compiler has no problems with the code, but when I test the code to see if the controls work, the controls simply will not work. This is an example of code that the compiler has allowed. The controls do not work (they should be up, down, left and right, and they should move the black box in the expected directions).

Code:

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

public class main extends Applet implements Runnable
{

public int machineblackx = 50;
public int myblackx = machineblackx - 25;
public int machineblacky = 50;
public int myblacky = machineblacky - 25;
public int machinespeed = 50;
public int displayspeed = (200 - machinespeed) ;


boolean isStoped = true;

Thread th;

Font f = new Font ("Serif", Font.BOLD, 20);

public void init ()
{
setBackground (Color.white);
}

public void start()
{

th = new Thread (this);
th.start ();
}


public void stop ()
{
th.stop();
}


public boolean keyDown (Event e, int key)
{




if (key == Event.LEFT)
{

machineblackx--;


update(getGraphics());
}

if(key == Event.RIGHT)
{

machineblackx++;
update(getGraphics());


}





return true;
}
public void run ()
{

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while (true)
{
if(machineblackx > 150)
{
machineblackx = 0;
}
if(machineblacky > 150)
{
machineblacky = 0;
}
if(machineblackx < 0)
{
machineblackx = 150;
}
if(machineblacky < 0)
{
machineblacky = 150;
}

repaint();

try
{

Thread.sleep (machinespeed);
}
catch (InterruptedException ex)
{
// do nothing
}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}


public void paint (Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, 200, 200);
g.setColor(Color.black);
g.fillRect(myblackx, myblacky, 50, 50);

}

public void update (Graphics g)
{

paint(g);
}
}

------------------------------------------------------------------
End Code

Any help would be appreciated. Thankyou for your time and a happy New Year to everyone.

ray326
01-09-2005, 01:46 AM
public class Animator extends Applet implements Runnable
...
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, 200, 200);
g.setColor(Color.black);
g.fillRect(machineblackx, machineblacky, 50, 50);
}

Paddy Notemaker
01-10-2005, 07:07 PM
Dear all,

Thanks for your advice. I tried that, but the keyboard input still does not work at all. You press the buttons and nothing happens. I don't think the computer is having problems with the calculations. In the version I included in my last post, I did not have the up and down controls. In my actual version, these are present, so please do not put the problem down to this. I also do not understand why the compiler is processing it when it does not work. Any answers would be great. Thanks, Paddy.

ray326
01-10-2005, 10:28 PM
Your code works fine with the changes I posted.

Paddy Notemaker
01-11-2005, 07:06 PM
Dear Ray,

I haven't tried the code yet, but I would like to thank you in advance for your help.