Main Java class (adds the JPanel and displays it)
package ttc;
import java.awt.Color;
import javax.swing.JFrame;
public class Ttc {
public static void main(String[] args) {
JFrame frame = new JFrame("Tic Tac Toe 2.0");
frame.getContentPane().add(new ttcPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setBackground(Color.BLACK);
frame.pack();
}
}
JPanel class (creates the JPanel and adds components to display)
package ttc;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class ttcPanel extends JPanel{
private int boxX, boxY, clickCounter;
private Color box1, box2, box3, box4, box5, box6, box7, box8, box9;
public ttcPanel()
{
addMouseListener(new coordinateListener());
clickCounter = 0;
setPreferredSize(new Dimension(320, 410));
}
public class coordinateListener implements MouseListener
{
@Override
public void mouseClicked(MouseEvent e)
{
boxX = e.getX();
boxY = e.getY();
clickCounter++;
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
@Override
public void paintComponent(Graphics g)
{
if(clickCounter %2 != 0)
{
g.setColor(Color.RED);
}
else
{
g.setColor(Color.BLUE);
}
if(boxX >=10 && boxX <= 110 && boxY >=100 && boxY <=200)
{
g.fillRect(10, 100, 100, 100);
box1 = g.getColor();
}
if(boxX >=110 && boxX <= 210 && boxY >=100 && boxY <=200)
{
g.fillRect(110, 100, 100, 100);
box2 = g.getColor();
}
if(boxX >=210 && boxX <= 310 && boxY >=100 && boxY <=200)
{
g.fillRect(210, 100, 100, 100);
box3 = g.getColor();
}
if(boxX >=10 && boxX <= 110 && boxY >=200 && boxY <=300)
{
g.fillRect(10, 200, 100, 100);
box4 = g.getColor();
}
if(boxX >=110 && boxX <= 210 && boxY >=200 && boxY <=300)
{
g.fillRect(110, 200, 100, 100);
box5 = g.getColor();
}
if(boxX >=210 && boxX <= 310 && boxY >=200 && boxY <=300)
{
g.fillRect(210, 200, 100, 100);
box6 = g.getColor();
}
if(boxX >=10 && boxX <= 110 && boxY >=300 && boxY <=400)
{
g.fillRect(10, 300, 100, 100);
box7 = g.getColor();
}
if(boxX >=110 && boxX <= 210 && boxY >=300 && boxY <=400)
{
g.fillRect(110, 300, 100, 100);
box8 = g.getColor();
}
if(boxX >=210 && boxX <= 310 && boxY >=300 && boxY <=400)
{
g.fillRect(210, 300, 100, 100);
box9 = g.getColor();
}
g.setColor(Color.WHITE);
for(int x = 10; x<=210; x+=100)
{
for(int y = 100; y<=300; y+=100)
{
g.drawRect(x, y, 100, 100);
}
}
g.setFont(new Font("Helvetica", Font.PLAIN, 24));
g.setColor(Color.GREEN);
if(box1==Color.red && box2==Color.red && box3==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box4==Color.red && box5==Color.red && box6==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box7==Color.red && box8==Color.red && box9==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box1==Color.red && box4==Color.red && box7==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box2==Color.red && box5==Color.red && box8==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box3==Color.red && box6==Color.red && box9==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box1==Color.red && box5==Color.red && box9==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box3==Color.red && box5==Color.red && box7==Color.red)
{
g.drawString("The winner is: Red", 10, 50);
}
else if(box1==Color.blue && box2==Color.blue && box3==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box4==Color.blue && box5==Color.blue && box6==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box7==Color.blue && box8==Color.blue && box9==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box1==Color.blue && box4==Color.blue && box7==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box2==Color.blue && box5==Color.blue && box8==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box3==Color.blue && box6==Color.blue && box9==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box1==Color.blue && box5==Color.blue && box9==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
else if(box3==Color.blue && box5==Color.blue && box7==Color.blue)
{
g.drawString("The winner is: blue", 10, 50);
}
repaint();
}
}
See the program in action
No comments:
Post a Comment