Java:Streets Project
From GPWiki(Redirected from Java:Java)
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] StreetsI started this project and presented for the final exam in Computer Science. It has three methods of driving for the AI, but nothing yet programmed for the user [edit] GUI
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
public class GUI extends JFrame
{
//array of Car
boolean init=true;
Car cars[]=new Car[3];
Street street=new Street(20,20,13,13);
public GUI()
{
init=true;
byte[][] temp= {{0,0,0,0,0,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,0,0,0,1,1,1,0},
{0,1,1,1,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,1,1},
{0,1,0,0,0,0,0,0,0,0,0,0,1},
{0,1,0,0,0,0,0,0,0,0,0,0,1},
{0,1,0,0,0,0,0,0,0,0,0,0,1},
{0,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0}};
/*{{1,1,1,1,1,1,0},
{1,0,0,0,0,1,0},
{1,0,0,0,0,1,1},
{1,1,0,0,0,0,1},
{1,1,0,0,0,0,1},
{0,1,0,0,0,1,1},
{0,1,1,1,1,1,0}};*/
// street.setStreetDefault();
street.setStreetUser(temp);
cars[0]=new Car(120,70,0,2,0,2,10);
cars[1]=new Car(120,80,0,0,0,4,0.2);
cars[2]=new Car(120,90,0,0,0,4,0.2);
setVisible(true);
setSize((street.width()*street.roadWidth())+street.xpos()*2,(street.height()*street.roadWidth())+street.ypos()*2+25);
repaint();
addWindowListener( //ends program when user closes JFrame
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
public void paint(Graphics g)
{
g.translate(0,25);
if(init)
{
g.setColor(Color.lightGray);
g.fillRect(0,0,getHeight(),getWidth());
init=false;
}
//draws street
//g.setColor(Color.white);
//g.fillRect(0,0,(street.width()*street.roadWidth())+street.xpos()*2,(street.height()*street.roadWidth())+street.ypos()*2);
street.drawStreet(g);
//draws cars
for(int i=0;i<cars.length;i++)
{
g.setColor(Color.lightGray);
cars[i].drawCars(g);
}
driveMethod1(0);
driveMethod2(1);
driveMethod3(2,g);
g.setColor(Color.red);
cars[0].drawCars(g);
g.setColor(Color.blue);
cars[1].drawCars(g);
g.setColor(Color.green);
cars[2].drawCars(g);
/*for(int i=0;i<cars.length;i++)
{
g.setColor(Color.red);
cars[i].drawCars(g);
}*/
}
public void grid(int xspace,int yspace, Graphics g)
{
//clears screen
g.setColor(Color.black);
for(int i=0;(i*xspace)<getWidth();i++)
{
for(int j=0;(j*yspace)<getHeight();j++)
{
g.drawLine(0,(j*yspace),getWidth(),(j*yspace));
}
g.drawLine((i*xspace),0,(i*xspace),getHeight());
}
}
public void driveMethod1(int index)//implements a driver who looks ahead
{
int checkrange=40;
if(!street.inBounds(cars[index].getx(),cars[index].gety()))
{cars[index].addScore();}
if({!street.inBounds((int)(cars[index].getx()+checkrange*(Math.cos((Math.PI/180)*
cars[index].getDirection()))),(int)(cars[index].gety()-
(checkrange*Math.sin((Math.PI/180)*cars[index].getDirection()))))})
{
//System.out.println("Level 1 accessed"); //if check
int j=0,k=0;
do{
for(j=0;j<=180;j++)
{
for(k=-1;k<=1;k+=2)//360 checks
{
if({street.inBounds((int)(cars[index].getx()+checkrange*
(Math.cos((Math.PI/180)*(cars[index].getDirection()+(k*j))))),
(int)(cars[index].gety()-(checkrange*(Math.sin((Math.PI/180)*
(cars[index].getDirection()+(k*j)))))))})
{
//System.out.println("Level 2 accessed"); //if check
cars[index].setDirectionRelative(k);
j=185;k=5;
}
}
}
checkrange+=30;
}while(k<4||j<182);
}
else
{
}
cars[index].move();
}
public void driveMethod2(int index)//implements acceleration
{
int checkrange=90;
if(!street.inBounds(cars[index].getx(),cars[index].gety()))
{cars[index].addScore();}
if({!street.inBounds((int)(cars[index].getx()+checkrange*
(Math.cos((Math.PI/180)*cars[index].getDirection()))),
(int)(cars[index].gety()-(checkrange*Math.sin((Math.PI/180)
*cars[index].getDirection()))))})
{
int j=0,k=0;
do{
if(!(cars[index].getSpeed()<=0.5))
{cars[index].setAccel(-0.03);}
else
{cars[index].setAccel(0.02);}
for(j=0;j<=180;j++)
{
for(k=-1;k<=1;k+=2)
{
if({street.inBounds((int)(cars[index].getx()+checkrange*
(Math.cos((Math.PI/180)*(cars[index].getDirection()+(k*j)))))
,(int)(cars[index].gety()-(checkrange*
(Math.sin((Math.PI/180)*(cars[index].getDirection()+
(k*j)))))))})
{
//System.out.println("Level 2 accessed"); //if check
cars[index].setDirectionRelative(k*2);
if(j<=30)
{
cars[index].setAccel(0.02);
}
//cars[index].setAccel(0.1);
j=185;k=5;
}
}
}
checkrange+=30;
}while(k<4||j<182);
}
else
{
cars[index].setAccel(0.01);
}
cars[index].move();
}
public void driveMethod3(int index,Graphics g)//implements acceleration and tries to eliminate wall riding
{
int checkrange=90;
boolean wall=false;
if(!street.inBounds(cars[index].getx(),cars[index].gety()))
{cars[index].addScore();}
// g.setColor(Color.red); //uncomment for cool debug
// g.fillRect((int)(cars[index].getx()+checkrange*
// (Math.cos((Math.PI/180)*cars[index].getDirection()))),
// (int)(cars[index].gety()-(checkrange*Math.sin((Math.PI/180)*
// cars[index].getDirection()))),1,1);
if({!street.inBounds((int)(cars[index].getx()+checkrange*
(Math.cos((Math.PI/180)*cars[index].getDirection()))),
(int)(cars[index].gety()-(checkrange*Math.sin((Math.PI/180)*
cars[index].getDirection()))))})
{
int j=0,k=0;
do{
if(!(cars[index].getSpeed()<=0.5))
{cars[index].setAccel(-0.03);}
else
{cars[index].setAccel(0.03);}
for(j=0;j<=180;j++)
{
for(k=-1;k<=1;k+=2)
{
if({street.inBounds((int)(cars[index].getx()+checkrange*
(Math.cos((Math.PI/180)*(cars[index].getDirection()+
(k*j))))),(int)(cars[index].gety()-(checkrange*
(Math.sin((Math.PI/180)*(cars[index].getDirection()+
(k*j)))))))})
{
//System.out.println("Level 2 accessed"); //if check
cars[index].setDirectionRelative(k*2);
if(j<=30)
{
cars[index].setAccel(0.02);
}
//cars[index].setAccel(0.1);
j=185;k=5;
}
}
}
checkrange+=30;
}while(k<4||j<182);
}
else
{
for(int j=45;j<95;j++)
{
for(int k=-1;k<=1;k+=2)
{
// g.setColor(Color.blue); //uncomment for cool debug
// g.fillRect((int)(cars[index].getx()+(((double)
// street.roadWidth())/1.8)*(Math.cos((Math.PI/180)*
// (cars[index].getDirection()+(k*j))))),
// (int)((cars[index].gety()-(((double)
// street.roadWidth())/2)*(Math.sin((Math.PI/180)*
// (cars[index].getDirection()+(k*j)))))),1,1);
if({!street.inBounds((int)(cars[index].getx()+(((double)
street.roadWidth())/1.8)*(Math.cos((Math.PI/180)*
(cars[index].getDirection()+(k*j))))),
(int)((cars[index].gety()-(((double)street.roadWidth())/2)*
(Math.sin((Math.PI/180)*(cars[index].getDirection()+
(k*j)))))))})
{
cars[index].setDirectionRelative((((double) k)*(j-90))/30);//(5*(Math.cos((Math.PI/180)*((j*k)+90))))
//System.out.println("Here i am");
j=136;k=5;
}
}
}
cars[index].setAccel(0.01);
}
cars[index].move();
}
/* Method 0
if(!street.inBounds(cars[i].getx(),cars[i].gety()))
{
//System.out.println("Car "+i+" is out of bounds.");
cars[i].setDirectionRelative(-1);
}
else
{
cars[i].setDirectionRelative(0.5);
//System.out.println("Car "+i+" is in bounds.");
}*/
public static void main(String args[])
{
int step=0;
boolean playing=true;
GUI gui=new GUI();
while(playing)
{
if(step%300000==0)
{
gui.repaint();
}
step++;
}
}
}
[edit] Street Class
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Street
{
private byte[][] street;
private static int width,height;
private static int roadWidth;
private static int xpos,ypos;
public Street(int x, int y, int xwidth,int yheight)
{
xpos=x;
ypos=y;
width=xwidth;
height=yheight;
roadWidth=50;
street=new byte[width][height];
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
street[i][j]=0;
}
}
}
public int roadWidth()
{
return roadWidth;
}
public int xpos()
{
return xpos;
}
public int ypos()
{
return ypos;
}
public int width()
{
return width;
}
public int height()
{
return height;
}
//checks position at pixel x,y
public boolean inBounds(int x,int y)
{
return {((x<roadWidth*width+xpos&&x>xpos)
&&(y<roadWidth*height+ypos&&y>ypos))
&&((x-xpos)/roadWidth)<roadWidth-1
&&((x-xpos)/roadWidth)>=0
&&((y-ypos)/roadWidth)<roadWidth-1
&&(((y-roadWidth)/roadWidth)>=0)
&&(street[(x-xpos)/roadWidth][(y-ypos)/roadWidth]==1)?true:false;}
}
/*
boolean temp=false;
if((x<roadWidth*width+xpos&&x>xpos)&&(y<roadWidth*height+ypos&&y>ypos))
{
if(((x-xpos)/roadWidth)<roadWidth-1&&((x-xpos)/roadWidth)>=0)
{
if(((y-ypos)/roadWidth)<roadWidth-1&&((y-roadWidth)/roadWidth)>=0)
{
if(street[(x-xpos)/roadWidth][(y-ypos)/roadWidth]==1)
{
temp=true;
}
}
}
}
return temp;*/
public void setStreetUser(byte[][] temp)
{
if(width==temp.length&&height==temp[0].length)
{
street=temp;
}
else
{
System.out.println("Internal Error: setStreetUser() array not of approprite size!");
}
}
public void setStreetDefault()
{
for(int i=0;i<width;i++)
{
street[i][height/2]=1;
}
for(int i=0;i<height;i++)
{
street[width/2][i]=1;
}
}
public void setStreetDefault2()
{
if(width==5&&height==5)
{
byte[][] temp={{1,1,0,0,0},
{0,1,1,1,1},
{0,1,0,1,0},
{0,1,1,1,0},
{0,1,0,0,0}};
street=temp;
}
else
{
System.out.println("Internal Error: setStreetDefault2() must be used with a 5by5 street!");
}
}
public void drawStreet(Graphics g)
{
g.setColor(Color.black);
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
if(street[i][j]==1)
{
//checks the space to the left
if((i-1)>=0)
{
if(street[i-1][j]!=1)
g.drawLine(i*(roadWidth)+xpos,j*(roadWidth)+ypos,i*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
else
{
g.drawLine(i*(roadWidth)+xpos,j*(roadWidth)+ypos,i*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
//checks the space to the right
if((i+1)<width)
{
if(street[i+1][j]!=1)
g.drawLine((i+1)*(roadWidth)+xpos,j*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
else
{
g.drawLine((i+1)*(roadWidth)+xpos,j*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
//checks the space above
if((j-1)>=0)
{
if(street[i][j-1]!=1)
g.drawLine(i*(roadWidth)+xpos,j*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,j*(roadWidth)+ypos);
}
else
{
g.drawLine(i*(roadWidth)+xpos,j*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,j*(roadWidth)+ypos);
}
//checks the space below
if((j+1)<height)
{
if(street[i][j+1]!=1)
g.drawLine(i*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
else //
{
g.drawLine(i*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos,(i+1)*(roadWidth)+xpos,(j+1)*(roadWidth)+ypos);
}
}
}
}
}
}
[edit] Car Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Car
{
private double direction; //direction 0 to 360 degrees
private double speed; //speed of car( a speed of 2 will move the car an average of one pixel per step)
private double x,y; //x,y position of car
private double acceleration; //current acceleration of car
private double maxAccel;
private double maxSpeed;
int score;
//declaration of car with initial x and y position, dir ,speed, and acceleration
public Car(double xpos,double ypos,double dir,double sped,double accel,double msped,double maccel)
{
score=0;
x=xpos;
y=ypos;
maxSpeed=msped;
maxAccel=maccel;
direction=dir;
if(!(Math.abs(accel)>maxAccel))
{
acceleration=accel;
}
else
{
acceleration=maxAccel*(accel/Math.abs(accel));
}
if((sped<=maxSpeed)&&(sped>=0))
{speed=sped;}
else
{speed=0;}
}
/*accelerates the car and moves the car 'speed' amount in the
//direction 'direction' relative to the 'x,y' position.
//Limits speed to max speed*/
public void addScore()
{
score++;
}
public int getScore()
{
return score;
}
public void move()
{
if(speed<maxSpeed||acceleration<0)
speed+=acceleration;
if(speed<0)
speed=0;
else if(speed>maxSpeed)
speed=maxSpeed;
x+=Math.cos((Math.PI/180)*direction)*(speed/2);
y-=Math.sin((Math.PI/180)*direction)*(speed/2);
}
//returns x or y values
public int getx()
{
return (int)x;
}
public int gety()
{
return (int)y;
}
public double getSpeed()
{
return speed;
}
//returns direction
public double getDirection()
{
return direction;
}
//set direction to a new value
public void setDirection(double dir)
{
direction=dir;
}
// adds or subtracts from current direction also returns new direction
public double setDirectionRelative(double dir)
{
direction+=dir;
return direction;
}
public double getAccel()
{
return acceleration;
}
public double getMaxAccel()
{
return maxAccel;
}
//sets Acceleration to accel if within bounds else set to max and return false
public boolean setAccel(double accel)
{
boolean temp=true;
if(!(Math.abs(accel)>maxAccel))
{
acceleration=accel;
}
else
{
acceleration=maxAccel*(accel/Math.abs(accel));
temp=false;
}
return temp;
}
//draws cars with x,y in center and 20pixels on each side
public void drawCars(Graphics g)
{
g.fillRect((int)x-5,(int)y-5,10,10);
}
}
|


