Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.Iterator;
- import javax.swing.JFrame;
- 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;
- public tileIterator iteratorek;
- public class tileIterator implements Iterator {
- public int x_index;
- public int y_index;
- @Override
- public boolean hasNext() {
- if(x_index+1 < getCols() || y_index+1 < getRows())
- return true;
- else
- return false; }
- @Override
- public Tile next() {
- if (this.hasNext()){
- if (x_index < getCols()) {
- x_index++;
- return matrix[y_index][x_index]; }
- else if (x_index == matrix.length) {
- if (y_index < getRows()) {
- x_index = 0;
- y_index++;
- return matrix[y_index][x_index]; }
- }
- }
- return null;
- }
- public tileIterator (int x, int y) {
- x_index = x;
- y_index = y;
- }
- }
- public void setIterator (int x, int y) {
- if (this.iteratorek == null)
- this.iteratorek = new tileIterator(x,y);
- else {
- this.iteratorek.x_index = x;
- this.iteratorek.y_index = y;
- }
- }
- // 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();
- }
- // trzy poniższe metody znikną w finalnej wersji
- public int getRows() {
- return matrix.length;
- }
- public int getCols() {
- return matrix[0].length;
- }
- public Tile getAt(int row, int col) {
- return matrix[row][col];
- }
- // za to pojawi się metoda pobierająca iterator
- public Iterator<Tile> iterator() {
- return iteratorek;
- }
- }
- // 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 void run() {
- // klasyczna podwójna pętla do iteracji
- // tutaj kontrolujemy kolejność odwiedzin
- // zostanie to zastąpione pętlą z użyciem iteratora
- p.setIterator(x,y);
- while (p.iterator().hasNext()) {
- System.out.println(x+" "+y);
- p.setIterator(x,y);
- p.iterator().next().flip();
- p.repaint();
- try {
- Thread.currentThread().sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (x+1<p.getCols())
- x++;
- else if (x==p.getCols() && y<p.getRows()) {
- x=0;
- y++;
- }
- }
- /*for (int i = y; i < p.getRows(); ++i) {
- int j;
- if (i == y) {
- j = x;
- } else {
- j = 0;
- }
- for (; j < p.getCols(); ++j) {
- // a w środku - obracamy, odświeżamy i czekamy
- p.getAt(i, j).flip();
- p.repaint();
- try {
- Thread.currentThread().sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }*/
- }
- }
- public class Ztp08 {
- static final int TILESIZE = 40;
- public static void main(String[] args) {
- // konstruowanie okna
- JFrame frame = new JFrame("Iterator");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- final Kafelki kafelki = new Kafelki(16, 9, TILESIZE);
- frame.getContentPane().add(kafelki);
- frame.pack();
- frame.setVisible(true);
- // reakcja na kliknięcie uruchomienie wątku z iteracją
- kafelki.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- int x = e.getX() / TILESIZE;
- int y = e.getY() / TILESIZE;
- new Thread(new Watek(kafelki, x, y)).start();
- }
- });
- // reakcja na ruch - podświetlenie wskazanego kafelka
- kafelki.addMouseMotionListener(new MouseAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- int x = e.getX() / TILESIZE;
- int y = e.getY() / TILESIZE;
- kafelki.highlight(x, y);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement