/* * Created on 2004-7-28 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package boggle.view; import mvc143.*; import boggle.controller.*; import boggle.model.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Font; import javax.swing.*; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.BoxLayout; import java.awt.Container; /** * @author xm * */ public class BoggleView extends JFrame implements IBoggleView,ActionListener { public static final String author = "X. Miao"; public static final String description = "the view"; public static final String START_GAME = "startgame"; public static final String END_GAME = "endgame"; public static final String PROPOSAL = "doneproposal"; public static final String REPLAY = "replay"; public static final String SHOWINFO = "showinfo"; private IBoggleController controller; public BoggleView(IBoggleController controller){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.controller = controller; Container contents = this.getContentPane(); contents.setBackground(new Color(0.0f,0.4f,0.7f)); contents.add(new BoggleBoardPanel(controller),BorderLayout.WEST); JPanel upperPanel = new JPanel(); upperPanel.setBackground(new Color(0.0f,0.4f,0.7f)); upperPanel.add(new BoggleScorePanel(controller)); upperPanel.add(new BoggleTimerPanel(controller)); upperPanel.add(new BoggleStatusPanel(controller)); contents.add(upperPanel,BorderLayout.NORTH); JPanel rightPanel = new JPanel(); rightPanel.setBackground(new Color(0.0f,0.4f,0.7f)); rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS)); rightPanel.add(Box.createRigidArea(new Dimension(60,80))); JButton propose = new JButton("Try this word"); propose.setBackground(new Color(0.0f,0.4f,0.7f,0.2f)); propose.setAlignmentX(JButton.CENTER_ALIGNMENT); propose.setActionCommand(PROPOSAL); propose.addActionListener(this); rightPanel.add(propose); rightPanel.add(Box.createVerticalGlue()); JButton replay = new JButton("Replay it"); replay.setBackground(new Color(0.0f,0.4f,0.7f,0.2f)); replay.setAlignmentX(JButton.CENTER_ALIGNMENT); replay.setActionCommand(REPLAY); replay.addActionListener(this); rightPanel.add(replay); rightPanel.add(Box.createVerticalGlue()); JButton startGame = new JButton("New Game"); startGame.setBackground(new Color(0.0f,0.4f,0.7f,0.2f)); startGame.setAlignmentX(JButton.CENTER_ALIGNMENT); startGame.setActionCommand(START_GAME); startGame.addActionListener(this); rightPanel.add(startGame); rightPanel.add(Box.createVerticalGlue()); JButton endGame = new JButton("Quit"); endGame.setAlignmentX(JButton.CENTER_ALIGNMENT); endGame.setBackground(new Color(0.0f,0.4f,0.7f,0.2f)); endGame.setActionCommand(END_GAME); endGame.addActionListener(this); rightPanel.add(endGame); rightPanel.add(Box.createVerticalGlue()); JButton showinfo = new JButton("Info"); showinfo.setAlignmentX(JButton.CENTER_ALIGNMENT); showinfo.setBackground(new Color(0.0f,0.4f,0.7f,0.2f)); showinfo.setActionCommand(SHOWINFO); showinfo.addActionListener(this); rightPanel.add(showinfo); rightPanel.add(Box.createRigidArea(new Dimension(60,80))); contents.add(rightPanel,BorderLayout.EAST); this.setTitle("A simple Boggle game"); this.pack(); this.show(); } public void showInfo(){ JOptionPane.showMessageDialog(null, BoggleModel.description+" "+BoggleModel.author+" "+BoggleController.description+" "+BoggleController.author+" "+BoggleView.description+" "+BoggleView.author); } /* (non-Javadoc) * @see mvc143.IBoggleView#redraw() */ public void redraw() { this.repaint(); } public void paintComponents(Graphics oldg){ super.paintComponents(oldg); Graphics2D g = (Graphics2D)oldg; } /* (non-Javadoc) * @see mvc143.IBoggleView#terminateView() */ public void terminateView() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(BoggleView.START_GAME)){ controller.startGame(); repaint(); } else if (e.getActionCommand().equals(BoggleView.END_GAME)){ controller.terminateApplication(); repaint(); } else if (e.getActionCommand().equals(BoggleView.PROPOSAL)){ controller.submitProposal(); controller.newProposal(); repaint(); } else if (e.getActionCommand().equals(BoggleView.REPLAY)){ controller.startGame(null); repaint(); } else if (e.getActionCommand().equals(BoggleView.SHOWINFO)){ showInfo(); repaint(); } } } /** * @author xm * */ class BoggleBoardPanel extends JPanel implements ActionListener { public static final int PANEL_WIDTH = 400; public static final int PANEL_HEIGHT = 400; public static final Color DEFAULT_BG_COLOR = new Color(0.0f,0.4f,0.7f); public static final Color BTN_INITIAL_COLOR = Color.BLUE; public static final Color BTN_PRESSED_COLOR = new Color(0.0f,0.7f,0.8f); private IBoggleController controller; private BoggleLetterButton[][] letterbuttons; private int rows, cols; /** Create a Boggle board panel. * The panel shows a 2-D array of buttons, each of which has a letter. * @param controller the controller has to be told when a letter is selected * by pressing a button. */ public BoggleBoardPanel(IBoggleController controller) { if (controller == null) { throw new IllegalArgumentException("Controller must not be null"); } this.controller = controller; this.setBackground(DEFAULT_BG_COLOR); this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); this.rows = controller.getBoardRows(); this.cols = controller.getBoardCols(); //this.setLayout(null); //manage layout manually this.setLayout(new GridLayout(this.rows, this.cols)); this.letterbuttons = new BoggleLetterButton[rows][cols]; for (int r = 0; r < this.rows; r++) { for (int c = 0; c < this.cols; c++) { BoggleLetterButton newBtn = new BoggleLetterButton(r, c); letterbuttons[r][c] = newBtn; newBtn.addActionListener(this); newBtn.setBackground(BTN_INITIAL_COLOR); this.add(newBtn); } } } public void paintComponent(Graphics oldg) { super.paintComponent(oldg); Graphics2D g = (Graphics2D) oldg; if (controller.isNewProposal()) resetAllColors(); for (int r = 0; r < this.rows; r++) { for (int c = 0; c < this.cols; c++) { BoggleLetterButton aBtn = letterbuttons[r][c]; String text = "" + (char)controller.getLetterInBoard(r, c); aBtn.setFont(new Font("Times",Font.BOLD,24)); aBtn.setForeground(Color.WHITE); aBtn.setText(text); aBtn.repaint(); } } } public void actionPerformed(ActionEvent e) { if (controller.isRunning()) { BoggleLetterButton button = (BoggleLetterButton)(e.getSource()); Color lastColor = button.getBackground(); button.setBackground(BTN_PRESSED_COLOR); if (!controller.addToProposal(button.getRow(),button.getCol())) button.setBackground(lastColor); } } public void resetAllColors() { for (int r = 0; r < this.rows; r++) { for (int c = 0; c < this.cols; c++) { letterbuttons[r][c].setBackground(BTN_INITIAL_COLOR); } } } class BoggleLetterButton extends JButton { private int myRow, myCol; BoggleLetterButton(int row, int col) { this.myRow = row; this.myCol = col; } public int getRow() {return this.myRow;} public int getCol() {return this.myCol;} } } /** * @author xm * */ class BoggleScorePanel extends JPanel { public static final int SCORE_WIDTH = 150; public static final int SCORE_HEIGHT = 100; private static final int leftmargin = 5; private static final int topmargin = 20; private static final int linespacing = 30; //should base on font! private static final int scorex = leftmargin, scorey = topmargin; //location of the "score" message /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ IBoggleController controller; public BoggleScorePanel(IBoggleController controller) { this.controller = controller; this.setPreferredSize(new Dimension(SCORE_WIDTH, SCORE_HEIGHT)); this.setBackground(new Color(1.0f,1.0f,1.0f,0.4f)); } public void paintComponent(Graphics oldg) { super.paintComponent(oldg); Graphics2D g = (Graphics2D) oldg; int score = this.controller.getScore(); String scoreInfo = "Current score: \n"; g.setFont(new Font("Times",Font.ITALIC,16)); g.drawString(scoreInfo, scorex, scorey); g.setFont(new Font("Times",Font.BOLD,24)); g.setColor(new Color(0.3f,0.3f,0.7f)); g.drawString(""+score,scorex+20,scorey+40); } } /** * @author xm * */ class BoggleStatusPanel extends JPanel { public static final int STATUS_WIDTH = 200; public static final int STATUS_HEIGHT = 100; private static final int leftmargin = 5; private static final int topmargin = 20; private static final int linespacing = 30; //should base on font! private static final int statusx = leftmargin, statusy = topmargin; //location of the "score" message /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ IBoggleController controller; public BoggleStatusPanel(IBoggleController controller) { this.controller = controller; this.setPreferredSize(new Dimension(STATUS_WIDTH, STATUS_HEIGHT)); this.setBackground(new Color(1.0f,1.0f,1.0f,0.4f)); } public void paintComponent(Graphics oldg) { super.paintComponent(oldg); Graphics2D g = (Graphics2D) oldg; g.setFont(new Font("Times",Font.PLAIN,16)); String str = controller.getStatus(); String[] strs = str.split("\n"); for (int i=0;i