Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.event.MouseListener;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class CheckerBoard extends JFrame implements MouseListener, ActionListener {
  8. //Rather than create just JLabels, it is better to create an array of
  9. //objects. The Tiles class is posted under neither this main class.
  10. Tiles[][] boardPieces = new Tiles[8][8];
  11. JRadioButton[] buttons = new JRadioButton[3];
  12. String[] colors = {"Red","Blue","Green"};
  13. Color theColor = Color.RED;
  14. JPanel panel1,panel2;
  15. public CheckerBoard(){
  16. super("CheckerBoard 1.0");
  17.  
  18. //Create the JPanel
  19. panel1 = new JPanel(new GridLayout(boardPieces.length,boardPieces.length));
  20. panel2 = new JPanel(new GridLayout(buttons.length,buttons.length));
  21.  
  22. //Start making all of the pieces. It is better to use the .length value that way
  23. //That way you can create as many as you like.
  24. for(int i = 0; i<boardPieces.length; i++){
  25. for(int k = 0; k<boardPieces.length; k++){
  26. //Creating each piece. This will become more apparent when you look
  27. //into the Tiles class.
  28. boardPieces[i][k] = new Tiles(i,k,0);
  29. //Adding the MouseEvent
  30. boardPieces[i][k].addMouseListener(this);
  31. //Adding it to the JPanel
  32. panel1.add(boardPieces[i][k]);
  33. }
  34. }
  35.  
  36. for(int j = 0; j<buttons.length; j++){
  37. buttons[j] = new JRadioButton(colors[j]);
  38. buttons[j].setBounds(510,510,1,1);
  39. buttons[j].addActionListener(this);
  40. panel2.add(buttons[j]);
  41. }
  42. buttons[0].setSelected(true);
  43.  
  44. //Repaint the Labels
  45. panel1.repaint();
  46.  
  47. //Adding the Panel to the Frame
  48. add(panel1,BorderLayout.CENTER);
  49. add(panel2,BorderLayout.EAST);
  50. }
  51. public void actionPerformed(ActionEvent e){
  52.  
  53. JRadioButton clicked = (JRadioButton)e.getSource();
  54.  
  55. if(clicked == buttons[0]){
  56. buttons[1].setSelected(false);
  57. buttons[2].setSelected(false);
  58. theColor = Color.RED;
  59. }
  60. if(clicked == buttons[1]){
  61. buttons[0].setSelected(false);
  62. buttons[2].setSelected(false);
  63. theColor = Color.BLUE;
  64. }
  65. if(clicked == buttons[2]){
  66. buttons[0].setSelected(false);
  67. buttons[1].setSelected(false);
  68. theColor = Color.GREEN;
  69. }
  70. }
  71. public void mouseClicked(MouseEvent e){
  72.  
  73. //Getting the source of Tiles and creating an object: clicked
  74. Tiles clicked = (Tiles)e.getSource();
  75. //If you clicked something that is displayed, then paint as necessary.
  76. if(clicked.isDisplayable()){
  77. //Call the function that will paint the JLabel
  78. clicked.setColor(theColor);
  79. clicked.paint(clicked.getGraphics());
  80. }
  81.  
  82. }
  83. public void mouseEntered(MouseEvent e)
  84. {
  85. }
  86. public void mousePressed(MouseEvent e)
  87. {
  88. }
  89. public void mouseReleased(MouseEvent e)
  90. {
  91. }
  92. public void mouseExited(MouseEvent e)
  93. {
  94. }
  95. public static void main(String[] args) {
  96. CheckerBoard cb = new CheckerBoard();
  97. cb.setSize(530, 530);
  98. cb.setVisible(true);
  99. cb.setResizable(false);
  100. }
  101.  
  102. }
  103.  
  104. import java.awt.*;
  105. import javax.swing.*;
  106. import java.awt.Graphics;
  107.  
  108. //As noted, it is better to create an array of objects. You have much
  109. //more functionality in here.
  110. public class Tiles extends JLabel {
  111. int x,y;
  112. int width = 50;
  113. int height = 50;
  114. int value;
  115. Color theColor;
  116. //These are all the initial values that you pass in the for-loop
  117. public Tiles(int X, int Y, int v){
  118. //Establish the coordinates and the bounds.
  119. this.x = X;
  120. this.y = Y;
  121. //Set all the pieces originally to 0. That way
  122. //we know they are all empty.
  123. this.value = v;
  124. this.setBounds(this.x*50,this.y*50,width,height);
  125. this.setOpaque(true);
  126. //Draw the initial shape. The original value will
  127. //simply return the color specified.
  128. if(this.x % 2 == 0){
  129. theColor = Color.black;
  130. this.setBackground(Color.black);
  131. }
  132. else {
  133. theColor = Color.white;
  134. this.setBackground(Color.white);
  135. }
  136. }
  137. public void setColor(Color a){
  138. this.theColor = a;
  139. }
  140. //This is the function that will draw the graphics.
  141. public void paint(Graphics g){
  142. super.paint(g);
  143. //First determine the value of the piece clicked.
  144. if(this.value == 0){
  145. //If empty, change the value to being occupied
  146. //by red. Change the oval color to red.
  147. g.setColor(theColor);
  148. }
  149. else {
  150. //If it is not empty, then change it
  151. //back to it's original color.
  152. if(this.x % 2 == 0){
  153. g.setColor(Color.BLACK);
  154. }
  155. else{
  156. g.setColor(Color.WHITE);
  157. }
  158. }
  159. //Fill the oval information.
  160. g.fillOval(x, y, width, height);
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement