You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.0 KiB
60 lines
1.0 KiB
package TicTacToe;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
public class Cell extends JButton {
|
|
|
|
public int playerID = 0;
|
|
private TicTacToeGame ttt;
|
|
|
|
public Cell(TicTacToeGame _ttt) {
|
|
ttt = _ttt;
|
|
setBackground(new Color(0));
|
|
|
|
addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
OnMouseClick();
|
|
}
|
|
});
|
|
}
|
|
|
|
protected void OnMouseClick() {
|
|
if (playerID == 0) {
|
|
playerID = ttt.playerID;
|
|
setEnabled(false);
|
|
ttt.endTurn();
|
|
repaint();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
Graphics2D g2 = (Graphics2D) g;
|
|
g2.setPaint(Color.white);
|
|
switch (playerID) {
|
|
case 1:
|
|
g2.drawLine(5, 5, 90, 90);
|
|
g2.drawLine(90, 5, 5, 90);
|
|
break;
|
|
case 2:
|
|
g2.drawOval(5, 5, 90, 90);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
protected void reset() {
|
|
playerID = 0;
|
|
setEnabled(true);
|
|
repaint();
|
|
}
|
|
|
|
}
|