Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

why is my code not showing up the x ' s and o ' s in my display? here is my code. package games.boards; import

why is my code not showing up the x's and o's in my display? here is my code.
package games.boards;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import games.utilities.FileManager;
import java.awt.event.ActionEvent;
@SuppressWarnings({ "serial", "unused" })
public class BoardGameTester extends JFrame {
private Board gb;
private int turn;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable()
{
public void run(){new BoardGameTester();
}
});}
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);
}
}
package games.boards;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Color;
import java.util.Scanner;
import java.awt.*;
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){
super.paintComponent(g);
int offset =5;
Graphics2D g2=(Graphics2D) g;
g2.setStroke(new BasicStroke(5));
switch (content){
case NOUGHT:
g.setColor(Color.RED);
g.drawOval(offset,offset,
this.getWidth()-offset,
this.getHeight()-offset);
break;
case CROSS:
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;
}
}
public void addActionListener(ActionListener ah){
}
}
package games.boards;
import javax.swing.*;
import java.awt.GridLayout;
@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);
}
}
}
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 {
throw new IllegalArgumentException("play already occupied");
}
} catch (IllegalArgumentException e){
System.out.println("invalid cell selection");
}
}
@Override
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;
public class GridLayout {
public GridLayout(){
// TODO Auto-generated constructor stub
}
public static void main(String[] args){
// TODO Auto-generated method stub
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions