Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.GridLayout;
  5. import java.awt.Image;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.event.MouseMotionListener;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12. import java.io.IOException;
  13.  
  14. import javax.imageio.ImageIO;
  15. import javax.swing.Icon;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20.  
  21. public class ChessBoard extends JPanel {
  22.  
  23. //implements MouseListener, MouseMotionListener
  24. private int activeButton;
  25. private ImageIcon player;
  26. JButton grid[][] = new JButton[8][8];
  27. JButton square;
  28.  
  29. public ChessBoard(){
  30.  
  31. //black pieces
  32. ImageIcon blackCastle = new ImageIcon("Images/BlackCastle.jpg");
  33. Image bc = blackCastle.getImage();
  34. Image bcimg = bc.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  35. ImageIcon bcIcon = new ImageIcon(bcimg);
  36.  
  37. ImageIcon blackBishop = new ImageIcon("Images/BlackBishop.jpg");
  38. Image bb = blackBishop.getImage();
  39. Image bbimg = bb.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  40. ImageIcon bbIcon = new ImageIcon(bbimg);
  41.  
  42. ImageIcon blackKing = new ImageIcon("Images/BlackKing.jpg");
  43. Image bK = blackKing.getImage();
  44. Image bKimg = bK.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  45. ImageIcon bKIcon = new ImageIcon(bKimg);
  46.  
  47. ImageIcon blackPawn = new ImageIcon("Images/BlackPond.jpeg");
  48. Image bp = blackPawn.getImage();
  49. Image bpimg = bp.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  50. ImageIcon bpIcon = new ImageIcon(bpimg);
  51.  
  52. ImageIcon blackQueen = new ImageIcon("Images/BlackQueen.jpg");
  53. Image bq = blackQueen.getImage();
  54. Image bqimg = bq.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  55. ImageIcon bqIcon = new ImageIcon(bqimg);
  56.  
  57. ImageIcon BlackKnite = new ImageIcon("Images/BlackKnight.jpg");
  58. Image bk = BlackKnite.getImage();
  59. Image bkimg = bk.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  60. ImageIcon bkIcon = new ImageIcon(bkimg);
  61.  
  62. //white pieces
  63. ImageIcon whiteCastle = new ImageIcon("Images/WhiteCastle.jpg");
  64. Image wc = whiteCastle.getImage();
  65. Image wcimg = wc.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  66. ImageIcon wcIcon = new ImageIcon(wcimg);
  67.  
  68. ImageIcon whiteBishop = new ImageIcon("Images/WhiteBishop.jpg");
  69. Image wb = whiteBishop.getImage();
  70. Image wbimg = wb.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  71. ImageIcon wbIcon = new ImageIcon(wbimg);
  72.  
  73. ImageIcon whiteKing = new ImageIcon("Images/WhiteKing.jpg");
  74. Image wK = whiteKing.getImage();
  75. Image wKimg = wK.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  76. ImageIcon wKIcon = new ImageIcon(wKimg);
  77.  
  78. ImageIcon whitePawn = new ImageIcon("Images/WhitePawn.jpg");
  79. Image wp = whitePawn.getImage();
  80. Image wpimg = wp.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  81. ImageIcon wpIcon = new ImageIcon(wpimg);
  82.  
  83. ImageIcon whiteQueen = new ImageIcon("Images/WhiteQueen.jpg");
  84. Image wq = whiteQueen.getImage();
  85. Image wqimg = wq.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  86. ImageIcon wqIcon = new ImageIcon(wqimg);
  87.  
  88. ImageIcon whiteKnite = new ImageIcon("Images/WhiteKnight.jpg");
  89. Image wk = whiteKnite.getImage();
  90. Image wkimg = wk.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
  91. ImageIcon wkIcon = new ImageIcon(wkimg);
  92.  
  93.  
  94. //set up board
  95. JFrame board = new JFrame();
  96. board.setSize(640,640);
  97. board.setLayout(new GridLayout(8,8));
  98.  
  99.  
  100. //create 64 button field
  101. for(int x = 0; x<8; x++)
  102. {
  103. for(int y = 0; y<8; y++)
  104. {
  105.  
  106. JButton square = new JButton();
  107. if (x % 2 == 0)
  108. if (y % 2 == 0)
  109. square.setBackground(Color.RED);
  110. else
  111. square.setBackground(Color.BLACK);
  112. if (x % 2 == 1)
  113. if (y % 2 == 1)
  114. square.setBackground(Color.RED);
  115. else
  116. square.setBackground(Color.BLACK);
  117. square.setOpaque(true);
  118. grid[x][y] = square;
  119. board.add(grid[x][y]);
  120.  
  121. }
  122.  
  123. }
  124.  
  125. //row 1
  126. grid[0][0].setIcon(bcIcon);
  127. grid[0][1].setIcon(bkIcon);
  128. grid[0][2].setIcon(bbIcon);
  129. grid[0][3].setIcon(bKIcon);
  130. grid[0][4].setIcon(bqIcon);
  131. grid[0][5].setIcon(bbIcon);
  132. grid[0][6].setIcon(bkIcon);
  133. grid[0][7].setIcon(bcIcon);
  134.  
  135. //row2
  136. grid[1][0].setIcon(bpIcon);
  137. grid[1][1].setIcon(bpIcon);
  138. grid[1][2].setIcon(bpIcon);
  139. grid[1][3].setIcon(bpIcon);
  140. grid[1][4].setIcon(bpIcon);
  141. grid[1][5].setIcon(bpIcon);
  142. grid[1][6].setIcon(bpIcon);
  143. grid[1][7].setIcon(bpIcon);
  144.  
  145. //row8
  146. grid[7][0].setIcon(wcIcon);
  147. grid[7][1].setIcon(wkIcon);
  148. grid[7][2].setIcon(wbIcon);
  149. grid[7][3].setIcon(wqIcon);
  150. grid[7][4].setIcon(wKIcon);
  151. grid[7][5].setIcon(wbIcon);
  152. grid[7][6].setIcon(wkIcon);
  153. grid[7][7].setIcon(wcIcon);
  154.  
  155. //row7
  156. grid[6][0].setIcon(wpIcon);
  157. grid[6][1].setIcon(wpIcon);
  158. grid[6][2].setIcon(wpIcon);
  159. grid[6][3].setIcon(wpIcon);
  160. grid[6][4].setIcon(wpIcon);
  161. grid[6][5].setIcon(wpIcon);
  162. grid[6][6].setIcon(wpIcon);
  163. grid[6][7].setIcon(wpIcon);
  164.  
  165.  
  166.  
  167. board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  168. board.setVisible(true);
  169. }
  170.  
  171. //This is the part that I am having trouble with. I need to find a way to select the icon and get it to move to the other JButton as well as remove it from the original JButton.
  172. public void actionPerformed(ActionEvent evt) {
  173.  
  174. Object source = evt.getSource();
  175. if (source instanceof JButton) {
  176. JButton clicked = (JButton)source;
  177. grid[8][8].get(clicked).setIcon(null);
  178. clicked.setIcon(player);
  179. activeButton = grid.indexOf(clicked);
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement