Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //INICIA LA CLASE TRAFICO
- package semaforo;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import javax.swing.JFrame;
- public class Trafico extends Thread {
- private int cola[] = {0, 0, 0, 0, 0, 0};
- private int posX[] = {620, 620, 330, 330, 530, 580};
- private int posY[] = {90, 130, 180, 220, 300, 300};
- private Graphics gr;
- private Color COLORS[] = {Color.green, Color.red, Color.blue};
- private Random r;
- private Datos data;
- private BufferedImage clr;
- private JFrame ventana;
- public Trafico(Datos data, BufferedImage cl) {
- this.gr = data.getGraphics();
- this.data = data;
- this.clr = cl;
- this.ventana = data.getVentana();
- r = new Random();
- }
- public void run() {
- try {
- int t, tiempo = 1000;
- while (true) {
- for(int sem = 0; sem < 4; sem++) {
- data.pausa();
- System.out.println(" - - Semaforo " + Integer.toString(sem + 1) );
- t = optimizar(sem);
- for(int i = 0; i < t; i++) {
- autos();
- avanza(sem);
- debug();
- sleep(tiempo);
- }
- data.go();
- }
- }
- } catch(Exception e){
- System.out.println("-- ERROR --");
- }
- }
- private int optimizar(int v) {
- int a;
- if (v == 0) {
- a = Math.max(cola[0] / 2, Math.max(cola[1], cola[5]) );
- }
- else if (v == 1) {
- a = Math.max(cola[0], cola[2] / 3 );
- }
- else if (v == 2) {
- a = Math.max(cola[2], cola[3] / 3 );
- }
- else {
- a = Math.max(cola[4], Math.max(cola[5], cola[3]) );
- }
- a = Math.max(a+3, 6);
- if(a > 15){
- a = 18;
- }
- return a;
- }
- private void debug() {
- for (int i= 0; i < 6; i++) {
- System.out.print(cola[i] + " ");
- }
- System.out.println();
- }
- private void avanza(int v) {
- if (v == 0) {
- cola[0] = salir(cola[0], 0);
- cola[1] = salir(cola[1], 1);
- cola[5] = salir(cola[5], 5);
- }
- else if (v == 1) {
- cola[0] = salir(cola[0], 0);
- cola[2] = salir(cola[2], 2);
- }
- else if(v == 2) {
- cola[2] = salir(cola[2], 2);
- cola[3] = salir(cola[3], 3);
- }
- else {
- cola[3] = salir(cola[3], 3);
- cola[4] = salir(cola[4], 4);
- cola[5] = salir(cola[5], 5);
- }
- }
- private int salir(int valor, int sverde) {
- if(valor > 0) {
- valor -= 1;
- clear(sverde);
- }
- return valor;
- }
- private void clear(int c) {
- int a = 0, b = 0;
- if (c/2 == 0) {
- a = 20;
- }
- else if (c/2 == 1) {
- a = -20;
- }
- else {
- b = 20;
- }
- posX[c] -= a;
- posY[c] -= b;
- gr.drawImage(clr, posX[c], posY[c], ventana);
- }
- private void autos() {
- int cantidad, i;
- for (int j = 0; j < 2; j++) {
- cantidad = r.nextInt(1) + 1;
- i = r.nextInt(6);
- cola[i] += cantidad;
- paint(cantidad, i);
- }
- }
- private void paint(int n, int c) {
- int a = 0, b = 0;
- gr.setColor(COLORS[c/2]);
- if (c/2 == 0) {
- a = 20;
- }
- else if (c/2 == 1) {
- a = -20;
- }
- else {
- b = 20;
- }
- for(int i = 0; i< n; i++) {
- gr.fillRect(posX[c], posY[c], 15, 15);
- posX[c] += a;
- posY[c] += b;
- }
- }
- }
- //TERMINA LA CLASE TRAFICO
- //INICIA LA CLASE MAIN
- package semaforo;
- public class Main{
- public static void main(String[] args) {
- Datos data = new Datos();
- Panel ambiente = new Panel(data);
- Trafico autos = new Trafico(data, ambiente.getClear() );
- ambiente.start();
- autos.start();
- }
- }
- //TERMINA LA CLASE MAIN
- //INICIA LA CLASE PANEL
- package semaforo;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- public class Panel extends Thread {
- private JFrame ventana;
- private String sem1, sem2, sem3, sem4;
- private BufferedImage mapa, s1, s2, s3, s4, clear;
- int tiempoespera;
- private Datos data;
- public Panel(Datos data){
- this.data = data;
- cargarimagen(1);
- data.getGraphics().drawImage(mapa,0,0, ventana);
- }
- public void run(){
- try {
- System.out.println("arranca hilo");
- while(true) {
- for(int i=1; i<=4; i++) {
- cargarimagen(i);
- paint(data.getGraphics());
- System.out.println("semaforo " + i);
- data.go();
- data.pausa();
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public void cargarimagen(int secuencia){
- int valor = secuencia;
- if(valor == 1){
- sem1 = "imagenes/s1rv.png"; // arriba
- sem2 = "imagenes/s2r.png"; // peaton
- sem3 = "imagenes/s3rr.png"; // derecha
- sem4 = "imagenes/s4vv.png"; // izq
- }
- else if(valor == 2){
- sem1 = "imagenes/s1rr.png";
- sem2 = "imagenes/s2v.png";
- sem3 = "imagenes/s3vr.png";
- sem4 = "imagenes/s4vr.png";
- }
- else if(valor == 3){
- sem1 = "imagenes/s1rr.png";
- sem2 = "imagenes/s2r.png";
- sem3 = "imagenes/s3vv.png";
- sem4 = "imagenes/s4vr.png";
- }
- else {
- sem1 = "imagenes/s1vv.png";
- sem2 = "imagenes/s2r.png";
- sem3 = "imagenes/s3rv.png";
- sem4 = "imagenes/s4rr.png";
- }
- try {
- mapa = ImageIO.read(getClass().getClassLoader().getResource("imagenes/fondo.png"));
- s1 = ImageIO.read(getClass().getClassLoader().getResource(sem1));
- s2 = ImageIO.read(getClass().getClassLoader().getResource(sem2));
- s3 = ImageIO.read(getClass().getClassLoader().getResource(sem3));
- s4 = ImageIO.read(getClass().getClassLoader().getResource(sem4));
- clear = ImageIO.read(getClass().getClassLoader().getResource("imagenes/clear.png"));
- } catch(Exception e) {
- System.out.println("no se pudo cargar las imagen");
- }
- }
- public void paint(Graphics gr){
- gr.drawImage(s1, 520, 30, ventana);
- gr.drawImage(s2, 630, 290, ventana);
- gr.drawImage(s3, 630, 175, ventana);
- gr.drawImage(s4, 300, 85, ventana);
- }
- public BufferedImage getClear() {
- return clear;
- }
- public JFrame getVentana() {
- return ventana;
- }
- }
- //TERMINA LA CLASE PANEL
- //INICIA LA CLASE DATOS
- package semaforo;
- import java.awt.Container;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Datos {
- private JFrame ventana;
- private JPanel panel;
- public Datos() {
- ventana = new JFrame("Semaforo");
- ventana.setSize(950, 650);
- ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container contenido = ventana.getContentPane();
- panel = new JPanel();
- panel.setSize(950, 650);
- contenido.add(panel);
- ventana.setVisible(true);
- }
- public synchronized void pausa() {
- try {
- wait();
- } catch(Exception e) {
- System.out.println("Error");
- }
- }
- public synchronized void go() {
- notify();
- }
- public Graphics getGraphics() {
- return panel.getGraphics();
- }
- public JFrame getVentana() {
- return ventana;
- }
- }
- //TERMINA LA CLASE DATOS
Advertisement
Add Comment
Please, Sign In to add comment