Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HumanPlayerUi.java GameUi gameUi Add a second parameter of data type class GameU

ID: 3918571 • Letter: H

Question

HumanPlayerUi.java

GameUi gameUi

Add a second parameter of data type class GameUi

On the member variable of class HumanPlayer, call method setUi passing as an argument the parameter of class GameUi

HumanPlayer Ui 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 core.HumanPlayer;
import core.Player;
import constants.Constants;
import constants.Constants.Face;
import constants.Constants.Suit;
import core.Card;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
*
* @author
*/
public class HumanPlayerUi extends JPanel
{
private HumanPlayer human;
private ArrayList cards;
private CardUi cardUi;
private GameUi gameUi;
private JFrame parent;
private Suit suit;
  
public HumanPlayerUi(Player player, GameUi gameUi)
{
human = (HumanPlayer)player;
this.gameUi = gameUi;
  
initComponents();
}
  
private void initComponents()
{
this.setBorder(BorderFactory.createTitledBorder(human.getName()));
this.setMinimumSize(new Dimension(250, 150));
this.setPreferredSize(new Dimension(250, 150));
  
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

displayCards();
}
  
private void displayCards()
{   
cards = new ArrayList();

for(int c = 0; c < Constants.CARDS_EACH; c++)
{
// instantiate the JButton
JButton card = new JButton();
// update the JButton and format it
  
  
card = cardUi.getButton();
card.setMinimumSize(new Dimension(60,100));
card.setPreferredSize(new Dimension(60,100));
card.setBorder(BorderFactory.createLineBorder(Color.BLACK));
Object C = null;
  
//add clientProperties
card.putClientProperty("position", C);
card.putClientProperty("face", human.getHand.get(c).getFace());
card.putClientProperty("Suit", human.getHand.get(c).getSuit());
  
//register an ActionListener
card.addActionListerner(new CardListerner());
  
// add the object to the the ArrayList and UI
cards.add(card);
  
for(JButton button : cards)
this.add(button);
}
}

}

HumanPlayer:

/*
* 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;

import constants.Constants.Face;
import constants.Constants.Suit;
import userinterface.HumanPlayerUi;

/**
*
* @author
*/
public class HumanPlayer extends Player
{

public Object getHand;

@Override
public Card playCard() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


@Override
public void makeTrump()
{
}  
  

}

GameUi:

/*

* 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 constants.Constants.Face;

import constants.Constants.Suit;

import core.Card;

import core.Game;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import static java.time.Clock.system;

import java.util.ArrayList;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.JComponent;

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 teamnOneScore;

private JLabel teamTwoScoreLbl;

private JLabel teamTwoScore;

private JLabel trumpCard;

  

private ArrayList<Card>cardsPlayed;

private ArrayList<JLabel>cardsPlayedLbl;

  

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, this);

aiTwoPanel = new AiPlayerUi(game.getTable().get(Constants.POSITION_3), Constants.POSITION_3, this);   

aiThreePanel = new AiPlayerUi(game.getTable().get(Constants.POSITION_4), Constants.POSITION_4, this);

hpPanel = new HumanPlayerUi(game.getTable().get(Constants.POSITION_1), this);

  

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));

}

private void initTrumpPanel()

{

trumpPanel = new JPanel();

trumpPanel.setBorder(BorderFactory.createTittledBorder("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);

trumpCard = cardUi.getLabel();

trumpCard.setMinimumSize(new Dimension(100, 160));

trumpCard.setPreferredSize(new Dimension(100,160));

trumpCard.setMaximumSize(new Dimension(100,160));

  

//addclientProperties

trumpCard.putClientProperty("face", game.getTrump.getFace());

trumpCard.putClientProperty("suit", game.getTrump.getSuit());

  

trumpPanel.add(trumpCard);

}

public void updateTabelPanel(JComponent cmpt)

{

System.out.println("updatetablepanel");

Face face = (Constants.Face)cmpt.getClientProperty("face");

Suit suit = (Constants.Suit)cmpt.getClientProperty("suit");

  

JLabel cardLbl = new JLabel();

CardUi cardUi = new CardUi(cardLbl,face,suit);

cmpt = cardUi.getLabel();

  

Card card = new Card();

card.setFace(face);

card.setSuit(suit);

  

tablePanel.add(cmpt);

  

try

{

Thread.sleep(2000);

}

catch(Exception ex)

{

ex.printStackTrace();

}

  

//game.setSuitlead(suit);

//game.addPlayercard(card);

}

public void clearTablePanel()

{

System.out.println("Clearing table");

//for(int c = 0 ; tablePanel.getComponentCount();C++)

  

tablePanel.removeAll();

tablePanel.revalidate();

tablePanel.repaint();

}

public JFrame getFrame()

{

return frame;

}

public ArrayList<Card> getCardsPlayed()

{

return cardsPlayed;

}

public void addCardPlayed(Card card)

{

cardsPlayed.add(card);

}

public Game getGame()

{

return null;

  

}

// 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 Karin Whiting Summer 2018";

JOptionPane.showMessageDialog(frame, message);

}

  

}

  

private class RulesListener 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

{

public void ActionPerformed(ActionEvent ae)

}

  

}   

}

HumanPlayerUi.java

Update the class to add the following member variables

GameUi gameUi

Modify the custom constructor to do the following

Add a second parameter of data type class GameUi

On the member variable of class HumanPlayer, call method setUi passing as an argument the parameter of class GameUi

Explanation / Answer

Answer: See below:

1. Updated HumanPlayerUi class: HumanPlayerUi.java (See lines that are bold.)

-------------------------------------------------

package userinterface;

import core.HumanPlayer;
import core.Player;
import constants.Constants;
import constants.Constants.Face;
import constants.Constants.Suit;
import core.Card;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
*
* @author
*/
public class HumanPlayerUi extends JPanel {

    private HumanPlayer human;
    private ArrayList cards;
    private CardUi cardUi;
    private GameUi gameUi; //gameUi data member
    private JFrame parent;
    private Suit suit;

    //Parameterized constructor having second parameter as of GameUi type
    public HumanPlayerUi(Player player, GameUi gameUi) {
        human = (HumanPlayer) player;
        this.gameUi = gameUi; //GameUi parameter assigned to GameUi data member
        human.setUi(this.gameUi); //call to setUi() method of HumanPlayer
        initComponents();
    }

    private void initComponents() {
        this.setBorder(BorderFactory.createTitledBorder(human.getName()));
        this.setMinimumSize(new Dimension(250, 150));
        this.setPreferredSize(new Dimension(250, 150));

        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        displayCards();
    }

    private void displayCards() {
        cards = new ArrayList();

        for (int c = 0; c < Constants.CARDS_EACH; c++) {
// instantiate the JButton
            JButton card = new JButton();
// update the JButton and format it

            card = cardUi.getButton();
            card.setMinimumSize(new Dimension(60, 100));
            card.setPreferredSize(new Dimension(60, 100));
            card.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            Object C = null;

//add clientProperties
            card.putClientProperty("position", C);
            card.putClientProperty("face", human.getHand.get(c).getFace());
            card.putClientProperty("Suit", human.getHand.get(c).getSuit());

//register an ActionListener
            card.addActionListerner(new CardListerner());

// add the object to the the ArrayList and UI
            cards.add(card);

            for (JButton button : cards) {
                this.add(button);
            }
        }
    }

}

----------------------------------------------

2. Updated HumanPlayer class: HumanPlayer.java (See lines in bold.)

-------------------------------------------

package core;

import constants.Constants.Face;
import constants.Constants.Suit;
import userinterface.GameUi;
import userinterface.HumanPlayerUi;

/**
*
* @author
*/
public class HumanPlayer extends Player {

    public Object getHand;
    private GameUi gameUi; //new data member gameUi

    @Override
    public Card playCard() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void makeTrump() {
    }

    //new member function setUi
    public void setUi(GameUi gameUi) {
        this.gameUi = gameUi;
    }

}

---------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote