(Response to comments: no claases provided for NBATeam and NBAcourtPanel. I beli
ID: 3858232 • Letter: #
Question
(Response to comments: no claases provided for NBATeam and NBAcourtPanel. I believe it has to be written from scratch.)
Write a java program to simulate showing an NBA Team on a Basketball court. Here is an example of the screenshot when running the program:
An example of the JFrame subclass with main method is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NBAPlayoff extends JFrame {
private JTextField txtName;
private JTextField txtAge;
private NBATeam spurs;
private NBAcourtPanel court;
private JLabel lMax, lMin, lAvg, lNum;
public NBAPlayoff(){
spurs=new NBATeam("Spurs");
court=new NBAcourtPanel(spurs);
add(court, BorderLayout.CENTER);
JLabel lMax0=new JLabel("Max Age:");
lMax=new JLabel("");
JLabel lMin0=new JLabel("Min Age:");
lMin=new JLabel("");
JLabel lAvg0=new JLabel("Average Age:");
lAvg=new JLabel("");
JLabel lNum0=new JLabel("Number of Players:");
lNum =new JLabel("");
JPanel rp=new JPanel(new GridLayout(8, 1)); //right panel
rp.add(lNum0);rp.add(lNum);rp.add(lMax0);rp.add(lMax);
rp.add(lMin0);rp.add(lMin);rp.add(lAvg0);rp.add(lAvg);
add(rp, BorderLayout.EAST);
JLabel l1=new JLabel("Player Name:");
txtName= new JTextField();
txtName.setPreferredSize(new Dimension(120,24));
JLabel l2=new JLabel("Player Age:");
txtAge= new JTextField();
txtAge.setPreferredSize(new Dimension(120,24));
JButton jbtAdd=new JButton("Add A Player");
jbtAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int age=Integer.parseInt(txtAge.getText());
spurs.addAPlayer(txtName.getText(), age);
lMax.setText(spurs.getMaxAge()+"");
lMin.setText(spurs.getMinAge()+"");
lAvg.setText(spurs.getAvgAge()+"");
lNum.setText(spurs.getNumOfPlayer()+"");
court.repaint();
}});
JButton jbtClear= new JButton("Clear");
jbtClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtName.setText("");
txtAge.setText("");
}});
JPanel pBot=new JPanel();
pBot.add(l1); pBot.add(txtName); pBot.add(l2);pBot.add(txtAge); pBot.add(jbtAdd);pBot.add(jbtClear);
add(pBot, BorderLayout.SOUTH);
}
public static void main(String[] args) {
NBAPlayoff frame = new NBAPlayoff();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(800, 400);
frame.setVisible(true);
}
}
Number of Players: Max Age Tony Parker K. Leonared T. Splitter T. Duncan M. Ginobili 32 Min Age Age 21 Average Age 28 Player Name: MGinobill Player Age Add A Player ClearExplanation / Answer
//NBAPlayoff
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NBAPlayoff extends JFrame {
private JTextField txtName;
private JTextField txtAge;
private NBATeam spurs;
private NBAcourtPanel court;
private JLabel lMax, lMin, lAvg, lNum;
public NBAPlayoff() {
spurs = new NBATeam("Spurs");
court = new NBAcourtPanel(spurs);
add(court, BorderLayout.CENTER);
JLabel lMax0 = new JLabel("Max Age:");
lMax = new JLabel("");
JLabel lMin0 = new JLabel("Min Age:");
lMin = new JLabel("");
JLabel lAvg0 = new JLabel("Average Age:");
lAvg = new JLabel("");
JLabel lNum0 = new JLabel("Number of Players:");
lNum = new JLabel("");
JPanel rp = new JPanel(new GridLayout(8, 1)); // right panel
rp.add(lNum0);
rp.add(lNum);
rp.add(lMax0);
rp.add(lMax);
rp.add(lMin0);
rp.add(lMin);
rp.add(lAvg0);
rp.add(lAvg);
add(rp, BorderLayout.EAST);
JLabel l1 = new JLabel("Player Name:");
txtName = new JTextField();
txtName.setPreferredSize(new Dimension(120, 24));
JLabel l2 = new JLabel("Player Age:");
txtAge = new JTextField();
txtAge.setPreferredSize(new Dimension(120, 24));
JButton jbtAdd = new JButton("Add A Player");
jbtAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int age = Integer.parseInt(txtAge.getText());
spurs.addAPlayer(txtName.getText(), age);
lMax.setText(spurs.getMaxAge() + "");
lMin.setText(spurs.getMinAge() + "");
lAvg.setText(spurs.getAvgAge() + "");
lNum.setText(spurs.getNumOfPlayer() + "");
court.repaint();
}
});
JButton jbtClear = new JButton("Clear");
jbtClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtName.setText("");
txtAge.setText("");
}
});
JPanel pBot = new JPanel();
pBot.add(l1);
pBot.add(txtName);
pBot.add(l2);
pBot.add(txtAge);
pBot.add(jbtAdd);
pBot.add(jbtClear);
add(pBot, BorderLayout.SOUTH);
}
private void add(NBAcourtPanel court2, String center) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
NBAPlayoff frame = new NBAPlayoff();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(800, 400);
frame.setVisible(true);
}
}
//NBATeam Class
import java.util.ArrayList;
import java.util.Collections;
public class NBATeam {
String name;
int age;
ArrayList<String> nameArray = new ArrayList<>();
ArrayList<Integer> ageArray = new ArrayList<>();
private String maxAge, minAge, avgAge, numOfPlayer;
public NBATeam(String a) {
}
public void addAPlayer(String name, int age) {
nameArray.add(name);
ageArray.add(age);
maxAge = Collections.max(ageArray)+"";
minAge = Collections.min(ageArray)+"";
avgAge = ( Integer.parseInt(maxAge) + Integer.parseInt(minAge))/ 2 +"";
numOfPlayer = nameArray.size()+"";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMaxAge() {
return maxAge;
}
public void setMaxAge(String maxAge) {
this.maxAge = maxAge;
}
public String getMinAge() {
return minAge;
}
public void setMinAge(String minAge) {
this.minAge = minAge;
}
public String getAvgAge() {
return avgAge;
}
public void setAvgAge(String avgAge) {
this.avgAge = avgAge;
}
public String getNumOfPlayer() {
return numOfPlayer;
}
public void setNumOfPlayer(String numOfPlayer) {
this.numOfPlayer = numOfPlayer;
}
}
//NBAcourtPanel
public class NBAcourtPanel {
NBATeam spurs;
public NBAcourtPanel(NBATeam spurs) {
// TODO Auto-generated constructor stub
}
public void repaint(){
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.