Advertisement
Guest User

Untitled

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