Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4.  
  5. import javax.imageio.ImageIO;
  6.  
  7.  
  8. public class Tramwaj extends Thread {
  9. private int prTramwaju;
  10. private int x;
  11. private int y;
  12. private Menu menu;
  13. private int tor;
  14. private BufferedImage image;
  15.  
  16. public Tramwaj(Menu menu, int tor, int prTramwaju, int wersjaTramwaju) {
  17. if (wersjaTramwaju == 1) {
  18. try {
  19. image = ImageIO.read(new File("res/t.png"));
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. } else if (wersjaTramwaju == 2) {
  24. try {
  25. image = ImageIO.read(new File("res/t2.png"));
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. this.menu = menu;
  31. this.tor = tor;
  32. this.prTramwaju = prTramwaju;
  33. x = y = 0;
  34. }
  35.  
  36.  
  37. public BufferedImage getImage() {
  38. return image;
  39. }
  40.  
  41. public int getX() {
  42. return x;
  43. }
  44.  
  45. public int getY() {
  46. return y;
  47. }
  48.  
  49. public void run() {
  50. for (; ; ) {
  51. switch (tor) {
  52. case 0: {
  53. x = 418;
  54. y = 0;
  55. while (y < 250) {
  56. trasaTramwaju2();
  57. }
  58. menu.getLight(tor).jedz();
  59. while (y < 1000) {
  60. trasaTramwaju2();
  61. }
  62. try {
  63. sleep(1000);
  64. } catch (InterruptedException e1) {
  65. System.out.println();
  66. }
  67. menu.usunZajeteMiejsce(x, y - 25);
  68. menu.usunZajeteMiejsce(x, y - 50);
  69. }
  70. break;
  71. case 1: {
  72. x = 490;
  73. y = 1000;
  74. while (y > 680) {
  75. trasaTramwaju1();
  76. }
  77. menu.getLight(tor).jedz();
  78. while (y > -50) {
  79. trasaTramwaju1();
  80. }
  81. try {
  82. sleep(500);
  83. } catch (InterruptedException e1) {
  84. System.out.println();
  85. }
  86. menu.usunZajeteMiejsce(x, y + 25);
  87. menu.usunZajeteMiejsce(x, y + 50);
  88. }
  89. break;
  90. case 2: {
  91. x = 0;
  92. y = 518;
  93. while (x < 250) {
  94. trasaTramwaju3();
  95. }
  96. menu.getLight(tor).jedz();
  97. while (x < 1000) {
  98. trasaTramwaju3();
  99. }
  100. try {
  101. sleep(1000);
  102. } catch (InterruptedException e1) {
  103. System.out.println();
  104. }
  105. menu.usunZajeteMiejsce(x - 25, y);
  106. menu.usunZajeteMiejsce(x - 50, y);
  107. }
  108. break;
  109. case 3: {
  110. x = 1000;
  111. y = 438;
  112. while (x > 680) {
  113. trasaTramwaju4();
  114. }
  115. menu.getLight(tor).jedz();
  116. while (x > -50) {
  117. trasaTramwaju4();
  118. }
  119. try {
  120. sleep(500);
  121. } catch (InterruptedException e1) {
  122. System.out.println();
  123. }
  124. menu.usunZajeteMiejsce(x + 25, y);
  125. menu.usunZajeteMiejsce(x + 50, y);
  126. }
  127. break;
  128. }
  129. }
  130. }
  131.  
  132. private void trasaTramwaju1() {
  133. if (!menu.sprawdzCzyMiejsceJestZajete(x, y)) {
  134. menu.usunZajeteMiejsce(x, y + 25);
  135. menu.usunZajeteMiejsce(x, y + 50);
  136. menu.dodajNoweZajeteMiejsce(new Miejsce(x, y));
  137. menu.dodajNoweZajeteMiejsce(new Miejsce(x, y + 25));
  138. try {
  139. sleep(prTramwaju);
  140. } catch (InterruptedException exception) {;
  141. System.out.println();
  142. }
  143. menu.repaint();
  144. y = y - 25;
  145. }
  146. }
  147.  
  148. private void trasaTramwaju2() {
  149. if (!menu.sprawdzCzyMiejsceJestZajete(x, y)) {
  150. menu.usunZajeteMiejsce(x, y - 25);
  151. menu.usunZajeteMiejsce(x, y - 50);
  152. menu.dodajNoweZajeteMiejsce(
  153. new Miejsce(x, y));
  154. menu.dodajNoweZajeteMiejsce(new Miejsce(x, y - 25));
  155. try {
  156. sleep(prTramwaju);
  157. } catch (InterruptedException e) {
  158. System.out.println();
  159. }
  160. menu.repaint();
  161. y = y + 25;
  162. }
  163.  
  164. }
  165.  
  166. private void trasaTramwaju3() {
  167. if (!menu.sprawdzCzyMiejsceJestZajete(x, y)) {
  168. menu.usunZajeteMiejsce(x - 25, y);
  169. menu.usunZajeteMiejsce(x - 50, y);
  170. menu.dodajNoweZajeteMiejsce(new Miejsce(x, y));
  171. menu.dodajNoweZajeteMiejsce(new Miejsce(x - 25, y));
  172. ;
  173. try {
  174. sleep(prTramwaju);
  175. } catch (InterruptedException e) {
  176. System.out.println();
  177. }
  178. menu.repaint();
  179. x = x + 25;
  180. }
  181. }
  182.  
  183. private void trasaTramwaju4() {
  184. if (!menu.sprawdzCzyMiejsceJestZajete(x, y)) {;
  185. menu.usunZajeteMiejsce(x + 25, y);
  186. menu.usunZajeteMiejsce(x + 50, y);
  187. menu.dodajNoweZajeteMiejsce(new Miejsce(x, y));
  188. menu.dodajNoweZajeteMiejsce(new Miejsce(x + 25, y));
  189. try {
  190. sleep(prTramwaju);
  191. } catch (InterruptedException e) {
  192. System.out.println();
  193. }
  194. menu.repaint();
  195. x = x - 25;
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement