Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import java.util.Iterator;
  7. import javax.swing.JFrame;
  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),
  17. off = new Color(0x1e90ff);
  18.  
  19. // odczyt koloru
  20. public Color getColor() {
  21. return value ? on : off;
  22. }
  23.  
  24. //zmiana koloru
  25. public void flip() {
  26. value = !value;
  27. }
  28. }
  29.  
  30. //macierz kafelków
  31. class Kafelki extends JPanel {
  32.  
  33. private Tile[][] matrix;
  34. private int tilesize;
  35. // kafelek podświetlony (myszką)
  36. private int hx = -1, hy = -1;
  37.  
  38. public tileIterator iteratorek;
  39.  
  40. public class tileIterator implements Iterator {
  41. public int x_index;
  42. public int y_index;
  43. @Override
  44. public boolean hasNext() {
  45. if(x_index+1 < getCols() || y_index+1 < getRows())
  46. return true;
  47. else
  48. return false; }
  49.  
  50. @Override
  51. public Tile next() {
  52. if (this.hasNext()){
  53. if (x_index < getCols()) {
  54. x_index++;
  55. return matrix[y_index][x_index]; }
  56. else if (x_index == matrix.length) {
  57. if (y_index < getRows()) {
  58. x_index = 0;
  59. y_index++;
  60. return matrix[y_index][x_index]; }
  61. }
  62. }
  63. return null;
  64. }
  65.  
  66. public tileIterator (int x, int y) {
  67. x_index = x;
  68. y_index = y;
  69. }
  70. }
  71.  
  72. public void setIterator (int x, int y) {
  73. if (this.iteratorek == null)
  74. this.iteratorek = new tileIterator(x,y);
  75. else {
  76. this.iteratorek.x_index = x;
  77. this.iteratorek.y_index = y;
  78. }
  79. }
  80.  
  81. // inicjalizacja macierzy
  82. public Kafelki(int cols, int rows, int tilesize) {
  83. this.setPreferredSize(new Dimension(cols * tilesize, rows * tilesize));
  84. this.tilesize = tilesize;
  85. matrix = new Tile[rows][cols];
  86. for (int i = 0; i < matrix.length; ++i) {
  87. for (int j = 0; j < matrix[i].length; ++j) {
  88. matrix[i][j] = new Tile();
  89. }
  90. }
  91. }
  92.  
  93. // rysowanie macierzy (oraz jednego podświetlonego)
  94. @Override
  95. public void paintComponent(Graphics g) {
  96. super.paintComponent(g);
  97. for (int i = 0; i < matrix.length; ++i) {
  98. for (int j = 0; j < matrix[i].length; ++j) {
  99. if (i == hy && j == hx) {
  100. g.setColor(matrix[i][j].getColor().brighter());
  101. } else {
  102. g.setColor(matrix[i][j].getColor());
  103. }
  104. g.fillRect(j * tilesize, i * tilesize + 1, tilesize - 1, tilesize - 1);
  105. }
  106. }
  107. }
  108.  
  109. // podświetl
  110. public void highlight(int x, int y) {
  111. hx = x;
  112. hy = y;
  113. repaint();
  114. }
  115.  
  116. // trzy poniższe metody znikną w finalnej wersji
  117. public int getRows() {
  118. return matrix.length;
  119. }
  120.  
  121. public int getCols() {
  122. return matrix[0].length;
  123. }
  124.  
  125. public Tile getAt(int row, int col) {
  126. return matrix[row][col];
  127. }
  128.  
  129. // za to pojawi się metoda pobierająca iterator
  130. public Iterator<Tile> iterator() {
  131. return iteratorek;
  132. }
  133.  
  134. }
  135.  
  136. // ten wątek nie wykorzystuje iteratora
  137. class Watek implements Runnable {
  138.  
  139. private Kafelki p;
  140. private int x, y;
  141.  
  142. // x, y to początkowa pozycja do iteracji
  143. public Watek(Kafelki k, int x, int y) {
  144. this.p = k;
  145. this.x = x;
  146. this.y = y;
  147. }
  148.  
  149. public void run() {
  150. // klasyczna podwójna pętla do iteracji
  151. // tutaj kontrolujemy kolejność odwiedzin
  152. // zostanie to zastąpione pętlą z użyciem iteratora
  153.  
  154. p.setIterator(x,y);
  155.  
  156. while (p.iterator().hasNext()) {
  157. System.out.println(x+" "+y);
  158. p.setIterator(x,y);
  159. p.iterator().next().flip();
  160. p.repaint();
  161. try {
  162. Thread.currentThread().sleep(100);
  163. } catch (InterruptedException e) {
  164. e.printStackTrace();
  165. }
  166. if (x+1<p.getCols())
  167. x++;
  168. else if (x==p.getCols() && y<p.getRows()) {
  169. x=0;
  170. y++;
  171. }
  172.  
  173. }
  174.  
  175. /*for (int i = y; i < p.getRows(); ++i) {
  176. int j;
  177. if (i == y) {
  178. j = x;
  179. } else {
  180. j = 0;
  181. }
  182. for (; j < p.getCols(); ++j) {
  183. // a w środku - obracamy, odświeżamy i czekamy
  184. p.getAt(i, j).flip();
  185. p.repaint();
  186. try {
  187. Thread.currentThread().sleep(100);
  188. } catch (InterruptedException e) {
  189. e.printStackTrace();
  190. }
  191. }
  192. }*/
  193. }
  194. }
  195.  
  196. public class Ztp08 {
  197.  
  198. static final int TILESIZE = 40;
  199.  
  200. public static void main(String[] args) {
  201.  
  202. // konstruowanie okna
  203. JFrame frame = new JFrame("Iterator");
  204. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  205. final Kafelki kafelki = new Kafelki(16, 9, TILESIZE);
  206. frame.getContentPane().add(kafelki);
  207. frame.pack();
  208. frame.setVisible(true);
  209.  
  210. // reakcja na kliknięcie uruchomienie wątku z iteracją
  211. kafelki.addMouseListener(new MouseAdapter() {
  212.  
  213. @Override
  214. public void mouseClicked(MouseEvent e) {
  215. int x = e.getX() / TILESIZE;
  216. int y = e.getY() / TILESIZE;
  217. new Thread(new Watek(kafelki, x, y)).start();
  218. }
  219. });
  220. // reakcja na ruch - podświetlenie wskazanego kafelka
  221. kafelki.addMouseMotionListener(new MouseAdapter() {
  222.  
  223. @Override
  224. public void mouseMoved(MouseEvent e) {
  225. int x = e.getX() / TILESIZE;
  226. int y = e.getY() / TILESIZE;
  227. kafelki.highlight(x, y);
  228. }
  229. });
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement