OnJava8-Examples/ui/TicTacToe.java
2015-12-15 11:47:04 -08:00

88 lines
2.7 KiB
Java

// ui/TicTacToe.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Dialog boxes and creating your own components.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static onjava.SwingConsole.*;
public class TicTacToe extends JFrame {
private JTextField
rows = new JTextField("3"),
cols = new JTextField("3");
private enum State { BLANK, XX, OO }
static class ToeDialog extends JDialog {
private State turn = State.XX; // Start with x's turn
ToeDialog(int cellsWide, int cellsHigh) {
setTitle("The game itself");
setLayout(new GridLayout(cellsWide, cellsHigh));
for(int i = 0; i < cellsWide * cellsHigh; i++)
add(new ToeButton());
setSize(cellsWide * 50, cellsHigh * 50);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class ToeButton extends JPanel {
private State state = State.BLANK;
public ToeButton() { addMouseListener(new ML()); }
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int
x1 = 0, y1 = 0,
x2 = getSize().width - 1,
y2 = getSize().height - 1;
g.drawRect(x1, y1, x2, y2);
x1 = x2/4;
y1 = y2/4;
int wide = x2/2, high = y2/2;
if(state == State.XX) {
g.drawLine(x1, y1, x1 + wide, y1 + high);
g.drawLine(x1, y1 + high, x1 + wide, y1);
}
if(state == State.OO)
g.drawOval(x1, y1, x1 + wide/2, y1 + high/2);
}
class ML extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
if(state == State.BLANK) {
state = turn;
turn =
(turn == State.XX ? State.OO : State.XX);
}
else
state =
(state == State.XX ? State.OO : State.XX);
repaint();
}
}
}
}
class BL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog d = new ToeDialog(
new Integer(rows.getText()),
new Integer(cols.getText()));
d.setVisible(true);
}
}
public TicTacToe() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
p.add(new JLabel("Rows", JLabel.CENTER));
p.add(rows);
p.add(new JLabel("Columns", JLabel.CENTER));
p.add(cols);
add(p, BorderLayout.NORTH);
JButton b = new JButton("go");
b.addActionListener(new BL());
add(b, BorderLayout.SOUTH);
}
public static void main(String[] args) {
run(new TicTacToe(), 200, 200);
}
}