Question: Java Project: The assignment is to take the TicTacToe class implement a turn order. So if you make one pannel a square, then the ablity
Java Project:
The assignment is to take the TicTacToe class implement a turn order.
So if you make one pannel a square, then the ablity to place a square in the other pannels should be blocked untill you make one pannel a circle.
here is the code.
ShowTicTac.java:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
public class ShowTicTac {
public static void main(String[] args)
{
JFrame MyFrame = new JFrame();
MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyFrame.setTitle("TIC TAC TOE");
MyFrame.setVisible(true);
MyFrame.setSize(550,550);
MyFrame.setResizable(false);
Border blackLine;
blackLine = BorderFactory.createLineBorder(Color.black);
MyFrame.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(Color.white);
tic[i].setBorder(blackLine);
MyFrame.add(tic[i]);
}
}
}
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 xWidth;
private int yHeight;
private Dimension d = new Dimension(150,150);
private int shape = 2 ;
public CustomPanel()
{
}
public Dimension getPreferredSize()
{
return new Dimension(150,150);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
xWidth = (int) d.getHeight();
yHeight = (int) d.getWidth();
if(shape == 0)
{
g.setColor(Color.BLUE);
g.fillOval( xWidth/3, yHeight/4 ,80, 80);
}
else if (shape == 1)
{
g.setColor(Color.RED);
g.fillRect(xWidth/3, yHeight/4,80,80);
}
}
public void draw(int choice)
{
shape = choice;
repaint();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
