/** Creates the error area component. */ private void initErrorArea() { SimpleAttributeSet attribs = new SimpleAttributeSet(); StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT); StyleConstants.setFontFamily(attribs, errorPane.getFont().getFamily()); StyleConstants.setForeground(attribs, Color.RED); errorPane.setParagraphAttributes(attribs, true); errorPane.setPreferredSize(new Dimension(100, 50)); errorPane.setMinimumSize(new Dimension(100, 50)); errorPane.setOpaque(false); }
/** Creates the text area. */ private void initTextArea() { textArea.setOpaque(false); textArea.setEditable(false); StyledDocument doc = textArea.getStyledDocument(); MutableAttributeSet standard = new SimpleAttributeSet(); StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER); StyleConstants.setFontFamily(standard, textArea.getFont().getFamily()); StyleConstants.setFontSize(standard, 12); doc.setParagraphAttributes(0, 0, standard, true); parentWindow.addSearchFieldListener(this); }
/** * Sets the reason of a call failure if one occurs. The renderer should display this reason to the * user. * * @param reason the reason to display */ public void setErrorReason(final String reason) { if (!SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater( new Runnable() { public void run() { setErrorReason(reason); } }); return; } if (errorMessageComponent == null) { errorMessageComponent = new JTextPane(); JTextPane textPane = (JTextPane) errorMessageComponent; textPane.setEditable(false); textPane.setOpaque(false); StyledDocument doc = textPane.getStyledDocument(); MutableAttributeSet standard = new SimpleAttributeSet(); StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER); StyleConstants.setFontFamily(standard, textPane.getFont().getFamily()); StyleConstants.setFontSize(standard, 12); doc.setParagraphAttributes(0, 0, standard, true); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 0; constraints.gridy = 4; constraints.weightx = 1; constraints.weighty = 0; constraints.insets = new Insets(5, 0, 0, 0); add(errorMessageComponent, constraints); this.revalidate(); } errorMessageComponent.setText(reason); if (isVisible()) errorMessageComponent.repaint(); }
/** * Returns a JPanel that represents the mancala board using strategy pattern to insert style. * * @param strat concrete strategy * @return JPanel containing both users' pits as controllers */ public JPanel boardContextDoWork(Strategy strat) { this.s = strat; Color boardColor = s.getBoardColor(); Color fontColor = s.getFontColor(); Font font = s.getFont(); JPanel panCenter = new JPanel(); JPanel panLeft = new JPanel(); JPanel panRight = new JPanel(); panCenter.setLayout(new GridLayout(2, 6, 10, 10)); // B6 to B1 Controllers for (int i = 12; i > 6; i--) { final Pits temp = new Pits(i); final int pit = i; final JLabel tempLabel = new JLabel(temp); tempLabel.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if (Model.player == 1) { JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, "Player A's turn!"); } else if (model.data[pit] == 0) { JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, "Pit is Empty try another one."); } else { if (temp.pitShape.contains(e.getPoint())) { model.move(pit); // mutator undoBtn.setText("Undo : " + model.getUndoCounter()); model.display(); } } } }); JPanel tempPanel = new JPanel(new BorderLayout()); JTextPane textPane = new JTextPane(); textPane.setEditable(false); textPane.setBackground(boardColor); textPane.setForeground(fontColor); textPane.setFont(font); textPane.setText("B" + (i - 6)); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); tempPanel.add(textPane, BorderLayout.NORTH); tempPanel.add(tempLabel, BorderLayout.SOUTH); panCenter.add(tempPanel, BorderLayout.SOUTH); tempPanel.setBackground(boardColor); } // A1 to A6 Controllers for (int i = 0; i < 6; i++) { final Pits newPits = new Pits(i); JLabel label = new JLabel(newPits); final int pit = i; label.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if (Model.player == 2) { JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, "Player B's turn!"); } else if (model.data[pit] == 0) { JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, "Pit is Empty try another one."); } else { if (newPits.pitShape.contains(e.getPoint())) { model.move(pit); // mutator undoBtn.setText("Undo : " + model.getUndoCounter()); model.display(); } } } }); JPanel tempPanel = new JPanel(new BorderLayout()); tempPanel.add(label, BorderLayout.NORTH); JTextPane textPane = new JTextPane(); textPane.setBackground(boardColor); textPane.setForeground(fontColor); textPane.setFont(font); textPane.setEditable(false); textPane.setText("A" + (i + 1)); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); tempPanel.add(textPane, BorderLayout.SOUTH); tempPanel.setBackground(boardColor); panCenter.add(tempPanel, BorderLayout.SOUTH); } // left text pane JTextPane paneLeft = new JTextPane(); paneLeft.setBackground(boardColor); paneLeft.setForeground(fontColor); paneLeft.setFont(font); paneLeft.setEditable(false); paneLeft.setText("M\nA\nN\nC\nA\nL\nA\n \nB"); // right text pane JTextPane paneRight = new JTextPane(); paneRight.setBackground(boardColor); paneRight.setForeground(fontColor); paneRight.setFont(font); paneRight.setEditable(false); paneRight.setText("M\nA\nN\nC\nA\nL\nA\n \nA"); // Add text panes to left and right panels panLeft.setLayout(new BorderLayout()); panRight.setLayout(new BorderLayout()); panLeft.add(paneLeft, BorderLayout.WEST); panRight.add(paneRight, BorderLayout.EAST); panLeft.add(new JLabel(new Pits(13)), BorderLayout.EAST); panRight.add(new JLabel(new Pits(6)), BorderLayout.WEST); // add the 2 mancala panels and pit panel to larger displayPanel JPanel displayPanel = new JPanel(); displayPanel.add(panLeft, BorderLayout.WEST); displayPanel.add(panCenter, BorderLayout.CENTER); displayPanel.add(panRight, BorderLayout.EAST); // set color panCenter.setBackground(boardColor); panLeft.setBackground(boardColor); panRight.setBackground(boardColor); displayPanel.setBackground(boardColor); // return display panel which contains the containers and elements created return displayPanel; }
@Override public void actionPerformed(ActionEvent e) { licence_text = new String( "This program is free software: you can redistribute it and/or modify\n" + "it under the terms of the GNU General Public License as published by\n" + "the Free Software Foundation, either version 3 of the License, or\n" + "(at your option) any later version.\n" + "\n" + "This program is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with this program. If not, see <http://www.gnu.org/licenses/>.\n" + "\n\n\n"); try { InputStream ips = new FileInputStream(getClass().getResource("COPYING").getPath()); InputStreamReader ipsr = new InputStreamReader(ips); BufferedReader br = new BufferedReader(ipsr); String line; while ((line = br.readLine()) != null) { licence_text += line + '\n'; } br.close(); } catch (Exception e1) { } Dimension dimension = new Dimension(600, 400); JDialog licence = new JDialog(memento, "Licence"); JTextPane text_pane = new JTextPane(); text_pane.setEditable(false); text_pane.setPreferredSize(dimension); text_pane.setSize(dimension); StyledDocument doc = text_pane.getStyledDocument(); Style justified = doc.addStyle( "justified", StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE)); StyleConstants.setAlignment(justified, StyleConstants.ALIGN_JUSTIFIED); try { doc.insertString(0, licence_text, justified); } catch (BadLocationException ble) { System.err.println("Couldn't insert initial text into text pane."); } Style logicalStyle = doc.getLogicalStyle(0); doc.setParagraphAttributes(0, licence_text.length(), justified, false); doc.setLogicalStyle(0, logicalStyle); JScrollPane paneScrollPane = new JScrollPane(text_pane); paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); paneScrollPane.setPreferredSize(dimension); paneScrollPane.setMinimumSize(dimension); JPanel pan = new JPanel(); LayoutManager layout = new BorderLayout(); pan.setLayout(layout); pan.add(new JScrollPane(paneScrollPane), BorderLayout.CENTER); JButton close = new JButton("Fermer"); close.addActionListener(new ButtonCloseActionListener(licence)); JPanel button_panel = new JPanel(); FlowLayout button_panel_layout = new FlowLayout(FlowLayout.RIGHT, 20, 20); button_panel.setLayout(button_panel_layout); button_panel.add(close); pan.add(button_panel, BorderLayout.SOUTH); licence.add(pan); licence.pack(); licence.setLocationRelativeTo(memento); licence.setVisible(true); }