Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. package kch.pb.ztp.iterator;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.util.Iterator;
  7.  
  8. import javax.swing.JPanel;
  9.  
  10. // pojedynczy kafelek
  11. class Tile {
  12.  
  13. // schowek na wartość logiczną
  14. private boolean value = false;
  15. // kolory
  16. private static final Color on = new Color(0xffd700), off = new Color(0x1e90ff);
  17.  
  18. // odczyt koloru
  19. public Color getColor() {
  20. return value ? on : off;
  21. }
  22.  
  23. // zmiana koloru
  24. public void flip() {
  25. value = !value;
  26. }
  27. }
  28.  
  29. // macierz kafelków
  30. class Kafelki extends JPanel {
  31.  
  32. private Tile[][] matrix;
  33. private int tilesize;
  34. // kafelek podświetlony (myszką)
  35. private int hx = -1, hy = -1;
  36.  
  37. // inicjalizacja macierzy
  38. public Kafelki(int cols, int rows, int tilesize) {
  39. this.setPreferredSize(new Dimension(cols * tilesize, rows * tilesize));
  40. this.tilesize = tilesize;
  41. matrix = new Tile[rows][cols];
  42. for (int i = 0; i < matrix.length; ++i) {
  43. for (int j = 0; j < matrix[i].length; ++j) {
  44. matrix[i][j] = new Tile();
  45. }
  46. }
  47. }
  48.  
  49. // rysowanie macierzy (oraz jednego podświetlonego)
  50. @Override
  51. public void paintComponent(Graphics g) {
  52. super.paintComponent(g);
  53. for (int i = 0; i < matrix.length; ++i) {
  54. for (int j = 0; j < matrix[i].length; ++j) {
  55. if (i == hy && j == hx) {
  56. g.setColor(matrix[i][j].getColor().brighter());
  57. } else {
  58. g.setColor(matrix[i][j].getColor());
  59. }
  60. g.fillRect(j * tilesize, i * tilesize + 1, tilesize - 1, tilesize - 1);
  61. }
  62. }
  63. }
  64.  
  65. // podświetl
  66. public void highlight(int x, int y) {
  67. hx = x;
  68. hy = y;
  69. repaint();
  70. }
  71.  
  72. public Tile[][] getMatrix() {
  73. return matrix;
  74. }
  75.  
  76. public void setMatrix(Tile[][] matrix) {
  77. this.matrix = matrix;
  78. }
  79.  
  80. // za to pojawi się metoda pobierająca iterator
  81. // public Iterator<Tile> iterator( ...
  82.  
  83. public Iterator<Tile> iterator() {
  84. return new Iterator<Tile>() {
  85.  
  86. private int posX = 0;
  87. private int posY = 0;
  88.  
  89. // Metoda sprawdza czy macierz posiada kolejny element.
  90. @Override
  91. public boolean hasNext() {
  92. int backupPosX = posX;
  93. int backupPosY = posY;
  94.  
  95. boolean nextExists = false;
  96.  
  97. if (posX < matrix.length && posY < matrix[0].length) {
  98. posY++;
  99. nextExists = true;
  100. } else {
  101. posY = 0;
  102. posX++;
  103. if (posX < matrix.length) {
  104. posY++;
  105. nextExists = true;
  106. }
  107. }
  108.  
  109. posX = backupPosX;
  110. posY = backupPosY;
  111.  
  112. return nextExists;
  113. }
  114.  
  115. // Metoda zwrca kolejny element w macierzy.
  116. @Override
  117. public Tile next() {
  118. if (posX < matrix.length && posY < matrix[0].length) {
  119. return matrix[posX][posY++];
  120. } else {
  121.  
  122. posY = 0;
  123. posX++;
  124. if (posX < matrix.length) {
  125. return matrix[posX][posY++];
  126. }
  127. }
  128.  
  129. return null;
  130. }
  131. };
  132. }
  133.  
  134. public Iterator<Tile> iteratorVertical() {
  135. return new Iterator<Tile>() {
  136.  
  137. private int posX = 0;
  138. private int posY = 0;
  139.  
  140. // Metoda sprawdza czy macierz posiada kolejny element.
  141. @Override
  142. public boolean hasNext() {
  143. int backupPosX = posX;
  144. int backupPosY = posY;
  145.  
  146. boolean nextExists = false;
  147.  
  148. if (posX < matrix.length && posY < matrix[0].length) {
  149. posX++;
  150. nextExists = true;
  151. } else {
  152. posX = 0;
  153. posY++;
  154. if (posY < matrix[0].length) {
  155. posX++;
  156. nextExists = true;
  157. }
  158. }
  159.  
  160. posX = backupPosX;
  161. posY = backupPosY;
  162.  
  163. return nextExists;
  164. }
  165.  
  166. @Override
  167. public Tile next() {
  168.  
  169. if (posX < matrix.length && posY < matrix[0].length) {
  170. return matrix[posX++][posY];
  171. } else {
  172. posX = 0;
  173. posY++;
  174. if (posY < matrix[0].length) {
  175. return matrix[posX++][posY];
  176. }
  177. }
  178.  
  179. return null;
  180. }
  181.  
  182. };
  183. }
  184.  
  185. public Iterator<Tile> iteratorObliquely(int row, int col) {
  186. return new Iterator<Tile>() {
  187.  
  188. private int posX = row;
  189. private int posY = 0;
  190. private int count = 0;
  191. private int count2 = 0;
  192.  
  193. // Metoda sprawdza czy macierz posiada kolejny element.
  194. @Override
  195. public boolean hasNext() {
  196. int backupPosX = posX;
  197. int backupPosY = posY;
  198.  
  199. boolean nextExists = false;
  200.  
  201. if (posX < matrix.length && posY < matrix[0].length) {
  202. posX++;
  203. posY++;
  204. nextExists = true;
  205. } else {
  206. count++;
  207. if (count < matrix[0].length) {
  208. posY = 0;
  209. posX = 0;
  210. posY = count;
  211. if (posX < matrix.length && posY < matrix[0].length) {
  212. posX++;
  213. posY++;
  214. nextExists = true;
  215. }
  216. }
  217. }
  218. posX = backupPosX;
  219. posY = backupPosY;
  220.  
  221. return nextExists;
  222. }
  223.  
  224. @Override
  225. public Tile next() {
  226. if (posX < matrix.length && posY < matrix[0].length) {
  227. return matrix[posX++][posY++];
  228. } else {
  229. count2++;
  230. if (count2 < matrix[0].length) {
  231. posY = 0;
  232. posX = 0;
  233. posY = count2;
  234. if (posX < matrix.length && posY < matrix[0].length) {
  235. return matrix[posX++][posY++];
  236. }
  237. }
  238. }
  239. return null;
  240. }
  241. };
  242. }
  243. }
  244.  
  245. // ten wątek nie wykorzystuje iteratora
  246. class Watek implements Runnable {
  247.  
  248. private Kafelki p;
  249. private int x, y;
  250.  
  251. // x, y to początkowa pozycja do iteracji
  252. public Watek(Kafelki k, int x, int y) {
  253. this.p = k;
  254. this.x = x;
  255. this.y = y;
  256. }
  257.  
  258. public Tile getTileUnderMouse() {
  259.  
  260. for (int i = 0; i < p.getMatrix().length; ++i) {
  261. for (int j = 0; j < p.getMatrix()[i].length; ++j) {
  262. if (i == y && j == x)
  263. return p.getMatrix()[i][j];
  264. }
  265. }
  266. return null;
  267. }
  268.  
  269. public void run() {
  270. // Iterator<Tile> it = p.iterator();
  271. // Iterator<Tile> it = p.iteratorVertical();
  272. Iterator<Tile> it = p.iteratorObliquely(x, y);
  273.  
  274. Tile tileUnderMouse = getTileUnderMouse();
  275. boolean fall = false;
  276. while (it.hasNext()) {
  277.  
  278. // Iterate until we get tile under mouse
  279. Tile tile = it.next();
  280. if (tile == tileUnderMouse) {
  281. fall = true;
  282. }
  283.  
  284. // Then flip tiles
  285. if (fall == true) {
  286. tile.flip();
  287. p.repaint();
  288. try {
  289. Thread.currentThread().sleep(100);
  290. } catch (InterruptedException e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. }
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement