Write Java code for the following psudocode of the void populateGrid() function.
ID: 3777406 • Letter: W
Question
Write Java code for the following psudocode of the void populateGrid() function.
HistoryMenu
Void populateGrid()
Purpose: To fill out the HistoryMenu with each players history. 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”
inititializeHistoryLIst is called.
The function populateGrid is called to fill out the HistoryMenu grid using the list returned from intitializeHistoryList()
HistoryMenu is displayed with every history data of each player. Number of wins, losses, and win/loss ratio is displayed
Explanation / Answer
WidgetView.java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class WidgetView {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public WidgetView(){
populateGrid();
}
public static void main(String[] args){
WidgetView swingControlDemo = new WidgetView();
swingControlDemo.showLabelDemo();
}
private void populateGrid(){
mainFrame = new JFrame("Widget View");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showLabelDemo(){
JButton button=new JButton("opponent player vs player");
JButton button2=new JButton("player vs computer");
JButton button1=new JButton("View History");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ArrayList<Player> players=intitializeHistoryList();
JFrame frame=new JFrame("History");
frame.setSize(800,800);
JPanel jPanel1=new JPanel(new FlowLayout());
for(Player player:players){
JPanel panel = new JPanel(new GridLayout(4,2));
panel.add(new Label("Player:"));
panel.add(new Label(""+player.getName()));
panel.add(new Label("Wins:"));
panel.add(new Label(""+player.getWins()));
panel.add(new Label("Loss:"));
panel.add(new Label(""+player.getLoss()));
panel.add(new Label("Ratio:"));
panel.add(new Label(""+player.getRatio()));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel1.add(panel);
}
frame.add(jPanel1);
frame.setVisible(true);
}
});
JButton button3=new JButton("Quit");
controlPanel.add(button);
controlPanel.add(button2);
controlPanel.add(button1);
controlPanel.add(button3);
mainFrame.setVisible(true);
}
private ArrayList<Player> intitializeHistoryList(){
ArrayList<Player> players=new ArrayList<Player>();
players.add(new Player("Sachin",4, 3));
players.add(new Player("Kohli",5, 7));
return players;
}
}
Player.java
public class Player {
private String name;
private int wins;
private int loss;
public Player(String name, int wins, int loss) {
super();
this.name = name;
this.wins = wins;
this.loss = loss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLoss() {
return loss;
}
public void setLoss(int loss) {
this.loss = loss;
}
public float getRatio(){
return wins/((float)loss);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.