Donny
Feb 10th, 2001, 04:19 PM
Why doesn't The following code do what i want it to do And i want to be able to control the rectangle around the applet screen With the arrow keys So like if you press the up arrow The rectangle will Move up And if it hits the Edge of the applet it rebounds a bit and then you start contolling again same with the other arrows.
code:
import java.awt.* ;
import java.applet.Applet ;
public class Game extends Applet implements Runnable
{
private int maxWidth , maxHeight ;
int temp_X[]= new int[400];
int temp_Y[]= new int[400];
int X,Y;
boolean left=false;
boolean right=false;
boolean up=false;
boolean down=false;
private int imageHeight , imageWidth ;
public void init()
{
setBackground( Color.yellow );
maxWidth = 200 ;
maxHeight = 200 ;
imageHeight = 25 ;
imageWidth = 40 ;
X = (int)( ( Math.random() * 80 ) + 1 );
Y = (int)( ( Math.random() * 80 ) + 10 );
Thread animator = new Thread( this );
animator.start();
}
public void paint( Graphics g )
{
g.drawRect( X , Y , imageWidth , imageHeight );
}
public void run()
{
while ( true )
{
try
{
Thread.sleep( 50 );
}
catch ( InterruptedException e )
{
}
if(left){
temp_X[0]-=10;
}
if(right){
temp_X[0]+=10;
}
if(up){
temp_Y[0]-=10;
}
if(down){
temp_Y[0]+=10;
}
if ( ( X + imageWidth ) >= maxWidth )
{
X = -1;
}
else if ( X <= 0 )
{
X = 1;
}
if ( ( Y + imageHeight ) >= maxHeight )
{
Y = -1;
}
else if ( Y <= 0 )
{
Y = 1;
}
repaint();
}
}
public boolean keyDown(Event e, int key)
{
if ((key == Event.LEFT) &&(!right)){
left = true; up = false; down = false;
}
if ((key == Event.RIGHT) && (!left)){
right = true; up = false; down = false;
}
if ((key == Event.UP) && (!down)){
up = true; right = false; left = false;
}
if ((key == Event.DOWN) && (!up)){
down = true; right = false; left = false;
}
return true;
}
}
end code:
Please help........
code:
import java.awt.* ;
import java.applet.Applet ;
public class Game extends Applet implements Runnable
{
private int maxWidth , maxHeight ;
int temp_X[]= new int[400];
int temp_Y[]= new int[400];
int X,Y;
boolean left=false;
boolean right=false;
boolean up=false;
boolean down=false;
private int imageHeight , imageWidth ;
public void init()
{
setBackground( Color.yellow );
maxWidth = 200 ;
maxHeight = 200 ;
imageHeight = 25 ;
imageWidth = 40 ;
X = (int)( ( Math.random() * 80 ) + 1 );
Y = (int)( ( Math.random() * 80 ) + 10 );
Thread animator = new Thread( this );
animator.start();
}
public void paint( Graphics g )
{
g.drawRect( X , Y , imageWidth , imageHeight );
}
public void run()
{
while ( true )
{
try
{
Thread.sleep( 50 );
}
catch ( InterruptedException e )
{
}
if(left){
temp_X[0]-=10;
}
if(right){
temp_X[0]+=10;
}
if(up){
temp_Y[0]-=10;
}
if(down){
temp_Y[0]+=10;
}
if ( ( X + imageWidth ) >= maxWidth )
{
X = -1;
}
else if ( X <= 0 )
{
X = 1;
}
if ( ( Y + imageHeight ) >= maxHeight )
{
Y = -1;
}
else if ( Y <= 0 )
{
Y = 1;
}
repaint();
}
}
public boolean keyDown(Event e, int key)
{
if ((key == Event.LEFT) &&(!right)){
left = true; up = false; down = false;
}
if ((key == Event.RIGHT) && (!left)){
right = true; up = false; down = false;
}
if ((key == Event.UP) && (!down)){
up = true; right = false; left = false;
}
if ((key == Event.DOWN) && (!up)){
down = true; right = false; left = false;
}
return true;
}
}
end code:
Please help........