Hi all,
I am a beginner in java.I have written a code to design a layout of a traffic signal.The lights are working fine.The problem i am facing is to randomly generate cars from all directions and to stop them at the junction.I am not understanding how to use repaint.Here is the code of the layout.Please ignore the fuzzy cycle tab.Can anyone help me out to generate the cars?
import java.awt.*;
import javax.swing.*;
public class Simulator extends JApplet{
private static final long serialVersionUID = 1L;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
makeGUI();
}
});
}catch (Exception e) {
System.out.println("Cant Create bcoz of " +e);
}
}
public void makeGUI(){
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Fixed Cycle", new FixedCycle());
jtp.addTab("Fuzzy Cycle", new Layout());
add(jtp);
setSize(560, 580);
}
}
//Fixed Cycle Class
class FixedCycle extends JPanel implements Runnable{
int flag,i=0;
int [] seq = {1,3,2,3};
int x=0,y=230;
private static final long serialVersionUID = 1L;
Thread t1,t2;
//Constructor
FixedCycle(){
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
}
//Override_paint_compnent
protected void paintComponent(Graphics g) {
//Always we need to call the superclass first
super.paintComponent(g);
t1 = new Thread(this);
//Draw the roads
roads(g);
//Draw the Border
g.drawRoundRect(10, 10, 530, 530, 15, 15);
//Traffic Signals
signal(g,305,270,1);//Right
signal(g,220,300,0);//Down
signal(g,220,220,1); //Left
signal(g,270,220,0); //Up
//Color of the lights
if(flag == 1) //North-South
light1(g);
if(flag == 2) //East-West
light2(g);
if(flag == 3)
orange(g);
play(g);
}
void roads(Graphics g)
{
//Roads
g.drawLine(20, 220, 220, 220);
g.drawLine(220, 220, 220, 20);
g.drawLine(320, 20, 320, 220);
g.drawLine(320, 220, 520, 220);
g.drawLine(20, 320, 220, 320);
g.drawLine(220, 320, 220, 520);
g.drawLine(320, 520, 320, 320);
g.drawLine(320, 320, 520, 320);
//Devider1
g.drawLine(270, 20, 270, 40);
g.drawLine(270, 50, 270, 70);
g.drawLine(270, 80, 270, 100);
g.drawLine(270, 110, 270, 130);
g.drawLine(270, 140, 270, 160);
g.drawLine(270, 170, 270, 190);
g.drawLine(270, 200, 270, 220);
//Devider2
g.drawLine(270, 320, 270, 340);
g.drawLine(270, 350, 270, 370);
g.drawLine(270, 380, 270, 400);
g.drawLine(270, 410, 270, 430);
g.drawLine(270, 440, 270, 460);
g.drawLine(270, 470, 270, 490);
g.drawLine(270, 500, 270, 520);
//Devider3
g.drawLine(20, 270, 40, 270);
g.drawLine(50, 270, 70, 270);
g.drawLine(80, 270, 100, 270);
g.drawLine(110, 270, 130, 270);
g.drawLine(140, 270, 160, 270);
g.drawLine(170, 270, 190, 270);
g.drawLine(200, 270, 220, 270);
//Devider4
g.drawLine(320, 270, 340, 270);
g.drawLine(350, 270, 370, 270);
g.drawLine(380, 270, 400, 270);
g.drawLine(410, 270, 430, 270);
g.drawLine(440, 270, 460, 270);
g.drawLine(470, 270, 490, 270);
g.drawLine(500, 270, 520, 270);
}
void signal(Graphics g, int x, int y, int direction){
if(direction == 1)
{
g.drawOval(x+4, y+6, 7, 7);
g.drawOval(x+4, y+21, 7, 7);
g.drawOval(x+4, y+36, 7, 7);
}
else
{
g.drawOval(x+6, y+4, 7, 7);
g.drawOval(x+21, y+4, 7, 7);
g.drawOval(x+36, y+4, 7, 7);
}
}
public void play(Graphics g)
{
if(i>3)
i=0;
if(flag == 3)
delay(3);
else
delay(1);
flag = seq[i++];
repaint(220,220,320,320);
}
public void delay(int x){
try {
Thread.sleep(x * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void light1(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(275, 223, 10, 10); //Up
g.fillOval(255, 303, 10, 10); //Down
g.setColor(Color.RED);
g.fillOval(308, 305, 10, 10); //Right
g.fillOval(223, 225, 10, 10); //Left
}
void light2(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(223, 255, 10, 10); //Left
g.fillOval(308, 275, 10, 10); //Right
g.setColor(Color.RED);
g.fillOval(305, 223, 10, 10); //Up
g.fillOval(225, 303, 10, 10); //Down
}
void orange(Graphics g)
{
g.setColor(Color.ORANGE);
g.fillOval(290, 223, 10, 10); //UP
g.fillOval(308, 290, 10, 10); //RIGHT
g.fillOval(240, 303, 10, 10); //DOWN
g.fillOval(223, 240 , 10, 10); //LEFT
}
@Override
public void run() {
repaint();
}
}
//Just the layout
class Layout extends JPanel{
private static final long serialVersionUID = 1L;
//Constructor
Layout(){
setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
}
//Override_paint_compnent
protected void paintComponent(Graphics g) {
//Always we need to call the superclass first
super.paintComponent(g);
//Draw the roads
roads(g);
//Draw the Border
g.drawRoundRect(10, 10, 530, 530, 15, 15);
//Traffic Signals
signal(g,305,270,1);//Right
signal(g,220,300,0);//Down
signal(g,220,220,1); //Left
signal(g,270,220,0); //Up
//Sensors
g.setColor(Color.BLUE);
g.fillArc(30, 200, 40, 40, 70, 40);
g.fillArc(300, 30, 40, 40, 340, 40);
g.fillArc(480, 300, 40, 40, 250, 40);
g.fillArc(200, 480, 40, 40, 160, 40);
}
void roads(Graphics g)
{
//Roads
g.drawLine(20, 220, 220, 220);
g.drawLine(220, 220, 220, 20);
g.drawLine(320, 20, 320, 220);
g.drawLine(320, 220, 520, 220);
g.drawLine(20, 320, 220, 320);
g.drawLine(220, 320, 220, 520);
g.drawLine(320, 520, 320, 320);
g.drawLine(320, 320, 520, 320);
//Devider1
g.drawLine(270, 20, 270, 40);
g.drawLine(270, 50, 270, 70);
g.drawLine(270, 80, 270, 100);
g.drawLine(270, 110, 270, 130);
g.drawLine(270, 140, 270, 160);
g.drawLine(270, 170, 270, 190);
g.drawLine(270, 200, 270, 220);
//Devider2
g.drawLine(270, 320, 270, 340);
g.drawLine(270, 350, 270, 370);
g.drawLine(270, 380, 270, 400);
g.drawLine(270, 410, 270, 430);
g.drawLine(270, 440, 270, 460);
g.drawLine(270, 470, 270, 490);
g.drawLine(270, 500, 270, 520);
//Devider3
g.drawLine(20, 270, 40, 270);
g.drawLine(50, 270, 70, 270);
g.drawLine(80, 270, 100, 270);
g.drawLine(110, 270, 130, 270);
g.drawLine(140, 270, 160, 270);
g.drawLine(170, 270, 190, 270);
g.drawLine(200, 270, 220, 270);
//Devider4
g.drawLine(320, 270, 340, 270);
g.drawLine(350, 270, 370, 270);
g.drawLine(380, 270, 400, 270);
g.drawLine(410, 270, 430, 270);
g.drawLine(440, 270, 460, 270);
g.drawLine(470, 270, 490, 270);
g.drawLine(500, 270, 520, 270);
}
void signal(Graphics g, int x, int y, int direction){
if(direction == 1)
{
g.drawOval(x+4, y+6, 7, 7);
g.drawOval(x+4, y+21, 7, 7);
g.drawOval(x+4, y+36, 7, 7);
}
else
{
g.drawOval(x+6, y+4, 7, 7);
g.drawOval(x+21, y+4, 7, 7);
g.drawOval(x+36, y+4, 7, 7);
}
}
}