Pictionary.java
import objectdraw.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
// This plays a Pictionary game on 2 computers. The drawer draws on one
// computer, while the guesser makes guesses on a second computer.
public class Pictionary extends WindowController implements ActionListener {
// Size and location of the drawing area
private static final int DRAWING_WIDTH = 380;
private static final int DRAWING_HEIGHT = 290;
private static final int DRAWING_OFFSET = 10;
// Port for sending pictionary data
private static final int PICTIONARY_PORT = 1340;
// UI component that displays the text of the next card.
private JLabel cardLabel;
// UI component where the guesser enters the guess
private JTextField guess;
// UI component where the drawer sees the guess
private JLabel guessLabel;
// Remember starting point of the next line segment
private Location nextLineStarts;
// The deck of cards
private Cards deck;
// Remembers if the user can draw.
private boolean isDrawer = false;
// JPanel to add UI components to under the drawing area
private JPanel gamePanel = new JPanel();
// Buttons to identify which player will draw and which player will guess
private JButton drawerButton, guesserButton;
// Button to signal that the guesser got it right
private JButton rightButton;
// Panels seen by the drawer
private JPanel cardPanel, guessViewPanel, buttonPanel;
// JPanel seen by the guesser
private JPanel guessPanel;
// The drawer object
private Drawer drawer;
// Initialize the display with the drawing area and buttons to identify
// the drawer and the guesser.
public void begin () {
// Draw a border around the drawing area.
new FramedRect (DRAWING_OFFSET, DRAWING_OFFSET,
DRAWING_WIDTH, DRAWING_HEIGHT, canvas);
// Draw the common parts of the drawer and guesser UI
gamePanel.setLayout (new GridLayout (0, 1));
buttonPanel = new JPanel();
drawerButton = new JButton ("Drawer");
buttonPanel.add (drawerButton);
drawerButton.addActionListener (this);
guesserButton = new JButton ("Guesser");
buttonPanel.add (guesserButton);
guesserButton.addActionListener (this);
// Create the drawer and guesser specific parts of the UI, but keep
// them hidden initially.
createGuesserDisplay();
createDrawerDisplay();
// Put the UI on the screen.
add (gamePanel, BorderLayout.SOUTH);
}
// Initialize the guesser's display. The guesser has a text field
// to make guesses in. This is not shown initially.
public void createGuesserDisplay () {
guessPanel = new JPanel();
guessPanel.add (new JLabel ("Guess: "));
guess = new JTextField (20);
guessPanel.add (guess);
gamePanel.add (guessPanel);
guessPanel.setVisible(false);
}
// Show the guesser specific parts of the UI
public void guesserDisplay() {
guessPanel.setVisible(true);
}
// Iniitialize the drawer's display. The drawer has a field showing
// the card so the drawing user knows what to draw. A second field shows
// the guess made by the guesser. There is also a button for the drawer
// to click when the guesser guesses right. These are all hidden initially.
public void createDrawerDisplay () {
// Create the display
cardPanel = new JPanel();
cardPanel.add (new JLabel ("Card: "));
cardLabel = new JLabel (" ");
cardPanel.add (cardLabel);
gamePanel.add (cardPanel);
guessViewPanel = new JPanel();
guessViewPanel.add (new JLabel ("Guess: "));
guessLabel = new JLabel (" ");
guessViewPanel.add (guessLabel);
gamePanel.add (guessViewPanel);
rightButton = new JButton ("Right");
buttonPanel.add (rightButton);
rightButton.addActionListener (this);
gamePanel.add (buttonPanel);
cardPanel.setVisible(false);
guessViewPanel.setVisible (false);
rightButton.setVisible(false);
}
// Show the UI components for the drawer.
public void drawerDisplay() {
cardPanel.setVisible(true);
guessViewPanel.setVisible(true);
rightButton.setVisible(true);
// Create the deck of cards and show the first card.
deck = new Cards ();
cardLabel.setText (deck.nextCard());
this.validate();
}
// Handle clicks of the drawer, guesser, and right buttons.
public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == drawerButton) {
// When the drawer button is clicked, hide the drawer and
// guesser buttons and make the drawer UI components visible.
drawerButton.setVisible(false);
guesserButton.setVisible(false);
drawerDisplay();
buttonPanel.validate();
// Remember that this user is allowed to draw.
isDrawer = true;
// Create the drawer and establish communication with the guesser.
drawer = new Drawer (PICTIONARY_PORT, guessLabel);
}
else if (evt.getSource() == guesserButton) {
// When the guesser button is clicked, hide the drawer and
// guesser buttons and make the guesser UI components visible.
drawerButton.setVisible(false);
guesserButton.setVisible(false);
guesserDisplay();
// Create the guess and establish communication with the drawer.
new Guesser (PICTIONARY_PORT, canvas, guess);
}
else if (evt.getSource() == rightButton) {
// When the write button is clicked, disable any more drawing.
// Close the connection between the drawer and the guesser.
isDrawer = false;
drawer.quit();
}
}
// Begin drawing when the user depresses the mouse button
public void onMousePress(Location point){
nextLineStarts = point;
}
// Draw a line segment as the user drags the mouse with the button down.
// Have the drawer transmit the segment to the guesser.
public void onMouseDrag(Location point){
if (isDrawer) {
Line newLine = new Line(nextLineStarts, point, canvas);
nextLineStarts = point;
drawer.writeLine (newLine);
}
}
}