Nur die besten Spiele ;3
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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package TicTacToe;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. public class Cell extends JButton {
  9. public int playerID = 0;
  10. private TicTacToeGame ttt;
  11. public Cell(TicTacToeGame _ttt) {
  12. ttt = _ttt;
  13. setBackground(new Color(0));
  14. addActionListener(new ActionListener() {
  15. @Override
  16. public void actionPerformed(ActionEvent e) {
  17. OnMouseClick();
  18. }
  19. });
  20. }
  21. protected void OnMouseClick() {
  22. if (playerID == 0) {
  23. playerID = ttt.playerID;
  24. setEnabled(false);
  25. ttt.endTurn();
  26. repaint();
  27. }
  28. }
  29. @Override
  30. protected void paintComponent(Graphics g) {
  31. super.paintComponent(g);
  32. Graphics2D g2 = (Graphics2D) g;
  33. g2.setPaint(Color.white);
  34. switch (playerID) {
  35. case 1:
  36. g2.drawLine(5, 5, 90, 90);
  37. g2.drawLine(90, 5, 5, 90);
  38. break;
  39. case 2:
  40. g2.drawOval(5, 5, 90, 90);
  41. break;
  42. }
  43. }
  44. protected void reset() {
  45. playerID = 0;
  46. setEnabled(true);
  47. repaint();
  48. }
  49. }