Hi. I'm trying to create a Traffic simulator and I'm stuck on how to get a Car object to turn into a road. I've managed to have a car go straight (which is really easy anyway lol) but I can't really find a way to get it to turn into a road, specifically a left turn.
The code I have currently is this:
import java.applet.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
public class Traffic3 extends Applet implements Runnable
{
//thread variables
Thread t;
int i;
//double buffer variables
Image offscreenImage;
Graphics offscr;
int width;
int height;
//car
//coordinates for lanes
//south road, left lane
int sLeftX = 232;
int sLeftY = 570;
Car car = new Car(sLeftX,sLeftY);
public void init()
{
this.setSize(600,600);
//thread stuff
t = new Thread(this);
t.start();
i = 0;
//double buffer stuff
width = size().width;
height = size().height;
offscreenImage = createImage(width, height);
offscr = offscreenImage.getGraphics();
}
public void run()
{
while(true)
{
this.requestFocus();
i++;
//straight
//sLeftY-=5;
//car.changePosition(sLeftY);
//if (sLeftY == -63)
//sLeftY=557;
//left
sLeftY-=5;
car.changePosition(sLeftY);
if (sLeftY == 365)
{
car.rotatePosition();
sLeftY=570;
//for (i=0;i<10;i++)
//{
// car.iX[1] += 1;
// car.iY[1] -= 1;
// car.iX[2] += 1;
// car.iY[2] -= 1;
// car.iX[3] -= 1;
// car.iY[3] -= 1;
//}
}
repaint();
try {
t.sleep(1000/30);
} catch (InterruptedException e) { ; }
}
}
public void paint(Graphics g)
{
//buffer stuff
offscr.setColor(Color.black);
offscr.fillRect(0, 0, width, height);
offscr.setColor(Color.blue);
offscr.drawString("i = "+i,10,20);
roads(offscr);
car.paint(offscr);
g.drawImage(offscreenImage, 0, 0, this);
}
//update for buffer
public void update(Graphics g)
{
paint(g);
}
void roads(Graphics g)
{
//Roads
//west
g.setColor(Color.gray);
g.fillRect(0, 220, 220, 150);
g.setColor(Color.black);
//north
g.setColor(Color.gray);
g.fillRect(220, 0, 150, 220);
g.setColor(Color.black);
//east
g.setColor(Color.gray);
g.fillRect(370, 220, 220, 150);
g.setColor(Color.black);
//south
g.setColor(Color.gray);
g.fillRect(220, 370, 150, 220);
g.setColor(Color.black);
//middle square
g.setColor(Color.gray);
g.fillRect(220, 220, 150, 150);
//north and south dividers
g.setColor(Color.white);
for(int j=0;j<30;j++)
{
if (j!=11 && j!=12 && j!=13 && j!=14 && j!=15 && j!=16 && j!=17 && j!=18)
{
g.drawLine(270,j*20,270,j*20+10);
g.drawLine(320,j*20,320,j*20+10);
}
}
//east and west dividers
for(int i=0;i<30;i++)
{
if (i!=11 && i!=12 && i!=13 && i!=14 && i!=15 && i!=16 && i!=17 && i!=18)
{
g.drawLine(i*20,270,i*20+10,270);
g.drawLine(i*20,320,i*20+10,320);
}
}
}
public class Car {
private float sfCarLength;
private Color cColor;
private int posX;
private int posY;
private float sfX[] = new float[4];
private float sfY[] = new float[4];
private int iX[] = new int[4]; // Integer arrays for constructing the polygon for painting
private int iY[] = new int[4];
private float sfPaintScaleFactor;
Polygon pgon2;
public Car(int x, int y)
{
cColor = Color.blue;
posX = x;
posY = y;
iX[0] = 0; iY[0] = 0;
iX[1] = 0; iY[1] = 25;
iX[2] = 20; iY[2] = 25;
iX[3] = 20; iY[3] = 0;
}
public void paint(Graphics g)
{
Polygon pgon;
pgon = new Polygon(iX, iY, 4);
pgon.translate(posX, posY);
g.setColor(cColor);
g.fillPolygon(pgon);
g.setColor(Color.black);
g.drawPolygon(pgon);
pgon2 = new Polygon(iX, iY, 4);
pgon2.translate(232, 365);
g.setColor(cColor);
g.fillPolygon(pgon2);
g.setColor(Color.black);
g.drawPolygon(pgon2);
}
public void changePosition(int y)
{
this.posY = y;
repaint();
}
public void rotatePosition()
{
AffineTransform tx = new AffineTransform();
tx.rotate(30.0 * Math.PI / 180.0);
Shape newShape = tx.createTransformedShape(pgon2);
//pgon2.rotate(30.0 * Math.PI / 180.0);
repaint();
}
}
}
The rotatePosition() method is just something i tried doing but failed miserably. Any ideas on the best approach to take?
Thanks.