I need help with this Java program. I need to make this program a tic tac toe, s
ID: 3825869 • Letter: I
Question
I need help with this Java program. I need to make this program a tic tac toe, so basically when one of the circle or the square is selected, the rest of the options are disabled on that given slot. I would also would like to know how to change the square for an X and the circle for a O. It must be the same program, I asked for help before and basically went online and found a code and added as a response. Thanks import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.util.*; public class ShowTicTac { public static void main(String[] args) { JFrame win = new JFrame(); win.setVisible(true); win.setLayout(new GridLayout(3,3, 5,5)); MyPanel [] tic = new MyPanel[9]; for(int i = 0; i < 9 ; i++) { tic[i] = new MyPanel();tic[i].myPanel.setBackground(new Color((int)(Math.random()*256), (int)(Math.random()*256),(int)(Math.random()*256))); win.add(tic[i]); } win.setSize(550,550); win.setResizable(false); } } class MyPanel extends JPanel { public CustomPanel myPanel; private JPanel jpButton; private JButton square; private JButton circle; private Color c; public void setColor(Color cc) { c = cc; myPanel.setBackground(c); } public MyPanel() { myPanel = new CustomPanel(); circle = new JButton("Circle"); square = new JButton("Square"); circle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myPanel.draw(CustomPanel.CIRCLE); } } ); square.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myPanel.draw(CustomPanel.SQUARE); } } ); jpButton = new JPanel(); jpButton.setSize(150,25); jpButton.setLayout(new GridLayout(1,2)); jpButton.add(circle);jpButton.add(square); this.setLayout(new BorderLayout()); this.add(myPanel,BorderLayout.CENTER); this.add(jpButton,BorderLayout.SOUTH); } public Dimension getPreferredSize() { return new Dimension(150,175); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paintComponent (Graphics g) { super.paintComponent(g); } } class CustomPanel extends JPanel { public static final int CIRCLE = 0; public static final int SQUARE = 1 ; private int shape ; public CustomPanel() { } public Dimension getPreferredSize() { return new Dimension(150,150); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); if(shape == 0) { g.fillOval(50 , 10 ,80, 80); } else g.fillRect(50,10,80,80); } public void draw(int choice) { shape = choice; repaint(); } }
Explanation / Answer
//Completed the disable feature
//Please cmment for any correction
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
public class ShowTicTac
{
public static void main(String[] args)
{
JFrame win = new JFrame();
win.setVisible(true);
win.setLayout(new GridLayout(3,3, 5,5));
MyPanel [] tic = new MyPanel[9];
for(int i = 0; i < 9 ; i++)
{
tic[i] =
new MyPanel();tic[i].myPanel.setBackground(new Color((int)(Math.random()*256),
(int)(Math.random()*256),(int)(Math.random()*256)));
win.add(tic[i]);
}
win.setSize(550,550);
win.setResizable(false);
}
}
class MyPanel extends JPanel implements ActionListener
{
public CustomPanel myPanel;
private JPanel jpButton;
private JButton square;
private JButton circle;
private Color c;
public void setColor(Color cc)
{
c = cc;
myPanel.setBackground(c);
}
public MyPanel()
{
myPanel = new CustomPanel();
circle = new JButton("Circle");
square = new JButton("Square");
circle.addActionListener(this);
square.addActionListener(this);
jpButton = new JPanel();
jpButton.setSize(150,25);
jpButton.setLayout(new GridLayout(1,2));
jpButton.add(circle);jpButton.add(square);
this.setLayout(new BorderLayout());
this.add(myPanel,BorderLayout.CENTER);
this.add(jpButton,BorderLayout.SOUTH);
}
public Dimension getPreferredSize()
{
return new Dimension(150,175);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(square)){
myPanel.draw(CustomPanel.SQUARE);
square.setEnabled(false);
circle.setEnabled(false);
}
if(e.getSource().equals(circle)){
myPanel.draw(CustomPanel.CIRCLE);
circle.setEnabled(false);
square.setEnabled(false);
}
}
}
class CustomPanel extends JPanel
{
public static final int CIRCLE = 0;
public static final int SQUARE = 1 ;
private int shape ;
public CustomPanel()
{
}
public Dimension getPreferredSize()
{
return new Dimension(150,150);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
if(shape == 0)
{
g.fillOval(50 , 10 ,80, 80);
}
else
g.fillRect(50,10,80,80);
}
public void draw(int choice)
{
shape = choice;
repaint();
}
}