Created
September 7, 2012 03:50
-
-
Save chrismedrdz/3662916 to your computer and use it in GitHub Desktop.
Semaforo-Adapativos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.*; | |
| import javax.swing.*; | |
| public class Interfaz extends JPanel { | |
| private String escenario = "Crucero.png"; | |
| ImageIcon image_icon1; | |
| private String carrox1="carrox1.png"; | |
| ImageIcon image_icon2; | |
| private String carrox2="carrox2.png"; | |
| ImageIcon image_icon3; | |
| private String carroxi1="carroxi1.png"; | |
| ImageIcon image_icon4; | |
| private String carroxi2="carroxi2.png"; | |
| ImageIcon image_icon5; | |
| private String carroy1="carroy1.png"; | |
| ImageIcon image_icon6; | |
| private String carroy2="carroy2.png"; | |
| ImageIcon image_icon7; | |
| private String carroya1="carroya1.png"; | |
| ImageIcon image_icon8; | |
| private String carroya2="carroya2.png"; | |
| ImageIcon image_icon9; | |
| public int x1,x2,xi1,xi2; | |
| public int y1,y2,ya1,ya2; | |
| public int A; | |
| public Interfaz() { | |
| setFocusable(true); | |
| setBackground(Color.BLACK); | |
| setDoubleBuffered(true); | |
| image_icon1 = new ImageIcon(this.getClass().getResource(escenario)); | |
| image_icon2 = new ImageIcon(this.getClass().getResource(carrox1)); | |
| image_icon3 = new ImageIcon(this.getClass().getResource(carrox2)); | |
| image_icon4 = new ImageIcon(this.getClass().getResource(carroxi1)); | |
| image_icon5 = new ImageIcon(this.getClass().getResource(carroxi2)); | |
| image_icon6 = new ImageIcon(this.getClass().getResource(carroy1)); | |
| image_icon7 = new ImageIcon(this.getClass().getResource(carroy2)); | |
| image_icon8 = new ImageIcon(this.getClass().getResource(carroya1)); | |
| image_icon9 = new ImageIcon(this.getClass().getResource(carroya2)); | |
| x1=0; | |
| x2=50; | |
| xi1=400; | |
| xi2=350; | |
| y1=0; | |
| y2=-50; | |
| ya1=250; | |
| ya2=300; | |
| } | |
| public void paint(Graphics g) { | |
| super.paint(g); | |
| Graphics2D g2d = (Graphics2D)g; | |
| A=1; | |
| if(A==1) { | |
| try { | |
| g2d.drawImage(image_icon1.getImage(), 0, 0, this); | |
| g2d.drawImage(image_icon2.getImage(), x1++,164 , this); | |
| g2d.drawImage(image_icon3.getImage(), x2++,150 , this); | |
| g2d.drawImage(image_icon4.getImage(), xi1--,124 , this); | |
| g2d.drawImage(image_icon5.getImage(), xi2--,137 , this); | |
| g2d.drawImage(image_icon6.getImage(), 173,77 , this); | |
| g2d.drawImage(image_icon7.getImage(), 187,77 , this); | |
| g2d.drawImage(image_icon8.getImage(), 205,197 , this); | |
| g2d.drawImage(image_icon9.getImage(), 220,197 , this); | |
| repaint(); | |
| Thread.sleep(5); | |
| } catch(InterruptedException e){ } | |
| } else { | |
| try { | |
| g2d.drawImage(image_icon1.getImage(), 0, 0, this); | |
| g2d.drawImage(image_icon2.getImage(), 123,164 , this); | |
| g2d.drawImage(image_icon3.getImage(), 123,150 , this); | |
| g2d.drawImage(image_icon4.getImage(), 255,124 , this); | |
| g2d.drawImage(image_icon5.getImage(), 255,137 , this); | |
| g2d.drawImage(image_icon6.getImage(), 173,y1++ , this); | |
| g2d.drawImage(image_icon7.getImage(), 187,y2++ , this); | |
| g2d.drawImage(image_icon8.getImage(), 205,ya1-- , this); | |
| g2d.drawImage(image_icon9.getImage(), 220,ya2-- , this); | |
| repaint(); | |
| Thread.sleep(5); | |
| } catch(InterruptedException e){} | |
| }//Fin del else | |
| A=0; | |
| repaint(); | |
| Toolkit.getDefaultToolkit().sync(); | |
| g.dispose(); | |
| }//Fin del paint | |
| } //Fin de la clase Interfaz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class ListaCarros extends Thread { | |
| public int CarrilA,CarrilB,CarrilC,CarrilD; | |
| public boolean SemAB,SemCD; | |
| public Random Rand = new Random(); | |
| public void run() { | |
| int a,b,c,d; | |
| boolean iniciar = true; | |
| try { | |
| Sistema CtrlSem = new Sistema(); | |
| CtrlSem.start(); | |
| while(true) { | |
| if(iniciar == true) { | |
| a = ( ( Rand.nextInt(10000) % 13) + 1); | |
| CarrilA = CarrilA + a; | |
| if (CarrilA < 0) { CarrilA = 0; } | |
| b = ( ( Rand.nextInt(10000) % 13) + 1); | |
| CarrilB = CarrilB + b; | |
| if (CarrilB < 0) { CarrilB = 0; } | |
| c = ( ( Rand.nextInt(10000) % 10) + 1); | |
| CarrilC = CarrilC + c; | |
| if (CarrilC < 0) { CarrilC = 0; } | |
| d = ( ( Rand.nextInt(10000) % 10) + 1); | |
| CarrilD = CarrilD + d; | |
| if (CarrilD < 0) { CarrilD = 0; } | |
| } | |
| else { | |
| if (SemAB == true) { | |
| CarrilA=CarrilA-3; | |
| CarrilB=CarrilB-2; | |
| if (CarrilA < 0) { CarrilA = 0; } | |
| if (CarrilB < 0) { CarrilB = 0; } | |
| CarrilC=CarrilC+5; | |
| CarrilD=CarrilD+5; | |
| } else { | |
| CarrilC=CarrilC-2; | |
| CarrilD=CarrilD-2; | |
| if (CarrilC < 0) { CarrilC = 0; } | |
| if (CarrilD < 0) { CarrilD = 0; } | |
| CarrilA=CarrilA+3; | |
| CarrilB=CarrilB+3; | |
| } | |
| } | |
| iniciar = false; | |
| CtrlSem.setCarros(CarrilA,CarrilB,CarrilC,CarrilD); | |
| CarrilA = 0; | |
| CarrilB = 0; | |
| CarrilC = 0; | |
| CarrilD = 0; | |
| Thread.sleep(5000); | |
| } //Fin del wile | |
| } catch(InterruptedException e){ | |
| System.out.println("Excepcion: " + e.getMessage()); | |
| } | |
| } //fin del metodo run() | |
| public void setEstado(boolean SAB,boolean SCD) { | |
| SemAB = SAB; | |
| SemCD = SCD; | |
| } | |
| } //Fin de la clase ListaCarros |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.*; | |
| public class Main extends JFrame { | |
| public Main() { | |
| add(new Interfaz()); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| setSize(400,300); | |
| setLocationRelativeTo(null); | |
| setTitle("Practica 1"); | |
| setResizable(false); | |
| setVisible(true); | |
| } | |
| public static void main(String[] args) { | |
| new Main(); | |
| ListaCarros LisCarr = new ListaCarros(); | |
| LisCarr.start(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Sistema extends Thread { | |
| public boolean SemAB,SemCD; //Variables que controlan el estado booleano de los Semaforos AB y CD | |
| public boolean CA,CB,CC,CD; //Variables que controlan el Congestionamiento en cada carril. | |
| public int CarrilA,CarrilB,CarrilC,CarrilD; //Num. de carros en cada carril (obtenidas de ListaCarros) | |
| public String EstadoAB = ""; | |
| public String EstadoCD = ""; | |
| public void run() { | |
| try { | |
| ListaCarros LisCarr = new ListaCarros(); | |
| while(true) { | |
| if (CarrilA > 10) { CA = true; } | |
| if (CarrilB > 10) { CB = true; } | |
| if (CarrilC > 5 ) { CC = true; } | |
| if (CarrilD > 5 ) { CD = true; } | |
| SemAB = (CA | CB | !CD ) & (CA | !CC | !CD ) & (CA | CB | !CC) & (CB | !CC | !CD); | |
| SemCD = !SemAB; | |
| LisCarr.setEstado(SemAB,SemCD); | |
| if (SemAB == true) { | |
| EstadoAB = "Verde"; | |
| EstadoCD = "Rojo"; | |
| } else { | |
| EstadoAB = "Rojo"; | |
| EstadoCD = "Verde"; | |
| } | |
| for(int r=0;r<20;r++){System.out.println("\n");} | |
| System.out.println("\t\t----------------------------------"); | |
| System.out.println("\t\t~Sistema de Control de Semaforos~"); | |
| System.out.println("\t\t----------------------------------"); | |
| System.out.println("\n\n\t\t~Congestionamiento~"); | |
| System.out.println("\n\tHay "+CarrilA+" carros en A"); | |
| System.out.println("\n\tHay "+CarrilB+" carros en B"); | |
| System.out.println("\n\tHay "+CarrilC+" carros en C"); | |
| System.out.println("\n\tHay "+CarrilD+" carros en D"); | |
| System.out.println("\n\n\t\t~Estado Semaforos~"); | |
| System.out.println("\n\tA y B -> "+EstadoAB ); | |
| System.out.println("\n\tC y D -> "+EstadoCD ); | |
| Thread.sleep(5000); | |
| } //Fin del while | |
| } catch(InterruptedException e) { | |
| System.out.println("Excepcion: " + e.getMessage()); | |
| } | |
| } //Termina el run() | |
| public void setCarros(int CaA, int CaB, int CaC, int CaD) { | |
| CarrilA = CaA; | |
| CarrilB = CaB; | |
| CarrilC = CaC; | |
| CarrilD = CaD; | |
| } | |
| }//Termina la clase Sistema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment