LAMPIRAN
LISTING PROGRAM
package simulasi_lalu_lintas;
import java.io.Console; import java.util.ArrayList; import java.util.List; import java.util.Random;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager; import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent;
public class SimulasiLaluLintas {
protected Shell shlSimulasiLampuLalu; protected Boolean check = false; protected Image green;
protected Image red; protected Image yellow; protected Image black; protected Image blackCar;
public static final int RED = 0; public static final int GREEN = 1; public static final int YELLOW = 2;
public boolean start = false; public Text text;
public boolean loop = true;
public int currentSimpang = 1; public int nextSimpang = 2;
public boolean rotate1 = false; public int carType1 = -1;
public int arrowType2 = 1; public boolean rotate2 = false; public int carType2 = -1;
public int arrowType3 = 1; public boolean rotate3 = false; public int carType3 = -1;
public int arrowType4 = 1; public boolean rotate4 = false; public int carType4 = -1;
public int carType = -1;
/*
* Parameter untuk timer */
public static final int ONE_SEC = 1000; //time step in milliseconds
public static final int TENTH_SEC = 100; public Label txtWaktu;
public Thread threadTimer; public Thread threadFuzzy; public Runnable timer;
public Runnable carAnnimation1; public Runnable carAnnimation2; public Runnable carAnnimation3; public Runnable carAnnimation4;
private int clockTick;
/**
* Fuzzy parameter */
public FuzzySet FuzKepadatanKendaraan; public FuzzySet FuzLebarJalan;
public Text txtLebarJalan; public Text txtMaxRandom; public Text txtSepi; public Text txtSedang; public Text txtAgkRamai; public Text txtRamai;
public Text txtSangatRamai; public Text txtMacet;
public Text txtLebar; public Text txtCkpLebar; public Text txtSempit;
/*
* Lampu * * */
Label lblYellow1; Label lblRed1; Label lblGreen2; Label lblYellow2; Label lblRed2; Label lblGreen3; Label lblYellow3; Label lblRed3; Label lblGreen4; Label lblYellow4; Label lblRed4;
private Label lblCar1; private Label lblCar2; private Label lblCar4; private Label lblCar3;
/**
* Launch the application. * @param args
*/
public static void main(String[] args) { try {
SimulasiLaluLintas window = new
SimulasiLaluLintas();
window.open(); } catch (Exception e) {
e.printStackTrace(); }
}
/**
* Open the window. */
public void open() {
Display display = Display.getDefault();
green = new Image(display,
SimulasiLaluLintas.class.getProtectionDomain().getCodeSource().getLoc ation().getPath() + "/simulasi_lalu_lintas/files/green.png");
red = new Image(display,
SimulasiLaluLintas.class.getProtectionDomain().getCodeSource().getLoc ation().getPath() + "/simulasi_lalu_lintas/files/red.png");
yellow = new Image(display,
SimulasiLaluLintas.class.getProtectionDomain().getCodeSource().getLoc ation().getPath() + "/simulasi_lalu_lintas/files/yellow.png");
black = new Image(display,
SimulasiLaluLintas.class.getProtectionDomain().getCodeSource().getLoc ation().getPath() + "/simulasi_lalu_lintas/files/black.png");
blackCar =
SWTResourceManager.getImage(SimulasiLaluLintas.class, "/simulasi_lalu_lintas/files/car/black-car.png");
createContents(display); shlSimulasiLampuLalu.open(); shlSimulasiLampuLalu.layout();
if (!display.readAndDispatch()) { display.sleep();
} }
}
/**
* Create contents of the window. */
protected void createContents(final Display display) { shlSimulasiLampuLalu = new Shell();
shlSimulasiLampuLalu.addShellListener(new ShellAdapter() {
@Override
public void shellClosed(ShellEvent e) { System.out.println("close"); if (threadFuzzy != null) {
if (threadFuzzy.isAlive()) { loop = false;
} }
} });
shlSimulasiLampuLalu.setSize(1000, 600);
shlSimulasiLampuLalu.setText("Simulasi Lampu Lalu Lintas Dengan Algoritma Fuzzy");
shlSimulasiLampuLalu.setLayout(null);
Composite composite = new Composite(shlSimulasiLampuLalu, SWT.NONE);
composite.setBounds(10, 10, 500, 500);
composite.setBackgroundImage(SWTResourceManager.getImage(Simula siLaluLintas.class, "/simulasi_lalu_lintas/files/street.png"));
composite.setBackgroundMode(SWT.INHERIT_FORCE);
lblGreen1 = new Label(composite, SWT.NONE); lblGreen1.setBounds(172, 309, 20, 20); setOnOffLight(lblGreen1, GREEN, false);
lblYellow1 = new Label(composite, SWT.NONE); lblYellow1.setBounds(146, 309, 20, 20); setOnOffLight(lblYellow1, YELLOW, false);
lblRed1 = new Label(composite, SWT.NONE); lblRed1.setBounds(120, 309, 20, 20); setOnOffLight(lblRed1, RED);
lblGreen2 = new Label(composite, SWT.NONE); lblGreen2.setBounds(312, 309, 20, 20); setOnOffLight(lblGreen2, GREEN, false);
lblRed2 = new Label(composite, SWT.NONE); lblRed2.setBounds(312, 361, 20, 20); setOnOffLight(lblRed2, RED);
lblGreen3 = new Label(composite, SWT.NONE); lblGreen3.setBounds(312, 169, 20, 20); setOnOffLight(lblGreen3, GREEN, false);
lblYellow3 = new Label(composite, SWT.NONE); lblYellow3.setBounds(338, 169, 20, 20); setOnOffLight(lblYellow3, YELLOW, false);
lblRed3 = new Label(composite, SWT.NONE); lblRed3.setBounds(364, 169, 20, 20); setOnOffLight(lblRed3, RED);
lblGreen4 = new Label(composite, SWT.NONE); lblGreen4.setBounds(172, 169, 20, 20); setOnOffLight(lblGreen4, GREEN, false);
lblYellow4 = new Label(composite, SWT.NONE); lblYellow4.setBounds(172, 143, 20, 20); setOnOffLight(lblYellow4, YELLOW, false);
lblRed4 = new Label(composite, SWT.NONE); lblRed4.setBounds(172, 117, 20, 20); setOnOffLight(lblRed4, RED);
lblCar1 = new Label(composite, SWT.NONE); lblCar1.setBounds(127, 206, 65, 35); lblCar1.setText("car");
lblCar1.setImage(getImage(SWT.LEFT));
lblCar2 = new Label(composite, SWT.NONE); lblCar2.setText("car");
lblCar2.setImage(getImage(SWT.DOWN)); lblCar2.setBounds(211, 309, 35, 65);
lblCar3 = new Label(composite, SWT.NONE); lblCar3.setBounds(312, 255, 65, 35); lblCar3.setText("car");
lblCar3.setImage(getImage(SWT.RIGHT));
lblCar4 = new Label(composite, SWT.NONE); lblCar4.setBounds(258, 124, 35, 65); lblCar4.setText("car");
lblCar4.setImage(getImage(SWT.UP));
txtWaktu = new Label(shlSimulasiLampuLalu, SWT.NONE); txtWaktu.setFont(SWTResourceManager.getFont("Segoe UI", 19, SWT.NORMAL));
txtWaktu.setBounds(536, 10, 64, 35); txtWaktu.setText("00");
Group grpFuzzySet = new Group(shlSimulasiLampuLalu,
grpFuzzySet.setText("Fuzzy Set : Kepadatan jalan"); grpFuzzySet.setBounds(536, 169, 191, 182);
Label lblSepi = new Label(grpFuzzySet, SWT.NONE); lblSepi.setBounds(10, 22, 55, 15);
lblSepi.setText("Sepi");
Label lblSedang = new Label(grpFuzzySet, SWT.NONE); lblSedang.setText("Sedang");
lblSedang.setBounds(10, 46, 55, 15);
Label lblAgakRamai = new Label(grpFuzzySet, SWT.NONE); lblAgakRamai.setText("Agak ramai");
lblAgakRamai.setBounds(10, 70, 67, 15);
Label lblRamai = new Label(grpFuzzySet, SWT.NONE); lblRamai.setText("Ramai");
lblRamai.setBounds(10, 95, 67, 15);
Label lblSangatRamai = new Label(grpFuzzySet, SWT.NONE); lblSangatRamai.setText("Sangat Ramai");
lblSangatRamai.setBounds(10, 118, 76, 15);
Label lblMacet = new Label(grpFuzzySet, SWT.NONE); lblMacet.setText("Macet");
lblMacet.setBounds(10, 143, 67, 15);
txtSepi = new Text(grpFuzzySet, SWT.BORDER); txtSepi.setText("0,0,10,15");
txtSepi.setBounds(92, 19, 90, 21);
txtSedang = new Text(grpFuzzySet, SWT.BORDER); txtSedang.setText("10,15,20,25");
txtSedang.setBounds(92, 43, 90, 21);
txtAgkRamai = new Text(grpFuzzySet, SWT.BORDER); txtAgkRamai.setText("20,25,30,35");
txtAgkRamai.setBounds(92, 67, 90, 21);
txtRamai = new Text(grpFuzzySet, SWT.BORDER); txtRamai.setText("30,35,40,45");
txtRamai.setBounds(92, 91, 90, 21);
txtSangatRamai = new Text(grpFuzzySet, SWT.BORDER); txtSangatRamai.setText("40,45,50,55");
txtSangatRamai.setBounds(92, 115, 90, 21);
txtMacet = new Text(grpFuzzySet, SWT.BORDER); txtMacet.setText("50,55,70,100");
txtMacet.setBounds(92, 140, 90, 21);
final Button btnStart = new Button(shlSimulasiLampuLalu, SWT.NONE);
btnStart.setBounds(536, 69, 75, 25); btnStart.setText("Start");
final Button btnStop = new Button(shlSimulasiLampuLalu, SWT.NONE);
btnStop.setEnabled(false);
btnStop.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
btnStop.setBounds(625, 69, 75, 25); btnStop.setText("Stop");
text = new Text(shlSimulasiLampuLalu, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
text.setBounds(536, 368, 422, 142);
Group grpFuzzySet_1 = new Group(shlSimulasiLampuLalu, SWT.NONE);
grpFuzzySet_1.setText("Fuzzy Set : Lebar jalan"); grpFuzzySet_1.setBounds(767, 169, 191, 182);
Label lblSempit = new Label(grpFuzzySet_1, SWT.NONE); lblSempit.setText("Sempit");
lblSempit.setBounds(10, 23, 55, 15);
Label lblCukupLebar = new Label(grpFuzzySet_1, SWT.NONE); lblCukupLebar.setText("Cukup Lebar");
lblCukupLebar.setBounds(10, 47, 76, 15);
Label lblLebar = new Label(grpFuzzySet_1, SWT.NONE); lblLebar.setText("Lebar");
lblLebar.setBounds(10, 71, 67, 15);
txtLebar = new Text(grpFuzzySet_1, SWT.BORDER); txtLebar.setText("5,5.5,7,8");
txtLebar.setBounds(92, 68, 90, 21);
txtCkpLebar = new Text(grpFuzzySet_1, SWT.BORDER); txtCkpLebar.setText("3,3.5,5,5.5");
txtCkpLebar.setBounds(92, 44, 90, 21);
txtSempit = new Text(grpFuzzySet_1, SWT.BORDER); txtSempit.setText("0,0,3,3.5");
txtSempit.setBounds(92, 20, 90, 21);
Label lblLebarJalan = new Label(shlSimulasiLampuLalu, SWT.NONE);
lblLebarJalan.setBounds(545, 136, 55, 15); lblLebarJalan.setText("Lebar Jalan :");
txtLebarJalan = new Text(shlSimulasiLampuLalu,
SWT.BORDER);
txtLebarJalan.setText("3.2");
Label lblMaxRandomKepadatan = new Label(shlSimulasiLampuLalu, SWT.NONE);
lblMaxRandomKepadatan.setBounds(545, 112, 154, 15);
lblMaxRandomKepadatan.setText("Max random kepadatan
Jalan");
txtMaxRandom = new Text(shlSimulasiLampuLalu,
SWT.BORDER);
txtMaxRandom.setText("100");
txtMaxRandom.setBounds(721, 106, 76, 21);
timer = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub if (txtWaktu.isDisposed()) {
return; }
if (clockTick >= 0) {
txtWaktu.setText(Integer.toString(clockTick)); start = false;
//threadMessage(Integer.toString(clockTick)); clockTick--;
}else if (!start) { start = true; }
display.timerExec(ONE_SEC, this); }
};
carAnnimation1 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub Rectangle rec = lblCar1.getBounds(); if (currentSimpang == 1) {
if(arrowType1 == 1) {
lblCar1.setBounds(rec.x + 3,
rec.y, rec.width, rec.height);
if ((rec.x + rec.width) >= 565) { carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
carType1 = carType;
lblCar1.setBounds(0, rec.y, rec.width, rec.height);
arrowType1 = 2; }
lblCar1.setBounds(rec.x + 3, rec.y, rec.width, rec.height);
if(rec.x >= 228){
if(rotate1 == false) {
carType = carType1;
lblCar1.setImage(getImage(SWT.UP));
lblCar1.setBounds(258, 223, rec.height, rec.width);
rotate1 = true; }else
{
lblCar1.setBounds(rec.x, rec.y + 3, rec.width, rec.height); }
}
if ((rec.y + rec.height) >= 565) {
carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
carType1 = carType;
lblCar1.setBounds(0, 206,
65, 35);
arrowType1 = 3; rotate1 = false; }
} else if(arrowType1 == 3) {
lblCar1.setBounds(rec.x + 3,
rec.y, rec.width, rec.height);
if(rec.x >= 181){
if(rotate1 == false) {
carType = carType1;
lblCar1.setImage(getImage(SWT.DOWN));
lblCar1.setBounds(212, 169, rec.height, rec.width);
rotate1 = true; }else
{
lblCar1.setBounds(rec.x, rec.y - 3, rec.width, rec.height); }
}
if ((rec.y + rec.height) <= 0) { carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
carType1= carType;
lblCar1.setBounds(0, 206,
65, 35);
rotate1 = false; }
}
}else if(rec.y > 223){
lblCar1.setBounds(rec.x, rec.y +3,
rec.width, rec.height);
if ((rec.y + rec.height) >= 565) { carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
lblCar1.setBounds(0, 206, 65,
35);
}
}else if (rec.y < 169) {
lblCar1.setBounds(rec.x, rec.y -3,
rec.width, rec.height);
if ((rec.y + rec.height) <= 0) { carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
lblCar1.setBounds(0, 206, 65,
35);
}
}else if(rec.x > 127 || rec.x < 120){
lblCar1.setBounds(rec.x + 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) >= 565) { carType = carType1 = -1;
lblCar1.setImage(getImage(SWT.LEFT));
lblCar1.setBounds(0, rec.y,
rec.width, rec.height);
} }
display.timerExec(10, this); }
};
carAnnimation2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub Rectangle rec = lblCar2.getBounds(); if (currentSimpang == 2) {
if (arrowType2 == 1) {
lblCar2.setBounds(rec.x, rec.y -3, rec.width, rec.height);
if ((rec.y + rec.height) <= 0) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
arrowType2 = 2; }
} else if(arrowType2 == 2){
lblCar2.setBounds(rec.x, rec.y -3, rec.width, rec.height);
if(rec.y <= 206){
if(rotate2 == false) {
carType = carType2;
lblCar2.setImage(getImage(SWT.LEFT));
lblCar2.setBounds(216, 205, rec.height, rec.width);
rotate2 = true; }else
{
lblCar2.setBounds(rec.x + 3, rec.y, rec.width, rec.height); }
}
if ((rec.x + rec.width) >= 565) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
carType2 = carType;
lblCar2.setBounds(211, 500, 35, 65);
arrowType2 = 3; rotate2 = false; }
} else if(arrowType2 == 3){
lblCar2.setBounds(rec.x, rec.y -3, rec.width, rec.height);
if(rec.y <= 270){
if(rotate2 == false) {
carType = carType2;
lblCar2.setImage(getImage(SWT.RIGHT));
lblCar2.setBounds(148, 256, rec.height, rec.width);
rotate2 = true; }else
{
lblCar2.setBounds(rec.x - 3, rec.y, rec.width, rec.height); }
}
if ((rec.x + rec.width) <= 0) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
lblCar2.setBounds(211, 500, 35, 65);
arrowType2 = 1; rotate2 = false; }
}
}else if(rec.x > 216){
lblCar2.setBounds(rec.x + 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) >= 565) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
carType3 = carType;
lblCar2.setBounds(211, 500, 35, 65);
}
}else if(rec.x < 148) {
lblCar2.setBounds(rec.x - 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) <= 0) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
carType3 = carType;
lblCar2.setBounds(211, 500, 35, 65);
}
}else if (rec.y < 309 || rec.y > 314) {
lblCar2.setBounds(rec.x, rec.y -3,
rec.width, rec.height);
if ((rec.y + rec.height) <= 0) { carType = carType2 = -1;
lblCar2.setImage(getImage(SWT.DOWN));
carType3 = carType;
lblCar2.setBounds(rec.x, 500,
rec.width, rec.height);
} }
display.timerExec(10, this); }
};
carAnnimation3 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub Rectangle rec = lblCar3.getBounds(); if (currentSimpang == 3) {
if (arrowType3 == 1) {
lblCar3.setBounds(rec.x - 3,
if ((rec.x + rec.width) <= 0) { carType = carType3 = -1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType; lblCar3.setBounds(565, rec.y, rec.width, rec.height);
arrowType3 = 2; }
} else if(arrowType3 == 2) {
lblCar3.setBounds(rec.x - 3,
rec.y, rec.width, rec.height);
if(rec.x <= 227){
if(rotate3 == false) {
carType = carType3;
lblCar3.setImage(getImage(SWT.DOWN));
lblCar3.setBounds(210, 200, rec.height, rec.width);
rotate3 = true; }else
{
lblCar3.setBounds(rec.x, rec.y - 3, rec.width, rec.height); }
}
if ((rec.y + rec.height) <= 0) { carType = carType3 = -1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType;
lblCar3.setBounds(565, 255, 65, 35);
arrowType3 = 3; rotate3 = false; }
}else if(arrowType3 == 3) {
lblCar3.setBounds(rec.x - 3,
rec.y, rec.width, rec.height);
if(rec.x <= 267){
if(rotate3 == false) {
carType = carType3;
lblCar3.setImage(getImage(SWT.UP));
lblCar3.setBounds(259, 271, rec.height, rec.width);
rotate3 = true; }else
{
lblCar3.setBounds(rec.x, rec.y + 3, rec.width, rec.height); }
if ((rec.y + rec.height) >= 565) {
carType = carType3 = -1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType;
lblCar3.setBounds(565, 255, 65, 35);
arrowType3 = 1; rotate3 = false; }
}
} else if(rec.y <= 200){
lblCar3.setBounds(rec.x, rec.y - 3, rec.width, rec.height);
if ((rec.y + rec.height) <= 0) { carType = carType3 = -1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType;
lblCar3.setBounds(565, 255, 65, 35);
}
} else if(rec.y >= 271){
lblCar3.setBounds(rec.x, rec.y + 3, rec.width, rec.height);
if ((rec.y + rec.height) >= 565) { carType = carType3 = -1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType;
lblCar3.setBounds(565, 255, 65, 35);
}
}else if (rec.x < 312 || rec.x > 315) {
lblCar3.setBounds(rec.x - 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) <= 0) { carType = carType3 = 1;
lblCar3.setImage(getImage(SWT.RIGHT));
carType3 = carType;
lblCar3.setBounds(565, rec.y,
rec.width, rec.height);
} }
display.timerExec(10, this); }
};
carAnnimation4 = new Runnable() {
@Override
// TODO Auto-generated method stub Rectangle rec = lblCar4.getBounds(); if (currentSimpang == 4) {
if(arrowType4 == 1){
lblCar4.setBounds(rec.x, rec.y
+3, rec.width, rec.height);
if ((rec.y + rec.height) >= 565) {
carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(rec.x, -65, rec.width, rec.height);
arrowType4 = 2; }
}else if (arrowType4 == 2) {
lblCar4.setBounds(rec.x, rec.y
+3, rec.width, rec.height);
if(rec.y >= 210){
if(rotate4 == false) {
carType = carType4;
lblCar4.setImage(getImage(SWT.RIGHT));
lblCar4.setBounds(211, 255, rec.height, rec.width);
rotate4 = true; }else
{
lblCar4.setBounds(rec.x -3, rec.y, rec.width, rec.height); }
}
if ((rec.x + rec.width) <= 0) { carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(258, -65, 35, 65);
arrowType4 = 3; rotate4 = false; }
}else if (arrowType4 == 3) {
lblCar4.setBounds(rec.x, rec.y
+3, rec.width, rec.height);
if(rec.y >= 160){
if(rotate4 == false) {
carType = carType4;
lblCar4.setImage(getImage(SWT.LEFT));
rotate4 = true; }else
{
lblCar4.setBounds(rec.x +3, rec.y, rec.width, rec.height); }
}
if ((rec.x + rec.width) >= 565) { carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(258, -65, 35, 65);
arrowType4 = 1; rotate4 = false; }
}
} else if(rec.x <= 210){
lblCar4.setBounds(rec.x - 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) <= 0) { carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(258, -65, 35, 65);
}
} else if(rec.x >= 280){
lblCar4.setBounds(rec.x + 3, rec.y, rec.width, rec.height);
if ((rec.x + rec.width) >= 565) { carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(258, -65, 35, 65);
}
} else if(rec.y > 124 || rec.y < 120){
lblCar4.setBounds(rec.x, rec.y +3,
rec.width, rec.height);
if ((rec.y + rec.height) >= 565) { carType = carType4 = -1;
lblCar4.setImage(getImage(SWT.UP));
carType4 = carType;
lblCar4.setBounds(rec.x, -65,
rec.width, rec.height);
} }
};
btnStart.addSelectionListener(new SelectionAdapter() { @Override
public void widgetSelected(SelectionEvent e) {
MessageBox msg = new
MessageBox(shlSimulasiLampuLalu, SWT.ICON_ERROR | SWT.YES);
if (txtLebarJalan.getText().isEmpty() ||
!isNan(txtLebarJalan.getText())) {
msg.setMessage("Lebar jalan harus diisi dan harus bilangan");
msg.open(); return; }
if(txtSepi.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Sepi tidak boleh kosong");
msg.open(); return; }else
if(!checkFuzzyFormat(txtSepi.getText())){
msg.setMessage("Fuzzy set untuk
variable Sepi tidak sesuai format"); msg.open(); return; }
if(txtSedang.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Sedang tidak boleh kosong"); msg.open(); return; }else
if(!checkFuzzyFormat(txtSedang.getText())){
msg.setMessage("Fuzzy set untuk
variable Sedang tidak sesuai format"); msg.open(); return; }
if(txtAgkRamai.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Agak Ramai tidak boleh kosong"); msg.open(); return; }else
if(!checkFuzzyFormat(txtAgkRamai.getText())){
msg.setMessage("Fuzzy set untuk
variable Agak Ramai tidak sesuai format"); msg.open(); return; }
msg.setMessage("Fuzzy set untuk variable Ramai tidak boleh kosong");
msg.open(); return; }else
if(!checkFuzzyFormat(txtRamai.getText())){
msg.setMessage("Fuzzy set untuk
variable Ramai tidak sesuai format"); msg.open(); return; }
if(txtSangatRamai.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Sangat Ramai tidak boleh kosong"); msg.open(); return; }else
if(!checkFuzzyFormat(txtSangatRamai.getText())){
msg.setMessage("Fuzzy set untuk
variable Sangat Ramai tidak sesuai format"); msg.open(); return; }
if(txtSempit.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Sempit tidak boleh kosong"); msg.open(); return; }else
if(!checkFuzzyFormat(txtSempit.getText())){
msg.setMessage("Fuzzy set untuk
variable Sempit tidak sesuai format"); msg.open(); return; }
if(txtCkpLebar.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
variable Cukup Lebar tidak boleh kosong"); msg.open(); return; }else
if(!checkFuzzyFormat(txtCkpLebar.getText())){
msg.setMessage("Fuzzy set untuk
variable Cukup Lebar tidak sesuai format"); msg.open(); return; }
if(txtLebar.getText().isEmpty()){
msg.setMessage("Fuzzy set untuk
}else
if(!checkFuzzyFormat(txtLebar.getText())){
msg.setMessage("Fuzzy set untuk
variable Lebar tidak sesuai format"); msg.open(); return; }
if(txtMaxRandom.getText().isEmpty() ||
!isNan(txtMaxRandom.getText())){
msg.setMessage("Fuzzy set untuk
variable Max Random Kepadatan Jalan tidak boleh kosong dan harus bilangan");
msg.open(); return; }
System.out.println("currentSimpang: " + currentSimpang);
System.out.println("nextSimpang: " +
nextSimpang);
start = true; loop = true;
setFuzzyParameter();
setFuzzyThread(Float.parseFloat(txtLebarJalan.getText()), Integer.parseInt(txtMaxRandom.getText()));
threadFuzzy.start();
display.timerExec(ONE_SEC, timer); display.timerExec(3, carAnnimation1); display.timerExec(3, carAnnimation2); display.timerExec(3, carAnnimation3); display.timerExec(3, carAnnimation4); btnStart.setEnabled(false);
btnStop.setEnabled(true); }
});
btnStop.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) { start = false;
loop = false;
display.timerExec(-1, timer);
display.timerExec(-1, carAnnimation1); display.timerExec(-1, carAnnimation2); display.timerExec(-1, carAnnimation3); display.timerExec(-1, carAnnimation4); btnStart.setEnabled(true);
btnStop.setEnabled(false); resetUI();
shlSimulasiLampuLalu.addListener(SWT.CLOSE, new Listener() {
@Override
public void handleEvent(Event event) { // TODO Auto-generated method stub System.out.println("close");
threadFuzzy.interrupt(); }
}); }
public void resetUI() {
txtWaktu.setText("00");
lblCar1.setBounds(127, 206, 65, 35); lblCar2.setBounds(211, 309, 35, 65); lblCar3.setBounds(312, 255, 65, 35); lblCar4.setBounds(258, 124, 35, 65); setOnOffLight(lblGreen1, GREEN, false); setOnOffLight(lblYellow1, YELLOW, false); setOnOffLight(lblRed1, RED);
setOnOffLight(lblGreen2, GREEN, false); setOnOffLight(lblYellow2, YELLOW, false); setOnOffLight(lblRed2, RED);
setOnOffLight(lblGreen3, GREEN, false); setOnOffLight(lblYellow3, YELLOW, false); setOnOffLight(lblRed3, RED);
setOnOffLight(lblGreen4, GREEN, false); setOnOffLight(lblYellow4, YELLOW, false); setOnOffLight(lblRed4, RED);
currentSimpang = 1; nextSimpang = 2;
arrowType1 = 1; rotate1 = false; carType1 = -1;
arrowType2 = 1; rotate2 = false; carType2 = -1;
arrowType3 = 1; rotate3 = false; carType3 = -1;
arrowType4 = 1; rotate4 = false; carType4 = -1;
System.out.println("currentSimpang: " + currentSimpang); System.out.println("nextSimpang: " + nextSimpang);
}
public boolean isNan(String nan) { try {
// TODO: handle exception return false;
}
return true; }
public boolean checkFuzzyFormat(String data) { String[] arrFuzzy = data.split(","); if (arrFuzzy.length != 4) {
return false; }else{
for (String item : arrFuzzy) { if (!isNan(item)) {
return false; }
} }
return true; }
public FuzzyItem parseToFuzzyItem(String data, String name, int type) {
String[] arrFuzzy = data.split(",");
return new FuzzyItem(Float.parseFloat(arrFuzzy[0]),
Float.parseFloat(arrFuzzy[1]), Float.parseFloat(arrFuzzy[2]), Float.parseFloat(arrFuzzy[3]), name, type); }
protected void setOnOffLight(final Label label, final int color, final boolean on) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub if (on) {
if (color == RED){
label.setImage(red);
//threadMessage("SET RED"); }else if (color == GREEN){
label.setImage(green);
//threadMessage("SET GREEN"); }else if(color == YELLOW){
label.setImage(yellow);
//threadMessage("SET YELLOW"); }
}else{
label.setImage(black); }
} }); }
protected void setOnOffLight(Label label, int color){ setOnOffLight(label, color, true);
protected void setLight(Label redLight, Label yellowLight, Label greenLight, int light){
if (light == RED) {
setOnOffLight(redLight, RED);
setOnOffLight(yellowLight, YELLOW, false); setOnOffLight(greenLight, GREEN, false); //threadMessage("RED");
} else if (light == YELLOW) {
setOnOffLight(redLight, RED, false); setOnOffLight(yellowLight, YELLOW); setOnOffLight(greenLight, GREEN, false); //threadMessage("YELLOW");
}else if (light == GREEN){
setOnOffLight(redLight, RED, false);
setOnOffLight(yellowLight, YELLOW, false); setOnOffLight(greenLight, GREEN);
//threadMessage("GREEN"); }
}
public void processSimpang(final int status, int simpang){ if (simpang == 1) {
setLight(lblRed1, lblYellow1, lblGreen1, status); } else if (simpang == 2) {
setLight(lblRed2, lblYellow2, lblGreen2, status); } else if (simpang == 3) {
setLight(lblRed3, lblYellow3, lblGreen3, status); } else if (simpang == 4) {
setLight(lblRed4, lblYellow4, lblGreen4, status); }
//threadMessage("Process simpang : " +
Integer.toString(simpang)); }
public void setNextSimpang() { currentSimpang++;
if (currentSimpang == 5) { currentSimpang = 1; }
nextSimpang = currentSimpang + 1;
if (nextSimpang == 5) { nextSimpang = 1; }
}
public void setFuzzyParameter() {
FuzKepadatanKendaraan = new FuzzySet();
FuzKepadatanKendaraan.getItemList().add(parseToFuzzyItem(txtSed ang.getText(), "Sedang", FuzzyItem.NORMAL));
FuzKepadatanKendaraan.getItemList().add(parseToFuzzyItem(txtAgk Ramai.getText(), "Agak Ramai", FuzzyItem.NORMAL));
FuzKepadatanKendaraan.getItemList().add(parseToFuzzyItem(txtRam ai.getText(), "Ramai", FuzzyItem.NORMAL));
FuzKepadatanKendaraan.getItemList().add(parseToFuzzyItem(txtSan gatRamai.getText(), "Sangat Ramai", FuzzyItem.NORMAL));
FuzKepadatanKendaraan.getItemList().add(parseToFuzzyItem(txtMac et.getText(), "Macet", FuzzyItem.NO_DOWN));
FuzLebarJalan = new FuzzySet();
FuzLebarJalan.getItemList().add(parseToFuzzyItem(txtSempit.getT ext(), "Sempit", FuzzyItem.NO_UP));
FuzLebarJalan.getItemList().add(parseToFuzzyItem(txtCkpLebar.ge tText(), "Cukup Lebar", FuzzyItem.NORMAL));
FuzLebarJalan.getItemList().add(parseToFuzzyItem(txtLebar.getTe xt(), "Lebar", FuzzyItem.NO_DOWN));
}
public List<Inference> getFuzificationResult(float value, FuzzySet fuzzy, StringBuilder msg) {
List<Inference> result = new ArrayList<Inference>(); FuzzyItem item1 = new FuzzyItem();
int type = fuzzy.getOption(item1, value); if (type != FuzzySet.LINIER) {
if (type == FuzzySet.NAIK) {
Inference inf = new
Inference(item1.getName(), fuzzy.getDerajatKeanggotaan(value,
item1.getA(), item1.getB(), type));
msg.append(item1.getName() + " : " + inf.value + "\n");
result.add(inf); }else{
FuzzyItem item2 = new FuzzyItem();
int type2 = fuzzy.getOption(item1, item2, value);
Inference inf = new
Inference(item1.getName(), fuzzy.getDerajatKeanggotaan(value,
item1.getC(), item1.getD(), type));
msg.append(item1.getName() + " : " + inf.value + "\n");
result.add(inf);
Inference inf2 = new
Inference(item2.getName(), fuzzy.getDerajatKeanggotaan(value,
msg.append(item2.getName() + " : " + inf2.value + "\n");
result.add(inf2); }
} else {
Inference inf = new Inference(item1.getName(),
fuzzy.getDerajatKeanggotaan(value, item1.getB(), item1.getC(),
type));
msg.append(item1.getName() + " : " + inf.value + "\n");
result.add(inf); }
return result; }
public void setTimer() {
threadTimer = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (clockTick >= 0) { try {
//txtWaktu.setText(Integer.toString(clockTick));
start = false;
//threadMessage(Integer.toString(clockTick));
clockTick--;
Thread.sleep(ONE_SEC);
} catch
(InterruptedException e) {
// TODO: handle
exception
} }
processSimpang(YELLOW, currentSimpang);
processSimpang(YELLOW, nextSimpang);
} }); }
public void resetTimer(int clock) { clockTick = clock;
}
public void setFuzzyThread(final Float lebarJalan, final int maxRandom){
threadFuzzy = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub while (loop) {
try {
if (start && clockTick < 0) { processSimpang(YELLOW, currentSimpang);
processSimpang(YELLOW, nextSimpang);
Thread.sleep(2000);
processSimpang(RED,currentSimpang);
setNextSimpang(); }
if (start) {
StringBuilder message = new StringBuilder();
Random rand = new Random();
int rndKepadatan =
rand.nextInt(maxRandom)+1;
message.append("Lebar Jalan : " + lebarJalan + "\nKepadatan Jalan :" + rndKepadatan + "\n\n");
message.append("Fuzzyfikasi \n---\n");
List<Inference> inf = Inference.conjuction(getFuzificationResult(rndKepadatan,
FuzKepadatanKendaraan, message), getFuzificationResult(lebarJalan,
FuzLebarJalan, message), message);
/*Inference.disjunction(inf);*/
Defuzzyfication def = new Defuzzyfication();
int redTime = (int)
def.defuzzyficationProcess(Inference.disjunction(inf, message));
message.append("\nDefuzzyfication\n---\n Waktu
merah : " + redTime);
threadSetTextBox(message.toString());
processSimpang(GREEN, currentSimpang);
resetTimer(redTime);
threadMessage(Boolean.toString(start));
//threadMessage("Main Thread start : " + start + " time : " + Integer.toString(clockTick));
Thread.sleep(ONE_SEC);
} catch (InterruptedException e) { // TODO: handle exception }
} }
}, "Main"); }
public void threadSetTextBox(final String message) { Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub text.setText(message);
} }); }
static void threadMessage(String message) { String threadName =
Thread.currentThread().getName(); System.out.format("%s: %s%n",
threadName, message); }
public Image rotateImage(Image image, int rotate) { ImageData sd = image.getImageData();
ImageData dd = new ImageData(sd.height, sd.height, sd.depth, sd.palette);
boolean up = (rotate & SWT.UP) == SWT.UP;
// Run through the horizontal pixels for (int sx = 0; sx < sd.width; sx++) { // Run through the vertical pixels
for (int sy = 0; sy < sd.height; sy++) {
// Determine where to move pixel to in destination image data
int dx = up ? sy : sd.height - sy - 1; int dy = up ? sd.width - sx - 1 : sx;
// Swap the x, y source data to y, x in the destination dd.setPixel(dx, dy, sd.getPixel(sx, sy));
} }
// Create the vertical image
Image vertical = new Image(Display.getDefault(), dd); return vertical;
public Image rotateImage(Image image, int swtStyle, int rotate) {
Image temp = image;
if (swtStyle != SWT.UP) {
temp = rotateImage(temp, swtStyle); } else {
for (int i = 0; i < rotate; i++) { temp = rotateImage(temp, SWT.UP); }
}
return temp; }
public Image getImage(int arrow) { Image temp;
String arrowName = ""; Random rnd = new Random();
int type = (carType == -1)? rnd.nextInt(3)+1 : carType; carType = type;
//to get car arrow
if (arrow == SWT.RIGHT) { arrowName = "-right"; } else if(arrow == SWT.LEFT) {
arrowName = "-left"; }else if (arrow == SWT.UP) {
arrowName = "-up";
}else if (arrow == SWT.DOWN) { arrowName = "-down"; }
if (type == 1) {
temp =
SWTResourceManager.getImage(SimulasiLaluLintas.class,
"/simulasi_lalu_lintas/files/car/black-car"+ arrowName +".png"); } else if(type == 2){
temp =
SWTResourceManager.getImage(SimulasiLaluLintas.class,
"/simulasi_lalu_lintas/files/car/red-car"+ arrowName +".png"); } else {
temp =
SWTResourceManager.getImage(SimulasiLaluLintas.class,
"/simulasi_lalu_lintas/files/car/green-car"+ arrowName +".png"); }
return temp; }