Question: The fist image is what my code is showing when running. The second image is what it should look like when running.What is wrong with

The fist image is what my code is showing when running. The second image is what it should look like when running.What is wrong with my code
package games.boards;
import javax.swing.JPanel;
import java.awt.*;
@SuppressWarnings("serial")
public class Board extends JPanel {
private Cell[][] cells;
public Board(int rows, int columns, ActionListener ah){
cells = new Cell[rows][columns];
this.setLayout(new GridLayout(3,3));
for( int r =0; r < cells.length; r++){
for (int c =0; c < cells[r].length; c++){
cells[r][c]= new Cell(r,c);
this.add(cells[r][c]);
cells[r][c].addActionListener(ah);
}
}
}
private void setLayout(GridLayout gridLayout){
// TODO Auto-generated method stub
}
public void setCell(Mark mark, int row, int column)
throws IllegalArgumentException {
try {
if (cells[row][column].getContent()== Mark.EMPTY){
cells[row][column].setContent(mark);
} else {
System.out.println("cell already occupied");
}
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("invalid cell selection");
}}
public Cell getCell(int row, int column){
return cells[row][column];
}
public String toString(){
StringBuilder str = new StringBuilder();
for( int r =0; r <3; r++){
str.append("|");
for (int c =0; c <3; c++){
switch(cells[r][c].getContent())
{
case NOUGHT:
str.append("0");
break;
case CROSS:
str.append("X");
break;
case YELLOW:
str.append("Y");
break;
case RED:
str.append("R");
break;
case BLUE:
str.append("B");
break;
case GREEN:
str.append("G");
break;
case MAGENTA:
str.append("M");
break;
case ORANGE:
str.append("O");
break;
default: //Empty
str.append("") ;
}
str.append("|");
}
str.append("
");
}
return str.toString();
}
}
package games.boards;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Color;
@SuppressWarnings("serial")
public class Cell extends JButton {
private Mark content;
private int row, column;
public Cell(int row, int column){
this.row = row;
this.column = column;
content = Mark.EMPTY;
}
public Mark getContent(){ return content; }
public void setContent(Mark content){
this.content = content;
}
public int getRow(){ return row; }
public int getColumn(){ return column; }
@Override
public void paintComponent(Graphics g){
//paint the basic button first
super.paintComponent(g);
int offset =5;
Graphics2D g2=(Graphics2D) g;
g2.setStroke(new BasicStroke(5));
// now paint 0 or X if required
switch(content){
case NOUGHT:
//Draw O
g2.setColor(Color.RED);
g2.drawOval(offset,offset,
this.getWidth()- offset *2,
this.getHeight()- offset *2);
break;
case CROSS:
//Draw X
g2.setColor(Color.BLACK);
g2.drawLine(offset, offset,
this.getWidth()- offset ,
this.getHeight()- offset );
g2.drawLine(this.getWidth()- offset,
offset, offset,
this.getHeight()- offset);
break;
default:
break;
}
}
public void addActionListener(ActionListener ah){
// TODO Auto-generated method stub
}
}
package games.boards;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import games.utilities.FileManager;
@SuppressWarnings({ "serial", "unused" })
public class BoardGameTester extends JFrame {
public static void main(String[] args){
SwingUtilities.invokeLater( new Runnable (){
public void run(){ new BoardGameTester(); }
});
}
private Board gb;
private int turn;
private void takeTurn(Cell c){
Mark curMark =(turn++%2==0)?
Mark.NOUGHT: Mark.CROSS;
gb.setCell(curMark,c.getRow(),c.getColumn());
}
private BoardGameTester(){
gb = new Board(3,3, new ActionListener(){
public void actionPerformed(ActionEvent ae){
Cell c =(Cell) ae.getSource();
takeTurn(c);
}
});
this.add(gb);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("TIC-TAC-TOE");
this.setSize(300,300);
this.setVisible(true);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!