Доска шахматная кнопки - Java

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. public class ChessBoardWithColumnsAndRows {
  6. private final JPanel gui = new JPanel(new BorderLayout(3, 3));
  7. private JButton[][] chessBoardSquares = new JButton[8][8];
  8. private JPanel chessBoard;
  9. private final JLabel message = new JLabel(
  10. "Chess Champ is ready to play!");
  11. private static final String COLS = "ABCDEFGH";
  12. ChessBoardWithColumnsAndRows() {
  13. initializeGui();
  14. }
  15. public final void initializeGui() {
  16. // set up the main GUI
  17. gui.setBorder(new EmptyBorder(5, 5, 5, 5));
  18. JToolBar tools = new JToolBar();
  19. tools.setFloatable(false);
  20. gui.add(tools, BorderLayout.PAGE_START);
  21. tools.add(new JButton("New")); // TODO - add functionality!
  22. tools.add(new JButton("Save")); // TODO - add functionality!
  23. tools.add(new JButton("Restore")); // TODO - add functionality!
  24. tools.addSeparator();
  25. tools.add(new JButton("Resign")); // TODO - add functionality!
  26. tools.addSeparator();
  27. tools.add(message);
  28. gui.add(new JLabel("?"), BorderLayout.LINE_START);
  29. chessBoard = new JPanel(new GridLayout(0, 9));
  30. chessBoard.setBorder(new LineBorder(Color.BLACK));
  31. gui.add(chessBoard);
  32. // create the chess board squares
  33. Insets buttonMargin = new Insets(0,0,0,0);
  34. for (int ii = 0; ii < chessBoardSquares.length; ii++) {
  35. for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
  36. JButton b = new JButton();
  37. b.setMargin(buttonMargin);
  38. // our chess pieces are 64x64 px in size, so we'll
  39. // 'fill this in' using a transparent icon..
  40. ImageIcon icon = new ImageIcon(
  41. new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
  42. b.setIcon(icon);
  43. if ((jj % 2 == 1 && ii % 2 == 1)
  44. //) {
  45. || (jj % 2 == 0 && ii % 2 == 0)) {
  46. b.setBackground(Color.WHITE);
  47. } else {
  48. b.setBackground(Color.BLACK);
  49. }
  50. chessBoardSquares[jj][ii] = b;
  51. }
  52. }
  53. //fill the chess board
  54. chessBoard.add(new JLabel(""));
  55. // fill the top row
  56. for (int ii = 0; ii < 8; ii++) {
  57. chessBoard.add(
  58. new JLabel(COLS.substring(ii, ii + 1),
  59. SwingConstants.CENTER));
  60. }
  61. // fill the black non-pawn piece row
  62. for (int ii = 0; ii < 8; ii++) {
  63. for (int jj = 0; jj < 8; jj++) {
  64. switch (jj) {
  65. case 0:
  66. chessBoard.add(new JLabel("" + (ii + 1),
  67. SwingConstants.CENTER));
  68. default:
  69. chessBoard.add(chessBoardSquares[jj][ii]);
  70. }
  71. }
  72. }
  73. }
  74. public final JComponent getChessBoard() {
  75. return chessBoard;
  76. }
  77. public final JComponent getGui() {
  78. return gui;
  79. }
  80. public static void main(String[] args) {
  81. Runnable r = new Runnable() {
  82. @Override
  83. public void run() {
  84. ChessBoardWithColumnsAndRows cb =
  85. new ChessBoardWithColumnsAndRows();
  86. JFrame f = new JFrame("ChessChamp");
  87. f.add(cb.getGui());
  88. f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  89. f.setLocationByPlatform(true);
  90. // ensures the frame is the minimum size it needs to be
  91. // in order display the components within it
  92. f.pack();
  93. // ensures the minimum size is enforced.
  94. f.setMinimumSize(f.getSize());
  95. f.setVisible(true);
  96. }
  97. };
  98. SwingUtilities.invokeLater(r);
  99. }
  100. }
Есть нарисованное шахматное поле Как сделать, что бы по клику на кнопку(button или клик мышки не важно) поля меняли местами свой цвет(белые с черным)

Решение задачи: «Доска шахматная кнопки»

textual
Листинг программы
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.BufferedImage;
  4. import java.time.temporal.IsoFields;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. public class ChessBoardWithColumnsAndRows implements ActionListener {
  9.  
  10.     private final JPanel gui = new JPanel(new BorderLayout(3, 3));
  11.     private JButton[][] chessBoardSquares = new JButton[8][8];
  12.     private JPanel chessBoardPanel;
  13.     private JPanel chessBoard;
  14.     private final JLabel message = new JLabel("Chess Champ is ready to play!");
  15.     private static final String COLS = "ABCDEFGH";
  16.     private JButton resigButton;
  17.     private boolean isFirstWhite = true;
  18.  
  19.     ChessBoardWithColumnsAndRows() {
  20.         initializeGui();
  21.     }
  22.  
  23.     public final void initializeGui() {
  24.         // set up the main GUI
  25.         gui.setBorder(new EmptyBorder(5, 5, 5, 5));
  26.         JToolBar tools = new JToolBar();
  27.         tools.setFloatable(false);
  28.         gui.add(tools, BorderLayout.PAGE_START);
  29.         tools.add(new JButton("New")); // TODO - add functionality!
  30.         tools.add(new JButton("Save")); // TODO - add functionality!
  31.         tools.add(new JButton("Restore")); // TODO - add functionality!
  32.         tools.addSeparator();
  33.         resigButton = new JButton("Resign");
  34.         tools.add(resigButton);
  35.         resigButton.addActionListener(this);
  36.         tools.addSeparator();
  37.         tools.add(message);
  38.  
  39.         gui.add(new JLabel("?"), BorderLayout.LINE_START);
  40.  
  41.         chessBoard = new JPanel(new GridLayout(0, 9));
  42.         chessBoard.setBorder(new LineBorder(Color.BLACK));
  43.  
  44.         initializeChessBoard();
  45.         gui.add(chessBoard);
  46.     }
  47.  
  48.     private void initializeChessBoard() {
  49.         chessBoard.removeAll();
  50.         Insets buttonMargin = new Insets(0, 0, 0, 0);
  51.         for (int ii = 0; ii < chessBoardSquares.length; ii++) {
  52.             for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
  53.                 JButton b = new JButton();
  54.                 b.setMargin(buttonMargin);
  55.                 // our chess pieces are 64x64 px in size, so we'll
  56.                 // 'fill this in' using a transparent icon..
  57.                 ImageIcon icon = new ImageIcon(new BufferedImage(64, 64,
  58.                         BufferedImage.TYPE_INT_ARGB));
  59.                 b.setIcon(icon);
  60.                 if ((jj % 2 == 1 && ii % 2 == 1)
  61.                         || (jj % 2 == 0 && ii % 2 == 0)) {
  62.                     if (isFirstWhite) {
  63.                         b.setBackground(Color.WHITE);
  64.                     } else {
  65.                         b.setBackground(Color.BLACK);
  66.                     }
  67.                 } else {
  68.                     if (isFirstWhite) {
  69.                         b.setBackground(Color.BLACK);
  70.                     } else {
  71.                         b.setBackground(Color.WHITE);
  72.                     }
  73.                 }
  74.                 chessBoardSquares[jj][ii] = b;
  75.             }
  76.         }
  77.  
  78.         // fill the chess board
  79.         chessBoard.add(new JLabel(""));
  80.         // fill the top row
  81.         for (int ii = 0; ii < 8; ii++) {
  82.             chessBoard.add(new JLabel(COLS.substring(ii, ii + 1),
  83.                     SwingConstants.CENTER));
  84.         }
  85.         // fill the black non-pawn piece row
  86.         for (int ii = 0; ii < 8; ii++) {
  87.             for (int jj = 0; jj < 8; jj++) {
  88.                 switch (jj) {
  89.                 case 0:
  90.                     chessBoard.add(new JLabel("" + (ii + 1),
  91.                             SwingConstants.CENTER));
  92.                 default:
  93.                     chessBoard.add(chessBoardSquares[jj][ii]);
  94.                 }
  95.             }
  96.         }
  97.         chessBoard.revalidate();
  98.         chessBoard.repaint();
  99.     }
  100.  
  101.     public final JComponent getChessBoard() {
  102.         return chessBoard;
  103.     }
  104.  
  105.     public final JComponent getGui() {
  106.         return gui;
  107.     }
  108.  
  109.     public static void main(String[] args) {
  110.         Runnable r = new Runnable() {
  111.  
  112.             @Override
  113.             public void run() {
  114.                 ChessBoardWithColumnsAndRows cb = new ChessBoardWithColumnsAndRows();
  115.  
  116.                 JFrame f = new JFrame("ChessChamp");
  117.                 f.add(cb.getGui());
  118.                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  119.                 f.setLocationByPlatform(true);
  120.  
  121.                 // ensures the frame is the minimum size it needs to be
  122.                 // in order display the components within it
  123.                 f.pack();
  124.                 // ensures the minimum size is enforced.
  125.                 f.setMinimumSize(f.getSize());
  126.                 f.setVisible(true);
  127.             }
  128.         };
  129.         SwingUtilities.invokeLater(r);
  130.     }
  131.  
  132.     @Override
  133.     public void actionPerformed(ActionEvent evnt) {
  134.         Object source = evnt.getSource();
  135.         if (source == resigButton) {
  136.             isFirstWhite = !isFirstWhite;
  137.             initializeChessBoard();
  138.         }
  139.     }
  140. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы