Write Java FX code for this pseudocode of the void initiliza
Write Java FX code for this pseudocode of the void initilizaHistoryList() function. (HistoryMenu of a game)
initializeHistoryList() : void
Purpose: To show the history of all players who logged in or played as guests. It will deserialize Player objects, get desired info from its History object and then serialize the Player object. Called by the constructor.
Pseudocode:
Ask user to to select opponent player vs player, player vs computer,view history, or quit.
User clicks on \"View History\"
initizalizeHistoryList() is called and a list of the Player object values from the Player map is loaded into a variable
The list of Player objects is returned
Solution
package players;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.text.StyleContext.SmallAttributeSet;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PlayerMain {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JRadioButton rdbtnNewRadioButton;
private JRadioButton rdbtnComputer;
public static Map<String,Player> pMap = new HashMap<String,Player>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PlayerMain window = new PlayerMain();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public PlayerMain() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 511, 363);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 495, 325);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel(\"Players History\");
lblNewLabel.setFont(new Font(\"Times New Roman\", Font.PLAIN, 24));
lblNewLabel.setBounds(182, 24, 155, 43);
panel.add(lblNewLabel);
JLabel lblPlayerName = new JLabel(\"Player Name :\");
lblPlayerName.setBounds(92, 99, 106, 14);
panel.add(lblPlayerName);
JLabel lblPlayerId = new JLabel(\"Player Id :\");
lblPlayerId.setBounds(92, 124, 106, 14);
panel.add(lblPlayerId);
JLabel lblPlayerAge = new JLabel(\"Player Age :\");
lblPlayerAge.setBounds(92, 149, 106, 14);
panel.add(lblPlayerAge);
rdbtnNewRadioButton = new JRadioButton(\"player\",true);
rdbtnNewRadioButton.setMnemonic(KeyEvent.VK_0);
rdbtnNewRadioButton.setBounds(151, 196, 71, 23);
rdbtnComputer = new JRadioButton(\"computer\");
rdbtnNewRadioButton.setMnemonic(KeyEvent.VK_1);
rdbtnComputer.setBounds(247, 196, 109, 23);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(rdbtnNewRadioButton);
group.add(rdbtnComputer);
panel.add(rdbtnComputer);
panel.add(rdbtnNewRadioButton);
textField = new JTextField();
textField.setBounds(182, 96, 144, 20);
panel.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(182, 121, 144, 20);
panel.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(180, 149, 144, 20);
panel.add(textField_2);
textField_2.setColumns(10);
JButton btnSubmit = new JButton(\"submit\");
btnSubmit.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if(textField_1.getText() !=null){
Player player = new Player();
player.setpName(textField.getText());
player.setpAge(textField_2.getText());
player.setpId(textField_1.getText());
if(rdbtnNewRadioButton.isSelected()){
player.setpOppent(\"player\");
}else{
player.setpOppent(\"computer\");
}
pMap.put(player.getpId(), player);
}
}
});
btnSubmit.setBounds(130, 249, 89, 23);
panel.add(btnSubmit);
JButton btnViewHistory = new JButton(\"view History\");
btnViewHistory.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
List<Player> playersList = initilizaHistoryList();
ViewHistory viewHistory = new ViewHistory(playersList);
}
});
btnViewHistory.setBounds(248, 249, 121, 23);
panel.add(btnViewHistory);
}
public List<Player> initilizaHistoryList() {
List<Player> players= new ArrayList<Player>();
List<Player> list = new ArrayList<Player>(pMap.values());
players.addAll(list);
return players;
}
}
package players;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class ViewHistory {
private JFrame frame;
/**
* Create the application.
*/
public ViewHistory(List<Player> players) {
initialize(players);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(List<Player> players) {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 262);
frame.getContentPane().add(panel);
panel.setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setText(\"\");
textPane.setBounds(10, 11, 414, 240);
panel.add(textPane);
if(players !=null && players.size()>0){
textPane.setText(players.toString());
}else{
textPane.setText(\"No players logged....................\");
}
frame.setVisible(true);
}
}
package players;
public class Player {
private String pName;
private String pId;
private String pAge;
private String pOppent;
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getpAge() {
return pAge;
}
public void setpAge(String pAge) {
this.pAge = pAge;
}
public String getpOppent() {
return pOppent;
}
public void setpOppent(String pOppent) {
this.pOppent = pOppent;
}
@Override
public String toString() {
return \"PlayerName=\" + pName + \",PlayerAge=\" + pAge
+ \",PlayerOpponent=\" + pOppent
+ \"\ \";
}
}







