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.
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.