Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kch.pb.ztp.iterator;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.util.Iterator;
- import javax.swing.JPanel;
- // pojedynczy kafelek
- class Tile {
- // schowek na wartość logiczną
- private boolean value = false;
- // kolory
- private static final Color on = new Color(0xffd700), off = new Color(0x1e90ff);
- // odczyt koloru
- public Color getColor() {
- return value ? on : off;
- }
- // zmiana koloru
- public void flip() {
- value = !value;
- }
- }
- // macierz kafelków
- class Kafelki extends JPanel {
- private Tile[][] matrix;
- private int tilesize;
- // kafelek podświetlony (myszką)
- private int hx = -1, hy = -1;
- // inicjalizacja macierzy
- public Kafelki(int cols, int rows, int tilesize) {
- this.setPreferredSize(new Dimension(cols * tilesize, rows * tilesize));
- this.tilesize = tilesize;
- matrix = new Tile[rows][cols];
- for (int i = 0; i < matrix.length; ++i) {
- for (int j = 0; j < matrix[i].length; ++j) {
- matrix[i][j] = new Tile();
- }
- }
- }
- // rysowanie macierzy (oraz jednego podświetlonego)
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- for (int i = 0; i < matrix.length; ++i) {
- for (int j = 0; j < matrix[i].length; ++j) {
- if (i == hy && j == hx) {
- g.setColor(matrix[i][j].getColor().brighter());
- } else {
- g.setColor(matrix[i][j].getColor());
- }
- g.fillRect(j * tilesize, i * tilesize + 1, tilesize - 1, tilesize - 1);
- }
- }
- }
- // podświetl
- public void highlight(int x, int y) {
- hx = x;
- hy = y;
- repaint();
- }
- public Tile[][] getMatrix() {
- return matrix;
- }
- public void setMatrix(Tile[][] matrix) {
- this.matrix = matrix;
- }
- // za to pojawi się metoda pobierająca iterator
- // public Iterator<Tile> iterator( ...
- public Iterator<Tile> iterator() {
- return new Iterator<Tile>() {
- private int posX = 0;
- private int posY = 0;
- // Metoda sprawdza czy macierz posiada kolejny element.
- @Override
- public boolean hasNext() {
- int backupPosX = posX;
- int backupPosY = posY;
- boolean nextExists = false;
- if (posX < matrix.length && posY < matrix[0].length) {
- posY++;
- nextExists = true;
- } else {
- posY = 0;
- posX++;
- if (posX < matrix.length) {
- posY++;
- nextExists = true;
- }
- }
- posX = backupPosX;
- posY = backupPosY;
- return nextExists;
- }
- // Metoda zwrca kolejny element w macierzy.
- @Override
- public Tile next() {
- if (posX < matrix.length && posY < matrix[0].length) {
- return matrix[posX][posY++];
- } else {
- posY = 0;
- posX++;
- if (posX < matrix.length) {
- return matrix[posX][posY++];
- }
- }
- return null;
- }
- };
- }
- public Iterator<Tile> iteratorVertical() {
- return new Iterator<Tile>() {
- private int posX = 0;
- private int posY = 0;
- // Metoda sprawdza czy macierz posiada kolejny element.
- @Override
- public boolean hasNext() {
- int backupPosX = posX;
- int backupPosY = posY;
- boolean nextExists = false;
- if (posX < matrix.length && posY < matrix[0].length) {
- posX++;
- nextExists = true;
- } else {
- posX = 0;
- posY++;
- if (posY < matrix[0].length) {
- posX++;
- nextExists = true;
- }
- }
- posX = backupPosX;
- posY = backupPosY;
- return nextExists;
- }
- @Override
- public Tile next() {
- if (posX < matrix.length && posY < matrix[0].length) {
- return matrix[posX++][posY];
- } else {
- posX = 0;
- posY++;
- if (posY < matrix[0].length) {
- return matrix[posX++][posY];
- }
- }
- return null;
- }
- };
- }
- public Iterator<Tile> iteratorObliquely(int row, int col) {
- return new Iterator<Tile>() {
- private int posX = row;
- private int posY = 0;
- private int count = 0;
- private int count2 = 0;
- // Metoda sprawdza czy macierz posiada kolejny element.
- @Override
- public boolean hasNext() {
- int backupPosX = posX;
- int backupPosY = posY;
- boolean nextExists = false;
- if (posX < matrix.length && posY < matrix[0].length) {
- posX++;
- posY++;
- nextExists = true;
- } else {
- count++;
- if (count < matrix[0].length) {
- posY = 0;
- posX = 0;
- posY = count;
- if (posX < matrix.length && posY < matrix[0].length) {
- posX++;
- posY++;
- nextExists = true;
- }
- }
- }
- posX = backupPosX;
- posY = backupPosY;
- return nextExists;
- }
- @Override
- public Tile next() {
- if (posX < matrix.length && posY < matrix[0].length) {
- return matrix[posX++][posY++];
- } else {
- count2++;
- if (count2 < matrix[0].length) {
- posY = 0;
- posX = 0;
- posY = count2;
- if (posX < matrix.length && posY < matrix[0].length) {
- return matrix[posX++][posY++];
- }
- }
- }
- return null;
- }
- };
- }
- }
- // ten wątek nie wykorzystuje iteratora
- class Watek implements Runnable {
- private Kafelki p;
- private int x, y;
- // x, y to początkowa pozycja do iteracji
- public Watek(Kafelki k, int x, int y) {
- this.p = k;
- this.x = x;
- this.y = y;
- }
- public Tile getTileUnderMouse() {
- for (int i = 0; i < p.getMatrix().length; ++i) {
- for (int j = 0; j < p.getMatrix()[i].length; ++j) {
- if (i == y && j == x)
- return p.getMatrix()[i][j];
- }
- }
- return null;
- }
- public void run() {
- // Iterator<Tile> it = p.iterator();
- // Iterator<Tile> it = p.iteratorVertical();
- Iterator<Tile> it = p.iteratorObliquely(x, y);
- Tile tileUnderMouse = getTileUnderMouse();
- boolean fall = false;
- while (it.hasNext()) {
- // Iterate until we get tile under mouse
- Tile tile = it.next();
- if (tile == tileUnderMouse) {
- fall = true;
- }
- // Then flip tiles
- if (fall == true) {
- tile.flip();
- p.repaint();
- try {
- Thread.currentThread().sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement