Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11.  
  12. import javax.imageio.ImageIO;
  13. import javax.swing.JFrame;
  14.  
  15.  
  16. public class Menu extends JFrame implements Runnable {
  17. private BufferedImage tlo;
  18. private BufferedImage tlo1;
  19. private BufferedImage tlo2;
  20. private BufferedImage tlo3;
  21. private BufferedImage tlo4;
  22. private int swiatla;
  23. private ArrayList<Auto> auta = new ArrayList<Auto>();
  24. private ArrayList<Tramwaj> tramwaje = new ArrayList<Tramwaj>();
  25. private ArrayList<Tramwaj> tramwaje2 = new ArrayList<Tramwaj>();
  26. private ArrayList<Miejsce> zajete = new ArrayList<Miejsce>();
  27. protected Graphics bufferGraphics;
  28. protected Image offscreen;
  29. Swiatlo s1;
  30. Swiatlo s2;
  31. Swiatlo s3;
  32. Swiatlo s4;
  33.  
  34.  
  35. public Menu() {
  36.  
  37. KeySpy keySpy = new KeySpy();
  38. this.addKeyListener(keySpy);
  39. try {
  40. tlo1 = ImageIO.read(new File("res/okopowa1.png"));
  41. } catch (IOException e) {
  42. }
  43. try {
  44. tlo2 = ImageIO.read(new File("res/okopowa2.png"));
  45. } catch (IOException e) {
  46. }
  47. try {
  48. tlo3 = ImageIO.read(new File("res/okopowa3.png"));
  49. } catch (IOException e) {
  50. }
  51. try {
  52. tlo4 = ImageIO.read(new File("res/okopowa4.png"));
  53. } catch (IOException e) {
  54. }
  55. swiatla = 1;
  56. s1 = new Swiatlo(this);
  57. s2 = new Swiatlo(this);
  58. s3 = new Swiatlo(this);
  59. s4 = new Swiatlo(this);
  60. s1.swiatloZielone();
  61. s2.swiatloCzerwone();
  62. s3.swiatloCzerwone();
  63. s4.swiatloCzerwone();
  64. this.setTitle("Skrzyzowanie Okopowej i Towarowej");
  65. this.setResizable(false);
  66. this.setSize(1000, 1000);
  67. Auto a0 = new Auto(this, 4, 100);
  68. Auto a1 = new Auto(this, 5, 100);
  69. Auto a2 = new Auto(this, 6, 100);
  70. Auto a3 = new Auto(this, 7, 100);
  71. Auto a4 = new Auto(this, 8, 100);
  72. Auto a5 = new Auto(this, 9, 100);
  73. Auto a6 = new Auto(this, 10, 120);
  74. Auto a7 = new Auto(this, 11, 140);
  75. Auto a8 = new Auto(this, 12, 80);
  76. Auto a9 = new Auto(this, 13, 100);
  77. Auto a10 = new Auto(this, 14, 100);
  78. Auto a11 = new Auto(this, 15, 100);
  79. Tramwaj t0 = new Tramwaj(this, 0, 100, 1);
  80. Tramwaj t1 = new Tramwaj(this, 1, 100, 1);
  81. Tramwaj t2 = new Tramwaj(this, 2, 100, 2);
  82. Tramwaj t3 = new Tramwaj(this, 3, 100, 2);
  83. auta.add(a1);
  84. auta.add(a2);
  85. auta.add(a3);
  86. auta.add(a0);
  87. auta.add(a4);
  88. auta.add(a5);
  89. auta.add(a6);
  90. auta.add(a7);
  91. auta.add(a8);
  92. auta.add(a9);
  93. auta.add(a10);
  94. auta.add(a11);
  95. tramwaje.add(t0);
  96. tramwaje.add(t1);
  97. tramwaje2.add(t2);
  98. tramwaje2.add(t3);
  99. a0.start();
  100. a1.start();
  101. a2.start();
  102. a3.start();
  103. a4.start();
  104. a5.start();
  105. a6.start();
  106. a7.start();
  107. a8.start();
  108. a9.start();
  109. a10.start();
  110. t0.start();
  111. t1.start();
  112. t2.start();
  113. t3.start();
  114. }
  115.  
  116. public void run() {
  117. while (true) {
  118. try {
  119. Thread.sleep(3000);
  120. } catch (InterruptedException e) {
  121. }
  122. swiatla = swiatla + 1;
  123. if(swiatla == 5){
  124. swiatla = 1;
  125. }
  126. repaint();
  127. wybierzWariantSwiatel();
  128. }
  129. }
  130.  
  131. public synchronized int dodajNoweZajeteMiejsce(Miejsce m) {
  132. int s = zajete.size();
  133. if (s < 0) s = 0;
  134. zajete.add(s, m);
  135. return s;
  136. }
  137.  
  138. public synchronized void usunZajeteMiejsce(int dx, int dy) {
  139. int s = -1;
  140. for (int i = 0; i < zajete.size(); i++)
  141. if (zajete.get(i).getX() == dx && zajete.get(i).getY() == dy)
  142. s = i;
  143. if (s >= 0)
  144. zajete.remove(s);
  145. }
  146.  
  147. public synchronized boolean sprawdzCzyMiejsceJestZajete(int dx, int dy) {
  148. for (Miejsce m : zajete)
  149. if (m.getX() == dx && m.getY() == dy)
  150. return true;
  151. return false;
  152. }
  153.  
  154. class KeySpy implements KeyListener {
  155. public void keyPressed(KeyEvent arg0) {
  156. }
  157.  
  158. public void keyReleased(KeyEvent arg0) {
  159. switch (arg0.getKeyChar()) {
  160. case ' ':
  161. swiatla = swiatla + 1;
  162. if(swiatla == 5){
  163. swiatla = 1;
  164. }
  165. break;
  166. }
  167. wybierzWariantSwiatel();
  168. }
  169.  
  170. public void keyTyped(KeyEvent arg0) {
  171. }
  172. }
  173.  
  174. public Swiatlo getLight(int tory) {
  175. switch (tory) {
  176. case 0:
  177. return s2;
  178. case 1:
  179. return s2;
  180. case 2:
  181. return s1;
  182. case 3:
  183. return s1;
  184. case 4:
  185. return s2;
  186. case 5:
  187. return s3;
  188. case 6:
  189. return s2;
  190. case 7:
  191. return s3;
  192. case 8:
  193. return s1;
  194. case 9:
  195. return s4;
  196. case 10:
  197. return s1;
  198. case 11:
  199. return s4;
  200. default:
  201. return s1;
  202. }
  203. }
  204.  
  205. public void wybierzWariantSwiatel() {
  206. switch (swiatla) {
  207. case 1:
  208. zmienSwiatla(1);
  209. break;
  210. case 2:
  211. zmienSwiatla(2);
  212. break;
  213. case 3:
  214. zmienSwiatla(3);
  215. break;
  216. case 4:
  217. zmienSwiatla(4);
  218. break;
  219. }
  220. }
  221.  
  222. public void zmienSwiatla(int opcjaSwiatel) {
  223. if (opcjaSwiatel == 1) {
  224. s1.swiatloZielone();
  225. s2.swiatloCzerwone();
  226. s3.swiatloCzerwone();
  227. s4.swiatloCzerwone();
  228. } else if (opcjaSwiatel == 2) {
  229. s1.swiatloCzerwone();
  230. s2.swiatloZielone();
  231. s3.swiatloCzerwone();
  232. s4.swiatloCzerwone();
  233. } else if (opcjaSwiatel == 3) {
  234. s1.swiatloCzerwone();
  235. s2.swiatloCzerwone();
  236. s3.swiatloZielone();
  237. s4.swiatloCzerwone();
  238. }
  239. else if (opcjaSwiatel == 4) {
  240. s1.swiatloCzerwone();
  241. s2.swiatloCzerwone();
  242. s3.swiatloCzerwone();
  243. s4.swiatloZielone();
  244. }
  245. }
  246.  
  247. public void paint(Graphics g) {
  248. if (bufferGraphics == null) {
  249. offscreen = createImage(getWidth(), getHeight());
  250. bufferGraphics = offscreen.getGraphics();
  251. }
  252. paintComponent(bufferGraphics);
  253. g.drawImage(offscreen, 0, 0, null);
  254. }
  255.  
  256. public void update(Graphics g) {
  257. paint(g);
  258. }
  259.  
  260. public void paintComponent(Graphics g) {
  261. {
  262. switch (swiatla) {
  263. case 1:
  264. tlo = tlo1;
  265. break;
  266. case 2:
  267. tlo = tlo2;
  268. break;
  269. case 3:
  270. tlo = tlo3;
  271. break;
  272. case 4:
  273. tlo = tlo4;
  274. break;
  275. }
  276. g.drawImage(tlo, 0, 0, 1000, 1000, this);
  277. Graphics2D g2 = (Graphics2D) g;
  278. for (Auto a : auta)
  279. g2.drawImage(a.getImage(), a.getX(), a.getY(), 25, 25, this);
  280. for (Tramwaj t : tramwaje)
  281. g2.drawImage(t.getImage(), t.getX(), t.getY(), 25, 50, this);
  282. for (Tramwaj t : tramwaje2)
  283. g2.drawImage(t.getImage(), t.getX(), t.getY(), 50, 25, this);
  284. g2.setColor(Color.BLACK);
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement