Posted By

shreyharia on 07/06/11


Tagged

java swings awt


Versions (?)

Chess Board GUI


 / Published in: Java
 

  1. package mychessgui;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import javax.swing.border.LineBorder;
  6.  
  7. public class Main {
  8.  
  9. static final char BLACK_PAWN = '\u265F';
  10. static final char BLACK_ROOK = '\u265C';
  11. static final char BLACK_KNIGHT = '\u265E';
  12. static final char BLACK_BISHOP = '\u265D';
  13. static final char BLACK_QUEEN = '\u265B';
  14. static final char BLACK_KING = '\u265A';
  15. static final char WHITE_PAWN = '\u2659';
  16. static final char WHITE_ROOK = '\u2656';
  17. static final char WHITE_KNIGHT = '\u2658';
  18. static final char WHITE_BISHOP = '\u2657';
  19. static final char WHITE_QUEEN = '\u2655';
  20. static final char WHITE_KING = '\u2654';
  21.  
  22. /**
  23.   * @ShreyHaria args the command line arguments
  24.   */
  25. public static void main(String[] args) {
  26. GUI gui = new GUI();
  27. ImageIcon WK = new ImageIcon("E:\\WHITE_KNIGHT.jpg");
  28. Tile[] board = new Tile[64];
  29. int count = 0;
  30. for (int rank = 1; rank <= 8; rank++, count++) {
  31. for (char file = 'a'; file <= 'h'; file++) {
  32.  
  33.  
  34. if ((file + rank) % 2 == 0) {
  35. board[count] = new Tile("", Color.GRAY);
  36. } else {
  37. board[count] = new Tile("", Color.WHITE);
  38. }
  39.  
  40.  
  41. gui.mainframe.add(board[count].lbl);
  42. if (rank == 2) {
  43. board[count].lbl.setText("" + WHITE_PAWN);
  44. } else if (rank == 7) {
  45. board[count].lbl.setText("" + BLACK_PAWN);
  46. } else if (rank == 1) {
  47. switch (file) {
  48. case 'a':
  49. case 'h':
  50. board[count].lbl.setText("" + WHITE_ROOK);
  51. break;
  52. case 'b':
  53. case 'g':
  54. //board[count].lbl.setIcon(WK);/**/
  55. /*heree got it*/// board[count].lbl.set
  56. board[count].lbl.setText("" + WHITE_KNIGHT);
  57. break;
  58. case 'c':
  59. case 'f':
  60. board[count].lbl.setText("" + WHITE_BISHOP);
  61. break;
  62. case 'd':
  63. board[count].lbl.setText("" + WHITE_QUEEN);
  64. break;
  65. case 'e':
  66. board[count].lbl.setText("" + WHITE_KING);
  67. break;
  68.  
  69. }
  70. } else if (rank == 8) {
  71. switch (file) {
  72. case 'a':
  73. case 'h':
  74. board[count].lbl.setText("" + BLACK_ROOK);
  75. break;
  76. case 'b':
  77. case 'g':
  78. board[count].lbl.setText("" + BLACK_KNIGHT);
  79. break;
  80. case 'c':
  81. case 'f':
  82. board[count].lbl.setText("" + BLACK_BISHOP);
  83. break;
  84. case 'd':
  85. board[count].lbl.setText("" + BLACK_QUEEN);
  86. break;
  87. case 'e':
  88. board[count].lbl.setText("" + BLACK_KING);
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. gui.mainframe.setVisible(true);
  95. }
  96. }
  97.  
  98. class GUI {
  99.  
  100. static JFrame mainframe = new JFrame();
  101. GridLayout grid = new GridLayout(8, 8);
  102.  
  103. GUI() {
  104. mainframe.setSize(700, 700);
  105. mainframe.setLayout(grid);
  106. mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107. }
  108. }
  109.  
  110. class Tile {
  111.  
  112. JLabel lbl = new JLabel();
  113. static Font unicode = new Font("Arial Unicode MS", Font.BOLD, 50);
  114. static LineBorder border = new LineBorder(Color.BLACK, 2);
  115.  
  116. Tile(String lblname, Color tc) {
  117. lbl.setFont(unicode);
  118. lbl.setHorizontalAlignment(JLabel.CENTER);
  119. lbl.setText(lblname);
  120. lbl.setBackground(tc);
  121. lbl.setBorder(border);
  122. lbl.setOpaque(true);
  123. }
  124. }

Report this snippet  

You need to login to post a comment.