Everything was fine until i added in the Move classes.No idea what to do, any help would be appreciated
package g1p1;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class G1P1 extends JFrame {
//screen size
final int WIDTH = 900, HEIGHT = 650;
//keep track of speed
double p1Speed = 0.5, p2Speed = 0.5;
//direction var
final int UP=0, DOWN = 1, RIGHT=2, LEFT=3;
//PLAYERS DIRECTION
int p1Direction = UP;
int p2Direction = UP;
//gives perameters of objects
//border
Rectangle left = new Rectangle(0,0,50,HEIGHT);
Rectangle right = new Rectangle(850,0,50,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH, HEIGHT/9);
Rectangle bottom = new Rectangle(0,600,WIDTH,100);
Rectangle center = new Rectangle(200,(int)((HEIGHT/9)*2.5),575,(HEIGHT/9)*4);
//OBSTACLES
Rectangle obstacle = new
Rectangle(250,0,100,140);
Rectangle obstacle2 = new
Rectangle(300,400,WIDTH/10,150);
Rectangle obstacle3 = new
Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle4 = new
Rectangle(450,525,100,100);
Rectangle obstacle5 = new
Rectangle(WIDTH/2,125,WIDTH/30,HEIGHT/4);
//FINISH LINE
Rectangle finish = new Rectangle(50,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5), HEIGHT/70);
//p1 car
Rectangle p1 = new Rectangle(70,330,WIDTH/30,WIDTH/30);
//p2 car
Rectangle p2 = new Rectangle(150,395,WIDTH/30,WIDTH/30);
//STARTING LINE FOR OUTER CAR
Rectangle lineO = new
Rectangle(50,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
//STARTING LINE FOR INNER PLAYER
Rectangle lineI = new Rectangle(125,(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2, HEIGHT/140);
//THE CONSTRUCTOR - makes the screen
public G1P1()
{
//creates JFrame
super("radical racing");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
/* Move1 m1 = new Move1();
Move2 m2 = new Move2();
m1.start();
m2.start();*/
}
//THIS WILL DRAW objects of the game
public void paint(Graphics g)
{
super.paint(g);
//draw the background
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,WIDTH,HEIGHT);
//make the border green
g.setColor(Color.GREEN);
//USING THE RECTANGLES TO DRAW IT
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.setColor(Color.GREEN);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//set starting line to white
g.setColor(Color.WHITE);
//DRAW THE STARTING LINE
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
//SET COLOR OF FINISH LINE TO YELLOW
g.setColor(Color.YELLOW);
//DRAW FINISH LINE
g.fillRect(finish.x,finish.y,finish.width,finish.height);
//SET COLOR OF P1 TO BLUE
g.setColor(Color.blue);
g.fill3DRect(p1.x, p1.y, p1.width, p1.height,true);
//SET COLOR OF P2 TO red
g.setColor(Color.red);
g.fill3DRect(p2.x, p2.y, p2.width, p2.height,true);
}
//add speed + movement
private class Move1 extends Thread implements KeyListener
{
@Override
public void run()
{
//wake up key listener
addKeyListener(this);
while(true)
{
try
{
//refresh screen
repaint();
//check for collision on walls, if so, make it slower
if(p1.intersects(left)||p1.intersects(right)||
p1.intersects(top) ||p1.intersects(bottom)||
p1.intersects(obstacle)||p1.intersects(obstacle2)
||p1.intersects(obstacle3)||p1.intersects(obstacle4)
||p1.intersects(obstacle5)|| p1.intersects(p2))
{
p1Speed =-4;
}
//if it hits the center do the same
if(p1.intersects(center))
{
p1Speed =-2.5;
}
//increase speed
if(p1Speed<=5)
{
p1Speed+=0.2;
}
//move player based on direction
if(p1Direction==UP)
{
p1.y-=(int)p1Speed;
}
if(p1Direction==DOWN)
{
p1.y+=(int)p1Speed;
}
if(p1Direction==LEFT)
{
p1.x-=(int)p1Speed;
}
if(p1Direction==LEFT)
{
p1.x+=(int)p1Speed;
}
//delay refresh
Thread.sleep(75);
}
catch(Exception e)
{
break;
}
}
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyPressed(KeyEvent event)
{
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyReleased(KeyEvent event)
{
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar()=='a')
{
p1Direction = LEFT;
}
if(event.getKeyChar()=='d')
{
p1Direction = RIGHT;
}
if(event.getKeyChar()=='s')
{
p1Direction = DOWN;
}
if(event.getKeyChar()=='w')
{
p1Direction = UP;
}
}
}
//add speed + movement
private class Move2 extends Thread implements KeyListener
{
@Override
public void run()
{
//wake up key listener
addKeyListener(this);
while(true)
{
try
{
//refresh screen
repaint();
//check for collision on walls, if so, make it slower
if(p2.intersects(left)||p2.intersects(right)||
p2.intersects(top) ||p2.intersects(bottom)||
p2.intersects(obstacle)||p2.intersects(obstacle2)
||p2.intersects(obstacle3)||p2.intersects(obstacle4)
||p2.intersects(obstacle5)|| p2.intersects(p1))
{
p2Speed =-4;
}
//if it hits the center do the same
if(p2.intersects(center))
{
p2Speed =-2.5;
}
//increase speed
if(p2Speed<=5)
{
p2Speed+=0.2;
}
//move player based on direction
if(p2Direction==UP)
{
p2.y-=(int)p1Speed;
}
if(p2Direction==DOWN)
{
p2.y+=(int)p1Speed;
}
if(p1Direction==LEFT)
{
p1.x-=(int)p1Speed;
}
if(p2Direction==LEFT)
{
p2.x+=(int)p1Speed;
}
//delay refresh
Thread.sleep(75);
}
catch(Exception e)
{
break;
}
}
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyPressed(KeyEvent event)
{
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyReleased(KeyEvent event)
{
}
//IMPLEMENT METHOD FROM KEY LISTENER
@Override
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar()=='j')
{
p2Direction = LEFT;
}
if(event.getKeyChar()=='l')
{
p2Direction = RIGHT;
}
if(event.getKeyChar()=='k')
{
p2Direction = DOWN;
}
if(event.getKeyChar()=='i')
{
p2Direction = UP;
}
}
}
public static void main(String[]args)
{
new G1P1();
}
}