Advertisement
Guest User

Untitled

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