Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.*;
- public class CheckerBoard extends JFrame implements MouseListener {
- //Rather than create just JLabels, it is better to create an array of
- //objects. The Tiles class is posted under neither this main class.
- Tiles[][] boardPieces = new Tiles[8][8];
- public CheckerBoard(){
- super("CheckerBoard 1.0");
- //Create the JPanel
- JPanel panel = new JPanel(new GridLayout(boardPieces.length,boardPieces.length));
- //Start making all of the pieces. It is better to use the .length value that way
- //That way you can create as many as you like.
- for(int i = 0; i<boardPieces.length; i++){
- for(int k = 0; k<boardPieces.length; k++){
- //Creating each piece. This will become more apparent when you look
- //into the Tiles class.
- boardPieces[i][k] = new Tiles(i,k,0);
- //Adding the MouseEvent
- boardPieces[i][k].addMouseListener(this);
- //Adding it to the JPanel
- panel.add(boardPieces[i][k]);
- }
- }
- //Adding the Panel to the Frame
- add(panel,BorderLayout.CENTER);
- }
- public void mouseClicked(MouseEvent e){
- //Getting the source of Tiles and creating an object: clicked
- Tiles clicked = (Tiles)e.getSource();
- //If you clicked something that is displayed, then paint as necessary.
- if(clicked.isDisplayable()){
- //Call the function that will paint the JLabel
- clicked.paintComponents(clicked.getGraphics());
- }
- }
- public void mouseEntered(MouseEvent e)
- {
- }
- public void mousePressed(MouseEvent e)
- {
- }
- public void mouseReleased(MouseEvent e)
- {
- }
- public void mouseExited(MouseEvent e)
- {
- }
- public static void main(String[] args) {
- CheckerBoard cb = new CheckerBoard();
- cb.setSize(475, 475);
- cb.setVisible(true);
- }
- }
- import java.awt.*;
- import javax.swing.*;
- import java.awt.Graphics;
- //As noted, it is better to create an array of objects. You have much
- //more functionality in here.
- public class Tiles extends JLabel {
- int x,y;
- int width = 50;
- int height = 50;
- int value;
- //These are all the initial values that you pass in the for-loop
- public Tiles(int X, int Y, int v){
- //Establish the coordinates and the bounds.
- this.x = X;
- this.y = Y;
- //Set all the pieces originally to 0. That way
- //we know they are all empty.
- this.value = v;
- this.setBounds(this.x*50,this.y*50,width,height);
- this.setOpaque(true);
- //Draw the initial shape. The original value will
- //simply return the color specified.
- this.repaint();
- if(this.x % 2 == 0){
- this.setBackground(Color.black);
- }
- else {
- this.setBackground(Color.white);
- }
- }
- //This is the function that will draw the graphics.
- public void paintComponents(Graphics g){
- //First determine the value of the piece clicked.
- if(this.value == 0){
- //If empty, change the value to being occupied
- //by red. Change the oval color to red.
- this.value = 1;
- g.setColor(Color.RED);
- }
- else {
- //If it is not empty, then change it
- //back to it's original color.
- this.value = 0;
- if(this.x % 2 == 0){
- g.setColor(Color.black);
- }
- else{
- g.setColor(Color.WHITE);
- }
- }
- //Fill the oval information.
- g.fillOval(x, y, width, height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement