/** * Draw a string * * @param font The font to draw with * @param s The text to draw * @param alignment The alignment to apply * @param x The x location to draw at * @param y The y location to draw at * @param width The width to fill with the string * @param color The color to draw in * @return The final x coordinate of the text */ public static final int drawString( Font font, final String s, final int alignment, final int x, final int y, final int width, Color color) { int resultingXCoordinate = 0; if (alignment == Alignment.LEFT) { font.drawString(x, y, s, color); } else if (alignment == Alignment.CENTER) { font.drawString(x + (width / 2) - (font.getWidth(s) / 2), y, s, color); } else if (alignment == Alignment.RIGHT) { font.drawString(x + width - font.getWidth(s), y, s, color); } else if (alignment == Alignment.JUSTIFY) { // calculate left width int leftWidth = width - font.getWidth(s); if (leftWidth <= 0) { // no width left, use standard draw string font.drawString(x, y, s, color); } return FontUtils.drawJustifiedSpaceSeparatedSubstrings( font, s, x, y, FontUtils.calculateWidthOfJustifiedSpaceInPixels(font, s, leftWidth)); } return resultingXCoordinate; }
@Override public void setPlayers(PlayerInfo[] value) { // set header label indicating how many players are still needed String labelText = ""; if (value.length == NUMBER_OF_PLAYERS) { labelText = "This game is ready to go!"; addAiButton.setEnabled(false); } else { labelText = ("Waiting for Players: Need " + (NUMBER_OF_PLAYERS - value.length) + " more"); addAiButton.setEnabled(true); } label.setText(labelText); // the center panel contains the individual player panels center.removeAll(); // build an individual player panel and add it to the center panel for (int i = 0; i < value.length; i++) { String builtString = (i + 1) + " " + value[i].getName(); JPanel playerPanel = new JPanel(); playerPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // left justify the text in the panel playerPanel.setPreferredSize(new Dimension(200, 50)); playerPanel.setBackground( value[i].getColor().getJavaColor()); // set the background color of the player JLabel playerLabel = new JLabel(builtString, SwingConstants.LEFT); // justify the text left FontUtils.setFont(playerLabel, LABEL_TEXT_SIZE); playerPanel.add(playerLabel); center.add(playerPanel); // add space between player panels Dimension minSize = new Dimension(5, 10); Dimension prefSize = new Dimension(5, 10); Dimension maxSize = new Dimension(Short.MAX_VALUE, 10); center.add(new Box.Filler(minSize, prefSize, maxSize)); } }
public PlayerWaitingView() { this.setOpaque(true); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH)); // set the heading at the top of the pane label = new JLabel("Player Waiting View"); FontUtils.setFont(label, LABEL_TEXT_SIZE); this.add(label, BorderLayout.NORTH); // create the center panel that displays player info center = new JPanel(); center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS)); this.add(center, BorderLayout.CENTER); // create the AI panel for the bottom of the pane aiPanel = new JPanel(); aiPanel.setLayout(new BoxLayout(aiPanel, BoxLayout.Y_AXIS)); // create the AI type panel JPanel aiTypePanel = new JPanel(); aiTypePanel.setLayout(new BoxLayout(aiTypePanel, BoxLayout.X_AXIS)); aiTypePanel.add(Box.createHorizontalGlue()); JLabel aiTypeLabel = new JLabel("Select AI Type:"); FontUtils.setFont(aiTypeLabel, AI_TEXT_SIZE); aiTypePanel.add(aiTypeLabel); aiTypePanel.add(Box.createRigidArea(new Dimension(5, 0))); aiModel = new SpinnerListModel(); aiChoices = new JSpinner(aiModel); ((JSpinner.DefaultEditor) aiChoices.getEditor()).getTextField().setEditable(false); FontUtils.setFont(aiChoices, AI_TEXT_SIZE); aiTypePanel.add(aiChoices); aiTypePanel.add(Box.createHorizontalGlue()); aiPanel.add(aiTypePanel); aiPanel.add(Box.createRigidArea(new Dimension(0, 10))); // create the AI button panel JPanel aiButtonPanel = new JPanel(); aiButtonPanel.setLayout(new BoxLayout(aiButtonPanel, BoxLayout.X_AXIS)); aiButtonPanel.add(Box.createHorizontalGlue()); addAiButton = new JButton("Add a computer player"); addAiButton.addActionListener(actionListener); FontUtils.setFont(addAiButton, BUTTON_TEXT_SIZE); aiButtonPanel.add(addAiButton); aiButtonPanel.add(Box.createHorizontalGlue()); aiPanel.add(aiButtonPanel); aiPanel.add(Box.createRigidArea(new Dimension(0, 10))); // add the AI panel this.add(aiPanel, BorderLayout.SOUTH); }