Witam. Na wstępie zwracam uwagę, że nie jestem orłem z programowania. Program pomagał mi zrobić kolega, lecz wyjechał i zostałam z tym sama. Potrzebuję pomocy ;(.
- Nie wiem, jak zrobić aby buttony 4x4 i 5x5 zmieniały wielkość kolumn i wierszy ;/
- Nie do końca rozumiem ten licznik czasu.
Oto moje wypociny:
// Program "Przesuwanka" zawiera 3 pliki
// 1) SlidePuzzle.java - tworzenie głównego okna
// 2) SlidePuzzleGUI.java - implementacja interfejsu
// 3) SlidePuzzleModel.java - funkcjonowanie programu - model logiczny
import javax.swing.JFrame;
// klasa SlidePuzzle
class SlidePuzzle {
//============================================= metoda main
public static void main(String[] args) {
JFrame window = new JFrame("Przesuwanka");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new SlidePuzzleGUI());
window.pack(); // finalizuje layout
window.show(); // widoczność okna
window.setResizable(false);
}//end metody main
}//end klasy SlidePuzzle
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
// klasa SlidePuzzleGUI
// Klasa zawiera wszystkie części interfejsu GUI
class SlidePuzzleGUI extends JPanel {
//=============================================== zmienne instancji
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
//end instance variables
//====================================================== constructor
public SlidePuzzleGUI() {
JButton newTrzyButton = new JButton("3 x 3");
newTrzyButton.addActionListener(new TrzyAction());
JButton newCzteryButton = new JButton("4 x 4");
newCzteryButton.addActionListener(new CzteryAction());
JButton newPiecButton = new JButton("5 x 5");
newPiecButton.addActionListener(new PiecAction());
//--- Tworzenie panelu kontrolnego
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newTrzyButton);
controlPanel.add(newCzteryButton);
controlPanel.add(newPiecButton);
//dodawanie przycisków do panelu
//controlPanel.showMessageDialog(SlidePuzzleGUI.this, "Twój wynik to: " + System.currentTimeMillis(), "Wynik", controlPanel.INFORMATION_MESSAGE);
//--- tworzenie panelu graficznego
_puzzleGraphics = new GraphicsPanel();
//--- ułożenie
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}//end constructor
// klasa GraphicsPanel
// Zdefiniowana wewnątrz zewnętrznej klasy, by mieć możliwość
// wykorzystywać zmienne instancji klas
class GraphicsPanel extends JPanel implements MouseListener {
private static final int ROWS = 3;
private static final int COLS = 3;
private static final int CELL_SIZE = 100; // rozmiar komórki 100 Pixels
private Font _biggerFont;
//================================================== constructor
public GraphicsPanel() {
_biggerFont = new Font("Arial", Font.BOLD, CELL_SIZE/2); // ustawienie czcionki
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.red); // tło
this.addMouseListener(this); // mouselistener reagujący na eventy
}//end constructor
// metoda paintComponent
// tworzenie klocków z odpowiednią numeracją, kolorem, czcionką itd.
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.yellow);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.blue);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}//end paintComponent
// listener mousePressed
public void mousePressed(MouseEvent e) {
// zmiana koordynatów x,y na kolumne i wiersz
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
// sprawdza możliwość przesunięcia; gdy można -> zwraca true, gdy nie false
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}//end mousePressed
// ignorowanie eventów
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}//end class GraphicsPanel
public class TrzyAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
// działanie przycisku 3x3
}
}
public class CzteryAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
// działanie przycisku 4x4
}
}
public class PiecAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
// działanie przycisku 5x5
}//end class SlidePuzzleGUI
// klasa SlidePuzzleModel
class SlidePuzzleModel {
private static final int ROWS = 3;
private static final int COLS = 3;
private Tile[][] _contents; // klocki.
private Tile _emptyTile; // pusta przestrzeń.
// constructor
public SlidePuzzleModel() {
_contents = new Tile[ROWS][COLS];
reset(); // inicjuj i miesza klocki
}//end constructor
// getFace
// zwraca liczby do wyświetlenia w odpowiednich klockach
String getFace(int row, int col) {
return _contents[row][col].getFace();
}//end getFace
// reset
// inicjuje i miesza klocki.
public void reset() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
_contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1));
}
}
// ostatni klocek na null - pusta przestrzeń
_emptyTile = _contents[ROWS-1][COLS-1];
_emptyTile.setFace(null);
// randomowe mieszanie klocków
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
exchangeTiles(r, c, (int)(Math.random()*ROWS)
, (int)(Math.random()*COLS));
}
}
}//end reset
// moveTile
// Przesunięcie klocka na pustą przestrzeń obok niego, jeśli to jest możliwe
// Zwraca true, jeśli przesunięto, false jeśli nie
public boolean moveTile(int r, int c) {
// ruch możliwy, jeśli puste miejsce znajduje się w bezpośrednim sąsiedztwie klocka
return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}//end moveTile
// checkEmpty
// Sprawdzanie czy obok klocka jest wolna przestrzeń
// Zwraca true i zmienia pozycję, jeśli można; false w przeciwnym wypadku
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
// sprawdzanie, czy "sąsiad" jest pustą przestrzenią
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}//end checkEmpty
// isLegalRowCol
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<ROWS && c>=0 && c<COLS;
}//end isLegalRowCol
// exchangeTiles
// zmiana pozycji klocków
private void exchangeTiles(int r1, int c1, int r2, int c2) {
Tile temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}//end exchangeTiles
// isGameOver
// sprawdzanie zakończenia gry
public boolean isGameOver() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
Tile trc = _contents[r][c];
if (! trc.isInFinalPosition(r, c) ) return false;
}
}
return true;
}//end isGameOver
}//end klasy SlidePuzzleModel
// klasa Tile
// reprezentuje poszczególne klocki, przesuwające się w grze
class Tile {
// zmienne instancji
private int _row; // wiersz pozycji koncowej
private int _col; // kolumna pozycji koncowej
private String _face;
//end zmienne instancji
// constructor
public Tile(int row, int col, String face) {
_row = row;
_col = col;
_face = face;
}//end constructor
// setFace
public void setFace(String newFace) {
_face = newFace;
}//end getFace
// getFace
public String getFace() {
return _face;
}//end getFace
// isInFinalPosition
public boolean isInFinalPosition(int r, int c) {
return r==_row && c==_col;
}//end isInFinalPosition
}//end class Tile
Proszę o pomoc, jutro muszę oddać projekt :(.
Ania.