package boggle.view; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.event.*; import java.awt.FlowLayout; import java.awt.Graphics; import java.util.Iterator; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import mvc143.IBoggleController; import mvc143.IBoggleView; import boggle.model.*; import boggle.controller.*; /** * This is the visual aspect of the game of Boggle. The view * will have a reference to the game Controller, but not to the Model. * Use the controller's methods to access and manipulate data stored in IBoggleModel. * A shallow View might just print information on the console. A deep * View can be as fancy as you can imagine (within the the limits imposed by * the interface structure). *
* In the constructor, you have to take one and only one parameter, an IBoggleController. * through this reference, you can get communicate with the controller. *
* If the View is a GUI, then the constructor of the View should create the * JFrame and all other GUI components, keeping references to them if needed * for later use. *
* IMPORTANT NOTE #2 : * You must define these two public static fields to specify the author and a brief description. * as follows:
* public static final String author = " ... ";
* public static final String description = " ... "; *
"authors" should identify the authors in some fashion, but pseudonyms or nicknames are acceptable. "description" should give a brief description of the module, especially if there are any unexpected or unique features about it. The information in both fields may be visible to the general public, indefinitely into the future, so do not place any information here which you do not wish to be publicly available. * @author Thomas Patecky * */ public class BoggleView implements IBoggleView { public static final String author = "Tom and Jing"; public static final String description = "Class that displays boggle session in a GUI."; private IBoggleController theController; private BoggleFrame theFrame; private GamePanel theGamePanel; private ButtonPanel theButtonPanel; private StatPanel theStatPanel; private BoardMouseListener mouseListener; public BoggleView(IBoggleController controller){ theController = controller; theGamePanel = new GamePanel(theController); theButtonPanel = new ButtonPanel(theController, this); theStatPanel = new StatPanel(theController); theFrame = new BoggleFrame(theGamePanel, theButtonPanel, theStatPanel); theFrame.setTitle("Ultimate Boggle"); mouseListener = new BoardMouseListener(theController,GamePanel.PANEL_HEIGHT,GamePanel.PANEL_WIDTH,theController.getBoardCols(),theController.getBoardRows()); theGamePanel.addMouseListener(mouseListener); } /** * shows the information, at least the author and description of different modules */ public void showInfo(){ String credits = "Controller Written By: " + (String)BoggleController.author + "\n" + BoggleController.description + "\nModel Written By: " + BoggleModel.author + "\n" + BoggleModel.description + "\nView Written By: " + BoggleView.author + "\n" + BoggleView.description; JOptionPane.showMessageDialog(theFrame,credits,"Credits",JOptionPane.PLAIN_MESSAGE); } /** * This method will redisplay everything. This should only * be called from the Controller. The typical way redraw() should be implemented * (for a GUI) is for redraw to call repaint() on each of the main GUI components * which it has created. */ public void redraw(){ theGamePanel.repaint(); theButtonPanel.repaint(); theStatPanel.repaint(); } /** * Perform cleanup when the application is about to end. Should only * be called by the Controller. * (You don't neccessarily need to do anything, but it's an opportunity.) * */ public void terminateView(){ } } /** * Frame for boggle game * @author Thomas Patecky */ class BoggleFrame extends JFrame { private static final Color backgroundColor = Color.GRAY; /** * Creates the boggle game frame and adds the panels. * @param gPanel the panel with the game board * @param bPanel the panel with the buttons * @param sPanel the panel with the game statistics */ public BoggleFrame(GamePanel gPanel, ButtonPanel bPanel, StatPanel sPanel) { super(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contents = this.getContentPane(); contents.setLayout(new FlowLayout()); contents.setBackground(backgroundColor); contents.add(gPanel); contents.add(bPanel); contents.add(sPanel); this.pack(); this.show(); } } class BoardMouseListener implements MouseListener { IBoggleController theController; int rows; int columns; int pwidth; int pheight; public BoardMouseListener(IBoggleController theController,int rows,int columns,int pwidth,int pheight){ super(); this.theController = theController; this.rows = rows; this.columns = columns; this.pwidth = pwidth; this.pheight = pheight; } public void mouseClicked(MouseEvent e) { int x = (e.getX()*columns)/pwidth; int y = (e.getY()*rows)/pheight; theController.addToProposal(y,x); } public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} } /** * Panel contains the game board. When user clicks on a square, that letter is added to the proposal unless it is the last letter in the proposal, * in which case it is removed from the proposal. * @author Thomas Patecky */ class GamePanel extends JPanel { private IBoggleController theController; private int width; private int height; public static final int PANEL_WIDTH = 400; public static final int PANEL_HEIGHT = 400; /** * Creates the panel * @param a Boggle Controller object */ public GamePanel(IBoggleController theController) { this.theController = theController; setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT)); width = theController.getBoardCols(); height = theController.getBoardRows(); repaint(); } /** * Redraws game board * @param g, a Graphics object */ protected void paintComponent(Graphics g){ super.paintComponent(g); setBackground(Color.WHITE); g.setColor(Color.BLACK); for(int i=0;i