turupache

Código

Sep 5th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. //INICIA LA CLASE TRAFICO
  2.  
  3. package semaforo;
  4.  
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.awt.image.BufferedImage;
  8. import java.util.Random;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. public class Trafico extends Thread {
  13.  
  14. private int cola[] = {0, 0, 0, 0, 0, 0};
  15. private int posX[] = {620, 620, 330, 330, 530, 580};
  16. private int posY[] = {90, 130, 180, 220, 300, 300};
  17. private Graphics gr;
  18. private Color COLORS[] = {Color.green, Color.red, Color.blue};
  19. private Random r;
  20. private Datos data;
  21. private BufferedImage clr;
  22. private JFrame ventana;
  23.  
  24. public Trafico(Datos data, BufferedImage cl) {
  25. this.gr = data.getGraphics();
  26. this.data = data;
  27. this.clr = cl;
  28. this.ventana = data.getVentana();
  29. r = new Random();
  30. }
  31.  
  32. public void run() {
  33. try {
  34. int t, tiempo = 1000;
  35. while (true) {
  36. for(int sem = 0; sem < 4; sem++) {
  37. data.pausa();
  38. System.out.println(" - - Semaforo " + Integer.toString(sem + 1) );
  39. t = optimizar(sem);
  40. for(int i = 0; i < t; i++) {
  41. autos();
  42. avanza(sem);
  43. debug();
  44. sleep(tiempo);
  45. }
  46. data.go();
  47. }
  48. }
  49. } catch(Exception e){
  50. System.out.println("-- ERROR --");
  51. }
  52. }
  53.  
  54. private int optimizar(int v) {
  55. int a;
  56. if (v == 0) {
  57. a = Math.max(cola[0] / 2, Math.max(cola[1], cola[5]) );
  58. }
  59. else if (v == 1) {
  60. a = Math.max(cola[0], cola[2] / 3 );
  61. }
  62. else if (v == 2) {
  63. a = Math.max(cola[2], cola[3] / 3 );
  64. }
  65. else {
  66. a = Math.max(cola[4], Math.max(cola[5], cola[3]) );
  67. }
  68. a = Math.max(a+3, 6);
  69. if(a > 15){
  70. a = 18;
  71. }
  72. return a;
  73. }
  74.  
  75. private void debug() {
  76. for (int i= 0; i < 6; i++) {
  77. System.out.print(cola[i] + " ");
  78. }
  79. System.out.println();
  80. }
  81.  
  82. private void avanza(int v) {
  83. if (v == 0) {
  84. cola[0] = salir(cola[0], 0);
  85. cola[1] = salir(cola[1], 1);
  86. cola[5] = salir(cola[5], 5);
  87. }
  88. else if (v == 1) {
  89. cola[0] = salir(cola[0], 0);
  90. cola[2] = salir(cola[2], 2);
  91. }
  92. else if(v == 2) {
  93. cola[2] = salir(cola[2], 2);
  94. cola[3] = salir(cola[3], 3);
  95. }
  96. else {
  97. cola[3] = salir(cola[3], 3);
  98. cola[4] = salir(cola[4], 4);
  99. cola[5] = salir(cola[5], 5);
  100. }
  101. }
  102.  
  103. private int salir(int valor, int sverde) {
  104. if(valor > 0) {
  105. valor -= 1;
  106. clear(sverde);
  107. }
  108. return valor;
  109. }
  110.  
  111. private void clear(int c) {
  112. int a = 0, b = 0;
  113. if (c/2 == 0) {
  114. a = 20;
  115. }
  116. else if (c/2 == 1) {
  117. a = -20;
  118. }
  119. else {
  120. b = 20;
  121. }
  122. posX[c] -= a;
  123. posY[c] -= b;
  124. gr.drawImage(clr, posX[c], posY[c], ventana);
  125. }
  126.  
  127. private void autos() {
  128. int cantidad, i;
  129. for (int j = 0; j < 2; j++) {
  130. cantidad = r.nextInt(1) + 1;
  131. i = r.nextInt(6);
  132. cola[i] += cantidad;
  133. paint(cantidad, i);
  134. }
  135. }
  136.  
  137. private void paint(int n, int c) {
  138. int a = 0, b = 0;
  139. gr.setColor(COLORS[c/2]);
  140. if (c/2 == 0) {
  141. a = 20;
  142. }
  143. else if (c/2 == 1) {
  144. a = -20;
  145. }
  146. else {
  147. b = 20;
  148. }
  149. for(int i = 0; i< n; i++) {
  150. gr.fillRect(posX[c], posY[c], 15, 15);
  151. posX[c] += a;
  152. posY[c] += b;
  153. }
  154. }
  155.  
  156. }
  157.  
  158. //TERMINA LA CLASE TRAFICO
  159.  
  160.  
  161. //INICIA LA CLASE MAIN
  162.  
  163. package semaforo;
  164.  
  165. public class Main{
  166.  
  167. public static void main(String[] args) {
  168. Datos data = new Datos();
  169. Panel ambiente = new Panel(data);
  170. Trafico autos = new Trafico(data, ambiente.getClear() );
  171. ambiente.start();
  172. autos.start();
  173. }
  174. }
  175.  
  176. //TERMINA LA CLASE MAIN
  177.  
  178. //INICIA LA CLASE PANEL
  179.  
  180. package semaforo;
  181.  
  182. import java.awt.*;
  183.  
  184. import javax.swing.*;
  185. import java.awt.image.BufferedImage;
  186. import javax.imageio.ImageIO;
  187.  
  188.  
  189. public class Panel extends Thread {
  190.  
  191. private JFrame ventana;
  192. private String sem1, sem2, sem3, sem4;
  193. private BufferedImage mapa, s1, s2, s3, s4, clear;
  194. int tiempoespera;
  195. private Datos data;
  196.  
  197. public Panel(Datos data){
  198. this.data = data;
  199.  
  200. cargarimagen(1);
  201. data.getGraphics().drawImage(mapa,0,0, ventana);
  202.  
  203. }
  204.  
  205. public void run(){
  206. try {
  207. System.out.println("arranca hilo");
  208. while(true) {
  209. for(int i=1; i<=4; i++) {
  210. cargarimagen(i);
  211. paint(data.getGraphics());
  212. System.out.println("semaforo " + i);
  213. data.go();
  214. data.pausa();
  215. }
  216. }
  217.  
  218. } catch(Exception e) {
  219. e.printStackTrace();
  220. }
  221. }
  222.  
  223. public void cargarimagen(int secuencia){
  224. int valor = secuencia;
  225. if(valor == 1){
  226. sem1 = "imagenes/s1rv.png"; // arriba
  227. sem2 = "imagenes/s2r.png"; // peaton
  228. sem3 = "imagenes/s3rr.png"; // derecha
  229. sem4 = "imagenes/s4vv.png"; // izq
  230. }
  231. else if(valor == 2){
  232. sem1 = "imagenes/s1rr.png";
  233. sem2 = "imagenes/s2v.png";
  234. sem3 = "imagenes/s3vr.png";
  235. sem4 = "imagenes/s4vr.png";
  236. }
  237. else if(valor == 3){
  238. sem1 = "imagenes/s1rr.png";
  239. sem2 = "imagenes/s2r.png";
  240. sem3 = "imagenes/s3vv.png";
  241. sem4 = "imagenes/s4vr.png";
  242. }
  243. else {
  244. sem1 = "imagenes/s1vv.png";
  245. sem2 = "imagenes/s2r.png";
  246. sem3 = "imagenes/s3rv.png";
  247. sem4 = "imagenes/s4rr.png";
  248. }
  249. try {
  250. mapa = ImageIO.read(getClass().getClassLoader().getResource("imagenes/fondo.png"));
  251. s1 = ImageIO.read(getClass().getClassLoader().getResource(sem1));
  252. s2 = ImageIO.read(getClass().getClassLoader().getResource(sem2));
  253. s3 = ImageIO.read(getClass().getClassLoader().getResource(sem3));
  254. s4 = ImageIO.read(getClass().getClassLoader().getResource(sem4));
  255. clear = ImageIO.read(getClass().getClassLoader().getResource("imagenes/clear.png"));
  256. } catch(Exception e) {
  257. System.out.println("no se pudo cargar las imagen");
  258. }
  259. }
  260.  
  261. public void paint(Graphics gr){
  262. gr.drawImage(s1, 520, 30, ventana);
  263. gr.drawImage(s2, 630, 290, ventana);
  264. gr.drawImage(s3, 630, 175, ventana);
  265. gr.drawImage(s4, 300, 85, ventana);
  266. }
  267.  
  268. public BufferedImage getClear() {
  269. return clear;
  270. }
  271.  
  272. public JFrame getVentana() {
  273. return ventana;
  274. }
  275.  
  276. }
  277.  
  278. //TERMINA LA CLASE PANEL
  279.  
  280. //INICIA LA CLASE DATOS
  281.  
  282. package semaforo;
  283.  
  284. import java.awt.Container;
  285. import java.awt.Graphics;
  286.  
  287. import javax.swing.JFrame;
  288. import javax.swing.JPanel;
  289.  
  290.  
  291. public class Datos {
  292.  
  293. private JFrame ventana;
  294. private JPanel panel;
  295.  
  296. public Datos() {
  297. ventana = new JFrame("Semaforo");
  298. ventana.setSize(950, 650);
  299. ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  300. Container contenido = ventana.getContentPane();
  301.  
  302. panel = new JPanel();
  303. panel.setSize(950, 650);
  304.  
  305. contenido.add(panel);
  306. ventana.setVisible(true);
  307. }
  308.  
  309. public synchronized void pausa() {
  310. try {
  311. wait();
  312. } catch(Exception e) {
  313. System.out.println("Error");
  314. }
  315. }
  316.  
  317. public synchronized void go() {
  318. notify();
  319. }
  320.  
  321. public Graphics getGraphics() {
  322. return panel.getGraphics();
  323. }
  324.  
  325. public JFrame getVentana() {
  326. return ventana;
  327. }
  328.  
  329. }
  330.  
  331. //TERMINA LA CLASE DATOS
Advertisement
Add Comment
Please, Sign In to add comment