Question
GameUi.java Update the class to add the following member variables JPanel trumpPanel (if you added bidPanel in the last assignment, please rename it) JLabel teamOneScoreLbl
GameUi.java | Update the class to add the following member variables JPanel trumpPanel (if you added bidPanel in the last assignment, please rename it) JLabel teamOneScoreLbl JLabel teamOneScore JLabel teamTwoScoreLbl JLabel teamTwoScore JLabel trumpCard |
Update the custom constructor to do the following Call method setGameUi() in class Game passing an instance to this class as an argument Call method play() in class Game Update initComponents to do the followingTrump PanelInstantiate the JPanel member variable for the trump panel Instantiate the JLabel member variable for the trump label Instantiate an instance of class CardUi passing as arguments to the constructor the followingJLabel member variable for the trump card The trump cards face value The trump cards suit Update the JLabel member variable for the trump to do the followingsetting it equal to method call getLabel on the instance of class CardUi add client property passing arguments face and the trump cards face value add client property passing arguments suit and the trump cards suit Add the trump card JLabel the trump panel Add the trump JPanel to the north JPanel Score PanelUse layout manager GridLayout so it is a 2 x 2 grid Instantiate the following member variables of class JLabelteamOneScoreLbl with text Team One teamOneScore with text using the following method call: game.getTeams().get(Constants.ONE).getTeamScore() teamTwoScoreLbl with text Team Two teamTwoScore with text using the following method call: game.getTeams().get(Constants.TWO).getTeamScore() Add the four JLabels to the score JPanel Add the score JPanel the the north JPanel For the four player JPanels, aiOnePanel, aiTwoPanel, aiThreePanel, and hpPanelAdd an additional argument to the constructor calls a reference to this class, GameUi |
previous code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package userinterface;
import constants.Constants;
import core.Card;
import core.Game;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author
*/
public class GameUi
{
// the game
private Game game;
// the layout
private JFrame frame;
private JPanel aiOnePanel;
private JPanel tablePanel;
private JPanel aiTwoPanel;
private JPanel hpPanel;
private JPanel aiThreePanel;
private JPanel northPanel;
private JPanel scorePanel;
private JPanel trumpPanel;
// the menu
private JMenuBar menuBar;
private JMenu gameMenu;
private JMenu helpMenu;
private JMenuItem newGameMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem aboutMenuItem;
private JMenuItem rulesMenuItem;
// other components
private JLabel teamOneScoreLbl;
private JLabel teamOneScore;
private JLabel teamTwoScoreLbl;
private JLabel teamTwoScore;
private JLabel trumpCard;
public GameUi(Game game)
{
this.game = game;
initComponents();
game.setGameUi(this);
game.play();
}
private void initComponents()
{
initMenuBar();
layoutTable();
}
private void initMenuBar()
{
menuBar = new JMenuBar();
// game menu
gameMenu = new JMenu("Game");
newGameMenuItem = new JMenuItem("New Game");
newGameMenuItem.addActionListener(new NewGameListener());
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new ExitListener());
// help menu
helpMenu = new JMenu("Help");
aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(new AboutListener());
rulesMenuItem = new JMenuItem("Game Rules");
rulesMenuItem.addActionListener(new RulesListener());
// put it all together
gameMenu.add(newGameMenuItem);
gameMenu.add(exitMenuItem);
helpMenu.add(aboutMenuItem);
helpMenu.add(rulesMenuItem);
menuBar.add(gameMenu);
menuBar.add(helpMenu);
}
private void layoutTable()
{
// create the jframe
frame = new JFrame("Euchre");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 700);
// create the player's panels
aiOnePanel = new AiPlayerUi(game.getTable().get(Constants.POSITION_2), Constants.POSITION_2);
aiTwoPanel = new AiPlayerUi(game.getTable().get(Constants.POSITION_3), Constants.POSITION_3);
aiThreePanel = new AiPlayerUi(game.getTable().get(Constants.POSITION_4), Constants.POSITION_4);
hpPanel = new HumanPlayerUi(game.getTable().get(Constants.POSITION_1));
game.getTable().get(Constants.POSITION_1).setUi(hpPanel);
game.getTable().get(Constants.POSITION_2).setUi(aiOnePanel);
game.getTable().get(Constants.POSITION_3).setUi(aiTwoPanel);
game.getTable().get(Constants.POSITION_4).setUi(aiThreePanel);
initNorthPanel();
initTablePanel();
// add UI components
frame.add(aiOnePanel, BorderLayout.WEST);
frame.add(northPanel, BorderLayout.NORTH);
frame.add(aiThreePanel, BorderLayout.EAST);
frame.add(hpPanel, BorderLayout.SOUTH);
frame.add(tablePanel, BorderLayout.CENTER);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
private void initNorthPanel()
{
northPanel = new JPanel();
northPanel.setMinimumSize(new Dimension(680, 170));
northPanel.setPreferredSize(new Dimension(680, 170));
initScorePanel();
initTrumpPanel();
aiTwoPanel.setMinimumSize(new Dimension(350, 160));
aiTwoPanel.setPreferredSize(new Dimension(350, 160));
northPanel.add(scorePanel);
northPanel.add(aiTwoPanel);
northPanel.add(trumpPanel);
}
private void initTablePanel()
{
tablePanel = new JPanel();
tablePanel.setBorder(BorderFactory.createTitledBorder("EUCHRE"));
tablePanel.setMaximumSize(new Dimension(300,200));
tablePanel.setMinimumSize(new Dimension(300,200));
tablePanel.setPreferredSize(new Dimension(300,200));
}
private void initScorePanel()
{
scorePanel = new JPanel();
scorePanel.setBorder(BorderFactory.createTitledBorder("Scores"));
scorePanel.setMinimumSize(new Dimension(130, 160));
scorePanel.setPreferredSize(new Dimension(130, 160));
scorePanel.setLayout(new GridLayout(2, 2));
// team one
teamOneScoreLbl = new JLabel("Team One");
teamOneScoreLbl.setHorizontalAlignment(JLabel.CENTER);
teamOneScore = new JLabel();
teamOneScore.setHorizontalAlignment(JLabel.CENTER);
teamOneScore.setText(String.valueOf(game.getTeams().get(Constants.ONE).getTeamScore()));
// team two
teamTwoScoreLbl = new JLabel("Team Two");
teamTwoScoreLbl.setHorizontalAlignment(JLabel.CENTER);
teamTwoScore = new JLabel();
teamTwoScore.setHorizontalAlignment(JLabel.CENTER);
teamTwoScore.setText(String.valueOf(game.getTeams().get(Constants.TWO).getTeamScore()));
scorePanel.add(teamOneScoreLbl);
scorePanel.add(teamOneScore);
scorePanel.add(teamTwoScoreLbl);
scorePanel.add(teamTwoScore);
}
private void initTrumpPanel()
{
trumpPanel = new JPanel();
trumpPanel.setBorder(BorderFactory.createTitledBorder("Trump"));
trumpPanel.setMinimumSize(new Dimension(130, 160));
trumpPanel.setPreferredSize(new Dimension(130, 160));
trumpPanel.setLayout(new BoxLayout(trumpPanel, BoxLayout.X_AXIS));
// trump card
trumpCard = new JLabel();
CardUi cardUi = new CardUi(trumpCard, game.getTrump().getFace(), game.getTrump().getSuit());
trumpCard = cardUi.getLabel();
trumpCard.setMinimumSize(new Dimension(100, 160));
trumpCard.setPreferredSize(new Dimension(100, 160));
trumpCard.setMaximumSize(new Dimension(100, 160));
// add clientProperties
trumpCard.putClientProperty("face", game.getTrump().getFace());
trumpCard.putClientProperty("suit", game.getTrump().getSuit());
trumpPanel.add(trumpCard);
}
public Game getGame()
{
return game;
}
// inner classes
private class NewGameListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
}
}
private class ExitListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
int response = JOptionPane.showConfirmDialog(frame, "Confirm to exit Euchre?",
"Exit?", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION)
System.exit(0);
}
}
private class AboutListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
String message = "Euchre version 1.0 Summer 2018";
JOptionPane.showMessageDialog(frame, message);
}
}
private class RulesListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
}
}
}
Game:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package core;
/** * * @author */
import constants.Constants; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; import userinterface.GameUi;
public class Game { // member variables private Card trump; private Player leadPlayer; private Player dealer; private Player wonTrick; private int round; private ArrayList
private void outputTeams() { for(Team team : teams) { team.outputTeam(); } } private void setTable() { // players are set up so that team members sit across from each other // therefore the deal would be to TeamOne.PlayerOne, TeamTwo.PlayerTwo, // TeamOne.PlayerTwo, TeamTwo.PlayerTwo as an example table = new ArrayList(); // get the teams in the game Team teamOne = teams.get(Constants.ONE); Team teamTwo = teams.get(Constants.TWO); // get the players from each team Player teamOnePlayerOne = teamOne.getTeam().get(Constants.ONE); Player teamOnePlayerTwo = teamOne.getTeam().get(Constants.TWO); Player teamTwoPlayerOne = teamTwo.getTeam().get(Constants.ONE); Player teamTwoPlayerTwo = teamTwo.getTeam().get(Constants.TWO); // we want to explicitly dictate which seat each player is in so we are // using the add method that takes two arguments, one to set the position // in the ArrayList and the associated object at that position table.add(0, teamOnePlayerOne); table.add(1, teamTwoPlayerOne); table.add(2, teamOnePlayerTwo); table.add(3, teamTwoPlayerTwo); // System.out.println("************************"); // System.out.println("Players at the table are"); // System.out.println("************************"); // // for(Player player : table) // { // System.out.println(player.getName()); // } } private void setDealerAndLead() { // select the first dealer Random random = new Random(); dealerIdx = random.nextInt(Constants.NUM_PLAYERS); dealer = table.get(dealerIdx); // create an index to keep track of which player got the card; // reset when get to 3 // set the leadIdx based on which player was selected as the dealer and // add one to it if(dealerIdx < 3) leadIdx = dealerIdx + 1; else leadIdx = 0; leadPlayer = table.get(leadIdx); } private void dealHand() { setDealerAndLead(); // // System.out.println("********************************"); // System.out.println(" DEALING THE HAND"); // System.out.println("********************************"); // // System.out.println("Player " + dealer.getName() + " will deal the hand");
int playerIdx = leadIdx; // loop through the shuffled deck and deal five cards to each player // first round, two at a time // second round, three at a time Iterator
for(int p = 0; p < Constants.NUM_PLAYERS; p++) { dealOne(playerIdx, currentCard); // increment the player index until value of 3, then reset to 0 if(playerIdx == 3) playerIdx = 0; else playerIdx++; }
// System.out.println("********************************"); // System.out.println(" SECOND DEAL, THREE CARDS EACH"); // System.out.println("********************************");
for(int p = 0; p < Constants.NUM_PLAYERS; p++) { dealTwo(playerIdx, currentCard); // increment the player index until value of 3, then reset to 0 if(playerIdx == 3) playerIdx = 0; else playerIdx++; } // set trump to next card on deck trump = currentCard.next(); // System.out.println("********************************"); // System.out.println("Trump card is " + trump.getFace() + " of " + // trump.getSuit() + " color " + trump.getColor()); // System.out.println("********************************"); }
public Card getTrump() { return trump; } private void dealOne(int playerIdx, Iterator
// System.out.println("Dealing " + card.getFace() + " of " + // card.getSuit() + " to player " + // table.get(playerIdx).getName()); // add card to a player's hand table.get(playerIdx).getHand().add(card); // remove the card from the deck after it has been dealt currentCard.remove(); } } } private void dealTwo(int playerIdx, Iterator
// System.out.println("Dealing " + card.getFace() + " of " + // card.getSuit() + " to player " + // table.get(playerIdx).getName()); // add card to a player's hand table.get(playerIdx).getHand().add(card);
// remove the card from the deck after it has been dealt currentCard.remove(); } } } private void displayHands() { for(Team team : teams) { team.outputHands(); } } private void createDeck() { deck = new Deck(); } public void play() { for(Player player : table) { player.makeTrump(); } } public void setGameUi(GameUi ui) { this.ui = ui; } /** * @return the wonTrick */ public Player getWonTrick() { return wonTrick; }
/** * @param wonTrick the wonTrick to set */ public void setWonTrick(Player wonTrick) { this.wonTrick = wonTrick; }
/** * @return the round */ public int getRound() { return round; }
/** * @param round the round to set */ public void setRound(int round) { this.round = round; }
/** * @return the leadPlayer */ public Player getLeadPlayer() { return leadPlayer; }
/** * @param leadPlayer the leadPlayer to set */ public void setLeadPlayer(Player leadPlayer) { this.leadPlayer = leadPlayer; }
/** * @return the dealer */ public Player getDealer() { return dealer; }
/** * @param dealer the dealer to set */ public void setDealer(Player dealer) { this.dealer = dealer; }
/** * @return the table */ public ArrayList
CardUi:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package userinterface;
import constants.Constants.Face; import constants.Constants.Suit; import core.Card;
import java.awt.Image; import java.io.FileNotFoundException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel;
/** * * @author */ public class CardUi { private Card card; private ImageIcon imageIcon; private JButton button; private JLabel label; private int position; public CardUi(Card card, JButton button) { this.card = card; this.button = button; selectFrontImage(this.button); } public CardUi(Card card, JLabel label, int position) { this.card = card; this.label = label; this.position = position; if(position == 1 || position == 3) selectVerticalBackImage(); else selectHorizontalBackImage(); }
public CardUi(Card card, JLabel label) { this.card = card; this.label = label; selectFrontImage(this.label); }
public CardUi(JLabel label) { this.label = label; selectHorizontalBackImage(); }
public CardUi(JLabel label, Face face, Suit suit) { this.label = label; Card card = new Card(); card.setFace(face); card.setSuit(suit); this.card = card; selectFrontImage(label); } private void selectFrontImage(JComponent component) { String fileName = "../images/"; switch(card.getFace()) { case ACE: fileName += "Ace"; break; case NINE: fileName += "Nine"; break; case TEN: fileName += "Ten"; break; case JACK: fileName += "Jack"; break; case QUEEN: fileName += "Queen"; break; case KING: fileName += "King"; } switch(card.getSuit()) { case CLUBS: fileName += "Clubs"; break; case HEARTS: fileName += "Hearts"; break; case DIAMONDS: fileName += "Diamonds"; break; case SPADES: fileName += "Spades"; } fileName += ".png"; //System.out.println("image file name is " + fileName); try { URL imgURL = getClass().getResource(fileName); //System.out.println("image file name is " + fileName); if(imgURL != null) { imageIcon = new ImageIcon(imgURL); imageIcon = imageResizeHorizontal(imageIcon); } if(component instanceof JLabel) label = new JLabel(imageIcon); else button = new JButton(imageIcon); } catch(Exception ex) { System.err.println("Couldn't find file: " + fileName); imageIcon = null; } } private void selectVerticalBackImage() { String fileName = "../images/backVertical.jpg"; try { URL imgURL = getClass().getResource(fileName);
//System.out.println("image file name is " + fileName); if(imgURL != null) { imageIcon = new ImageIcon(imgURL); imageIcon = imageResizeVertical(imageIcon); label = new JLabel(imageIcon); } } catch(Exception ex) { System.err.println("Couldn't find file: " + fileName); imageIcon = null; } } private void selectHorizontalBackImage() { String fileName = "../images/backHorizontal.jpg"; try { URL imgURL = getClass().getResource(fileName); if(imgURL != null) { imageIcon = new ImageIcon(imgURL); imageIcon = imageResizeHorizontal(imageIcon); label = new JLabel(imageIcon); } } catch(Exception ex) { System.err.println("Couldn't find file: " + fileName); imageIcon = null; } }
private ImageIcon imageResizeHorizontal(ImageIcon icon) { Image image = icon.getImage(); Image newImage = image.getScaledInstance(70, 100, java.awt.Image.SCALE_SMOOTH); icon = new ImageIcon(newImage); return icon; }
private ImageIcon imageResizeVertical(ImageIcon icon) { Image image = icon.getImage(); Image newImage = image.getScaledInstance(100, 70, java.awt.Image.SCALE_SMOOTH); icon = new ImageIcon(newImage); return icon; } /** * @return the button */ public JButton getButton() { return button; }
/** * @return the button */ public JLabel getLabel() { return label; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started