Нарисовал доску шахматную, расставил фигуры и добавил парочку евентов.
Все старался сразу же делать по принципу MVC дабы набивать руку. Перечитал и сейчас и раньше десятки статей, вроде и принцип понимаю, а при программировании шаг влево, шаг вправо и тону.. :(
Так вот, вот черновой вариант трех классов, вот так вот сходу они мне написались.
1. Пожалуйста подкорректируйте как кто понимает это все?
2. И еще вопросик, припустим есть обычная форма - две кнопки и два поля для ввода - откуда высосать Model? Получается будет только интерфейс(View) и обработчик событий этой формы(Controller)?
3. И еще вопросик, тут вот мыслю подкинули(да я и сам понимаю), что такой код не есть хорошо...
1.
Object chekPiece = ((JLabel) (((JComponent) evt.getComponent()).getComponent(0))).getIcon();
Альтернатива? Создавать временные переменные - будет больше кода, но читабельней...
Спасибо за советы - вот три класса!
Да конечно, кто-то может лучшую идею предложить как код написать и так д.. но я спрашиваю о архитектуре.
View
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110.
package chess.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChessBoardView extends JPanel {
private JPanel northInfoPanel = new JPanel(new GridLayout(1, 8));
private JPanel southInfoPanel = new JPanel(new GridLayout(1, 8));
private JPanel westInfoPanel = new JPanel(new GridLayout(8, 1));
private JPanel eastInfoPanel = new JPanel(new GridLayout(8, 1));
private JPanel centerBoardPanel = new JPanel(new GridLayout(8, 8));
private String[] letterArray = new String[] { " a",
" b", " c", " d", " e",
" f", " g", " h" };
private JPanel[][] countChessCells = new JPanel[8][8];
private ChessBoardController chessBoardController;
public ChessBoardView() {
this.chessBoardController = new ChessBoardController();
this.setLayout(new BorderLayout());
drawNorthInfoPanel();
drawSouthInfoPanel();
drawChessBoard();
drawEastInfoPanel();
drawWestInfoPanel();
this.chessBoardController.setChessBoardView(this);
this.add(northInfoPanel, BorderLayout.NORTH);
this.add(southInfoPanel, BorderLayout.SOUTH);
this.add(centerBoardPanel, BorderLayout.CENTER);
this.add(westInfoPanel, BorderLayout.WEST);
this.add(eastInfoPanel, BorderLayout.EAST);
}
private void drawChessBoard() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
countChessCells[y][x] = new JPanel(new BorderLayout());
countChessCells[y][x].addMouseListener(this.chessBoardController);
this.centerBoardPanel.add(countChessCells[y][x]);
if (y % 2 == 0) {
if (x % 2 != 0) {
countChessCells[y][x].setBackground(Color.GRAY);
} else {
countChessCells[y][x].setBackground(Color.WHITE);
}
} else {
if (x % 2 == 0) {
countChessCells[y][x].setBackground(Color.GRAY);
} else {
countChessCells[y][x].setBackground(Color.WHITE);
}
}
}
}
}
private void drawNorthInfoPanel() {
for (int i = 0; i < 8; i++) {
this.northInfoPanel.add(new JLabel(letterArray[i]),
BorderLayout.CENTER);
}
}
private void drawSouthInfoPanel() {
for (int i = 0; i < 8; i++) {
this.southInfoPanel.add(new JLabel(letterArray[i]),
BorderLayout.CENTER);
}
}
private void drawEastInfoPanel() {
for (int i = 8; i >= 1; i--) {
this.eastInfoPanel.add(new JLabel(Integer.toString(i)),
BorderLayout.CENTER);
}
}
private void drawWestInfoPanel() {
for (int i = 8; i >= 1; i--) {
this.westInfoPanel.add(new JLabel(Integer.toString(i)),
BorderLayout.CENTER);
}
}
public JPanel[][] getCountChessCells() {
return countChessCells;
}
public void setCountChessCells(JPanel[][] countChessCells) {
this.countChessCells = countChessCells;
}
}
Controller
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150.
package chess.ui;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
public class ChessBoardController implements MouseListener{
private ChessBoardView chessBoardView;
private ChessBoardModel chessBoardModel;
private JComponent mouseFocusComponent;
private JComponent mousePressedComponent;
public ChessBoardController() {
this.chessBoardModel = new ChessBoardModel();
}
private void init(){
arrangeChessPieces();
}
private void arrangeChessPieces() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
this.chessBoardView.getCountChessCells()[y][x].add(
this.getPieceObject(this.chessBoardModel.getStrChessBoard()[y][x]),
BorderLayout.CENTER);
this.chessBoardView.getCountChessCells()[y][x].validate();
}
}
}
private JLabel getPieceObject(String strPieceName) {
JLabel temPieceLabel;
if (strPieceName.equals("RB"))
temPieceLabel = new JLabel(this.chessBoardModel.getRookBlack());
else if (strPieceName.equals("BB"))
temPieceLabel = new JLabel(this.chessBoardModel.getBishopBlack());
else if (strPieceName.equals("NB"))
temPieceLabel = new JLabel(this.chessBoardModel.getKnightBlack());
else if (strPieceName.equals("QB"))
temPieceLabel = new JLabel(this.chessBoardModel.getQueenBlack());
else if (strPieceName.equals("KB"))
temPieceLabel = new JLabel(this.chessBoardModel.getKingBlack());
else if (strPieceName.equals("PB"))
temPieceLabel = new JLabel(this.chessBoardModel.getPawnBlack());
else if (strPieceName.equals("RW"))
temPieceLabel = new JLabel(this.chessBoardModel.getRookWhite());
else if (strPieceName.equals("BW"))
temPieceLabel = new JLabel(this.chessBoardModel.getBishopWhite());
else if (strPieceName.equals("NW"))
temPieceLabel = new JLabel(this.chessBoardModel.getKnightWhite());
else if (strPieceName.equals("QW"))
temPieceLabel = new JLabel(this.chessBoardModel.getQueenWhite());
else if (strPieceName.equals("KW"))
temPieceLabel = new JLabel(this.chessBoardModel.getKingWhite());
else if (strPieceName.equals("PW"))
temPieceLabel = new JLabel(this.chessBoardModel.getPawnWhite());
else
temPieceLabel = new JLabel();
return temPieceLabel;
}
private String getPieceName(String chessIconPath) {
int start = 28;
int end = chessIconPath.length() - 4;
String pieceName = chessIconPath.substring(start, end);
return pieceName;
}
@Override
public void mouseClicked(MouseEvent evt) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent evt) {
if (((JComponent) evt.getComponent()).getBackground() != this.chessBoardModel.getMousePressedColor())
this.chessBoardModel.setTempMouseEnteredComponentColor(((JComponent) evt
.getComponent()).getBackground());
if ((JComponent) evt.getSource() != this.mousePressedComponent) {
this.mouseFocusComponent = (JComponent) evt.getComponent();
this.chessBoardModel.setTempMouseFocusComponentColor(this.mouseFocusComponent
.getBackground());
this.mouseFocusComponent.setBackground(this.chessBoardModel.getMouseEnteredColor());
}
}
@Override
public void mouseExited(MouseEvent evt) {
if ((JComponent) evt.getSource() != this.mousePressedComponent)
this.mouseFocusComponent
.setBackground(this.chessBoardModel.getTempMouseFocusComponentColor());
}
@Override
public void mousePressed(MouseEvent evt) {
Object chekPiece = ((JLabel) (((JComponent) evt.getComponent())
.getComponent(0))).getIcon();
if (chekPiece != null) {
System.out.println(getPieceName(chekPiece.toString()));
} else {
System.out.println("Zero");
}
if (this.mousePressedComponent == null) {
this.mousePressedComponent = (JComponent) evt.getComponent();
this.chessBoardModel.setTempMousePressedComponentColor(this.chessBoardModel.getTempMouseEnteredComponentColor());
this.mousePressedComponent.setBackground(this.chessBoardModel.getMousePressedColor());
} else {
if (((JComponent) evt.getComponent()).getBackground() != this.chessBoardModel.getMousePressedColor()) {
this.mousePressedComponent
.setBackground(this.chessBoardModel.getTempMousePressedComponentColor());
this.chessBoardModel.setTempMousePressedComponentColor(this.chessBoardModel.getTempMouseEnteredComponentColor());
this.mousePressedComponent = (JComponent) evt.getComponent();
this.mousePressedComponent
.setBackground(this.chessBoardModel.getMousePressedColor());
} else {
this.mousePressedComponent
.setBackground(chessBoardModel.getTempMousePressedComponentColor());
}
}
}
@Override
public void mouseReleased(MouseEvent evt) {
// mouseFocusComponent.setBackground(mouseEnteredColor);
}
public void setChessBoardView(ChessBoardView chessBoardView) {
this.chessBoardView = chessBoardView;
init();
}
}
Model
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137.
package chess.ui;
import java.awt.Color;
import javax.swing.ImageIcon;
public class ChessBoardModel {
private Color mouseEnteredColor = new Color(200, 230, 255);
private Color tempMouseFocusComponentColor;
private Color tempMousePressedComponentColor;
private Color tempMouseEnteredComponentColor;
private Color mousePressedColor = new Color(255, 150, 150);
private ImageIcon rookBlack = new ImageIcon(
"./lib/images/ChessFigurPack/rookBlack.png");
private ImageIcon rookWhite = new ImageIcon(
"./lib/images/ChessFigurPack/rookWhite.png");
private ImageIcon bishopBlack = new ImageIcon(
"./lib/images/ChessFigurPack/bishopBlack.png");
private ImageIcon bishopWhite = new ImageIcon(
"./lib/images/ChessFigurPack/bishopWhite.png");
private ImageIcon knightBlack = new ImageIcon(
"./lib/images/ChessFigurPack/knightBlack.png");
private ImageIcon knightWhite = new ImageIcon(
"./lib/images/ChessFigurPack/knightWhite.png");
private ImageIcon kingBlack = new ImageIcon(
"./lib/images/ChessFigurPack/kingBlack.png");
private ImageIcon kingWhite = new ImageIcon(
"./lib/images/ChessFigurPack/kingWhite.png");
private ImageIcon queenBlack = new ImageIcon(
"./lib/images/ChessFigurPack/queenBlack.png");
private ImageIcon queenWhite = new ImageIcon(
"./lib/images/ChessFigurPack/queenWhite.png");
private ImageIcon pawnBlack = new ImageIcon(
"./lib/images/ChessFigurPack/pawnBlack.png");
private ImageIcon pawnWhite = new ImageIcon(
"./lib/images/ChessFigurPack/pawnWhite.png");
private String[][] strChessBoard = new String[][] {
{ "RB", "NB", "BB", "QB", "KB", "BB", "NB", "RB" },
{ "PB", "PB", "PB", "PB", "PB", "PB", "PB", "PB" },
{ " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " " },
{ "PW", "PW", "PW", "PW", "PW", "PW", "PW", "PW" },
{ "RW", "NW", "BW", "QW", "KW", "BW", "NW", "RW" } };
public Color getMouseEnteredColor() {
return mouseEnteredColor;
}
public Color getTempMouseFocusComponentColor() {
return tempMouseFocusComponentColor;
}
public Color getTempMousePressedComponentColor() {
return tempMousePressedComponentColor;
}
public Color getTempMouseEnteredComponentColor() {
return tempMouseEnteredComponentColor;
}
public Color getMousePressedColor() {
return mousePressedColor;
}
public ImageIcon getRookBlack() {
return rookBlack;
}
public ImageIcon getRookWhite() {
return rookWhite;
}
public ImageIcon getBishopBlack() {
return bishopBlack;
}
public ImageIcon getBishopWhite() {
return bishopWhite;
}
public ImageIcon getKnightBlack() {
return knightBlack;
}
public ImageIcon getKnightWhite() {
return knightWhite;
}
public ImageIcon getKingBlack() {
return kingBlack;
}
public ImageIcon getKingWhite() {
return kingWhite;
}
public ImageIcon getQueenBlack() {
return queenBlack;
}
public ImageIcon getQueenWhite() {
return queenWhite;
}
public ImageIcon getPawnBlack() {
return pawnBlack;
}
public ImageIcon getPawnWhite() {
return pawnWhite;
}
public String[][] getStrChessBoard() {
return strChessBoard;
}
public void setTempMouseFocusComponentColor(
Color tempMouseFocusComponentColor) {
this.tempMouseFocusComponentColor = tempMouseFocusComponentColor;
}
public void setTempMousePressedComponentColor(
Color tempMousePressedComponentColor) {
this.tempMousePressedComponentColor = tempMousePressedComponentColor;
}
public void setTempMouseEnteredComponentColor(
Color tempMouseEnteredComponentColor) {
this.tempMouseEnteredComponentColor = tempMouseEnteredComponentColor;
}
}
|