Reputation: 65
public static void main(String[] arguments){
TicTac frame = new TicTac();
}
The second TicTac says cannot find symbol and asks to create constructor TicTac();
.
My constructer is as follows:
public TicTac(TicTac in) {
}
I have tried
TicTac frame = new TicTac(TicTac in);
Here is the whole code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import tictactoe.TicTac;
public class TicTac extends JFrame implements ItemListener, ActionListener, Runnable{
TicTac gui;
Thread playing;
TicTacEvent tictac = new TicTacEvent(this);
JPanel row1 = new JPanel();
JButton[][] boxes = new JButton[3][3];
JButton play = new JButton("Play");
JTextField blank1 = new JTextField();
JTextField blank2 = new JTextField();
JOptionPane win = new JOptionPane("Winner");
ImageIcon back = new ImageIcon("cardback.jpg");
ImageIcon a = new ImageIcon("x.jpg");
ImageIcon b = new ImageIcon("o.jpg");
int clicks = 0;
//int win = 0;
int winX = 0;
int winO = 0;
int tie = 0;
int[][] check = new int[3][3];
public TicTac(TicTac in) {
super ("Tic Tac Toe");
gui = in;
setSize (500,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout();
setLayout(layout);
int name = 0;
String newname;
for (int row=0; row<=2;row++){
for(int col=0; col<=2; col++){
check[row][col]=0;
}
}
GridLayout layout1 = new GridLayout(4, 3, 10, 10);
row1.setLayout(layout1);
for (int x=0; x<=2; x++){
for (int y=0; y<=2; y++){
name = name + 1;
newname = Integer.toString(name);
boxes[x][y] = new JButton(newname);
boxes[x][y].setIcon(back);
row1.add(boxes[x][y]);
}
}
row1.add(blank1);
row1.add(play);
row1.add(blank2);
add (row1);
play.addActionListener(tictac);
for (int x=0; x<=2; x++){
for (int y=0; y<=2; y++){
boxes[x][y].addActionListener(tictac);
}
}
setVisible(true);
}
public static void main(String[] arguments){
TicTac frame = new TicTac();
}
public void itemStateChanged(ItemEvent ie) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Play")) {
startPlaying();
}
if (command.equals("1")) {
b1();
}
if (command.equals("2")) {
b2();
}
if (command.equals("3")) {
b3();
}
if (command.equals("4")) {
b4();
}
if (command.equals("5")) {
b5();
}
if (command.equals("6")) {
b6();
}
if (command.equals("7")) {
b7();
}
if (command.equals("8")) {
b8();
}
if (command.equals("9")) {
b9();
}
}
void b1() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][0].setIcon(a);
check[0][0] = 1;
} else {
gui.boxes[0][0].setIcon(b);
check[0][0] = 2;
}
winner();
}
void b2() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][1].setIcon(a);
check[0][1] = 1;
} else {
gui.boxes[0][1].setIcon(b);
check[0][1] = 2;
}
winner();
}
void b3() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][2].setIcon(a);
check[0][2] = 1;
} else {
gui.boxes[0][2].setIcon(b);
check[0][2] = 2;
}
winner();
}
void b4() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][0].setIcon(a);
check[1][0] = 1;
} else {
gui.boxes[1][0].setIcon(b);
check[1][0] = 2;
}
winner();
}
void b5() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][1].setIcon(a);
check[1][1] = 1;
} else {
gui.boxes[1][1].setIcon(b);
check[1][1] = 2;
}
winner();
}
void b6() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][2].setIcon(a);
check[1][2] = 1;
} else {
gui.boxes[1][2].setIcon(b);
check[1][2] = 2;
}
winner();
}
void b7() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][0].setIcon(a);
check[2][0] = 1;
} else {
gui.boxes[2][0].setIcon(b);
check[2][0] = 2;
}
winner();
}
void b8() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][1].setIcon(a);
check[2][1] = 1;
} else {
gui.boxes[2][1].setIcon(b);
check[2][1] = 2;
}
winner();
}
void b9() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][2].setIcon(a);
check[2][2] = 1;
} else {
gui.boxes[2][2].setIcon(b);
check[2][2] = 2;
}
winner();
}
void winner() {
/** Check rows for winner */
for (int x=0; x<=2; x++){
if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2])) {
if (check[x][0]==1) {
JOptionPane.showMessageDialog(null, "X is the winner");
winX = 1;
System.out.println(Integer.toString(winX));
blank1.setText("Number of X Wins = " + (Integer.toString(winX)));
} else if (check[x][0]==2){
JOptionPane.showMessageDialog(null, "O is the winner");
winO = 1;
}
}
}
/** Check columns for winner */
for (int x=0; x<=2; x++){
if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])) {
if (check[0][x]==1) {
JOptionPane.showMessageDialog(null, "X is the winner");
winX = 1;
} else if (check[0][x]==2) {
JOptionPane.showMessageDialog(null, "O is the winner");
winO = 1;
}
}
}
/** Check diagonals for winner */
if (((check[0][0]==check[1][1])&&(check[0][0]==check[2][2]))||
((check[2][0]==check[1][1])&&(check[1][1]==check[0][2]))){
if (check[1][1]==1) {
JOptionPane.showMessageDialog(null, "X is the winner");
winX = 1;
} else if (check[1][1]==2) {
JOptionPane.showMessageDialog(null, "O is the winner");
winO = 1;
}
}
/** Checks if the game is a tie */
/* if ((clicks==9) && (win==0)) {
JOptionPane.showMessageDialog(null, "The game is a tie");
}
*/}
void startPlaying() {
playing = new Thread(this);
playing.start();
gui.play.setEnabled(false);
}
public void run() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
To fix my problem I created a separate class. Class one:
import java.awt.*;
import javax.swing.*;
public class TicTac extends JFrame {
TicTacEvent tictac = new TicTacEvent(this);
JPanel row1 = new JPanel();
JButton[][] boxes = new JButton[3][3];
JButton play = new JButton("Play");
JTextField blank1 = new JTextField();
JTextField blank2 = new JTextField();
JOptionPane win = new JOptionPane("Winner");
ImageIcon back = new ImageIcon("cardback.jpg");
public TicTac() {
super ("Tic Tac Toe");
setSize (500,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout();
setLayout(layout);
int name = 0;
String newname;
GridLayout layout1 = new GridLayout(4, 3, 10, 10);
row1.setLayout(layout1);
for (int x=0; x<=2; x++){
for (int y=0; y<=2; y++){
name = name + 1;
newname = Integer.toString(name);
boxes[x][y] = new JButton(newname);
boxes[x][y].setIcon(back);
row1.add(boxes[x][y]);
}
}
row1.add(blank1);
row1.add(play);
row1.add(blank2);
add (row1);
play.addActionListener(tictac);
for (int x=0; x<=2; x++){
for (int y=0; y<=2; y++){
boxes[x][y].addActionListener(tictac);
}
}
setVisible(true);
}
public static void main(String[] arguments){
TicTac frame = new TicTac();
}
}
Class 2:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TicTacEvent implements ItemListener, ActionListener, Runnable {
TicTac gui;
Thread playing;
ImageIcon a = new ImageIcon("x.jpg");
ImageIcon b = new ImageIcon("o.jpg");
int clicks = 0;
int win = 0;
int[][] check = new int[3][3];
public TicTacEvent (TicTac in){
gui = in;
for (int row=0; row<=2; row++){
for (int col=0; col<=2; col++){
check[row][col]=0;
}
}
}
public void actionPerformed (ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Play")) {
startPlaying();
}
if (command.equals("1")) {
b1();
}
if (command.equals("2")) {
b2();
}
if (command.equals("3")) {
b3();
}
if (command.equals("4")) {
b4();
} if (command.equals("5")) {
b5();
}
if (command.equals("6")) {
b6();
}
if (command.equals("7")) {
b7();
}
if (command.equals("8")) {
b8();
}
if (command.equals("9")) {
b9();
}
}
void b1() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][0].setIcon(a);
check[0][0] = 1;
} else {
gui.boxes[0][0].setIcon(b);
check[0][0] = 2;
}
winner();
}
void b2() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][1].setIcon(a);
check[0][1] = 1;
} else {
gui.boxes[0][1].setIcon(b);
check[0][1] = 2;
}
winner();
}
void b3() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[0][2].setIcon(a);
check[0][2] = 1;
} else { gui.boxes[0][2].setIcon(b);
check[0][2] = 2;
}
winner();
}
void b4() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][0].setIcon(a);
check[1][0] = 1;
} else {
gui.boxes[1][0].setIcon(b);
check[1][0] = 2;
}
winner();
}
void b5() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][1].setIcon(a);
check[1][1] = 1;
} else {
gui.boxes[1][1].setIcon(b);
check[1][1] = 2;
}
winner();
}
void b6() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[1][2].setIcon(a);
check[1][2] = 1;
} else {
gui.boxes[1][2].setIcon(b);
check[1][2] = 2;
}
winner();
}
void b7() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][0].setIcon(a);
check[2][0] = 1;
} else {
gui.boxes[2][0].setIcon(b);
check[2][0] = 2; }
winner();
}
void b8() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][1].setIcon(a);
check[2][1] = 1;
} else {
gui.boxes[2][1].setIcon(b);
check[2][1] = 2;
}
winner();
}
void b9() {
clicks = clicks + 1;
if ((clicks%2)==1){
gui.boxes[2][2].setIcon(a);
check[2][2] = 1;
} else {
gui.boxes[2][2].setIcon(b);
check[2][2] = 2;
}
winner();
}
void winner() {
/** Check rows for winner */
for (int x=0; x<=2; x++){
if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2])) {
if (check[x][0]==1) {
JOptionPane.showMessageDialog(null, "X is the winner");
win = 1;
} else if (check[x][0]==2){
JOptionPane.showMessageDialog(null, "Y is the winner");
win = 1;
}
}
}
/** Check columns for winner */
for (int x=0; x<=2; x++){
if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])) {
if (check[0][x]==1) {
JOptionPane.showMessageDialog(null, "X is the winner"); win = 1;
} else if (check[0][x]==2) {
JOptionPane.showMessageDialog(null, "Y is the winner");
win = 1;
}
}
}
/** Check diagonals for winner */
if (((check[0][0]==check[1][1])&&(check[0][0]==check[2][2]))||
((check[2][0]==check[1][1])&&(check[1][1]==check[0][2]))){
if (check[1][1]==1) {
JOptionPane.showMessageDialog(null, "X is the winner");
win = 1;
} else if (check[1][1]==2) {
JOptionPane.showMessageDialog(null, "Y is the winner");
win = 1;
}
}
/** Checks if the game is a tie */
if ((clicks==9) && (win==0)) {
JOptionPane.showMessageDialog(null, "The game is a tie");
}
}
void startPlaying() {
playing = new Thread(this);
playing.start();
gui.play.setEnabled(false);
}
public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void run() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Upvotes: 0
Views: 152
Reputation: 32680
Your constructor requires you to pass in an existing TicTac
as a parameter. In the code you show, you're trying to create a TicTac
without doing so.
As @MadProgrammer points out, requiring a TicTac
to create a TicTac
creates something of a catch-22 situation: How can you ever create one without having one first? If, for some reason, you do need to create a TicTac
with a reference to an existing TicTac
, you need to create an additional constructor with no parameters (as your compiler is suggesting) to create the first one:
public TicTac() { }
(more accurately, your constructor can take parameters, it just cant require a TicTac
):
class TicTac {
public TicTac(int size) { }
public TicTac(TicTac otherTicTac) { }
...
}
//elsewhere:
TicTac first = new TicTac(5); //or just TicTac first = new TicTac()
TicTac second = new TicTac(first);
However, I'm guessing that you don't really need a TicTac(TicTac otherTicTac)
constructor (if so, tell me why), and that you're trying to do so as a result of a misunderstanding about how constructors work - in which case this might be a helpful read, for starters.
Upvotes: 2