import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; /* * DO NOT CHANGE THIS CLASS * * This class runs the GUI for your field of 8 basketball tournament. * * Richard McKenna * Stony Brook University * for CSE 214 - HW 3 */ public class TournamentBracketFrame extends JFrame implements ActionListener { // THIS STORES AND MANIPULATES OUR TREE DATA private TournamentBracketManager treeManager = new TournamentBracketManager(); // GUI STUFF FOR THE NORTH OF THE SCREEN private JPanel northPanel = new JPanel(); private JButton startTournamentButton = new JButton("Start Tournament"); // WE'LL RENDER OUR TREE IN THIS GUY private BracketPanel bracketPanel = new BracketPanel(); /* * DO NOT CHANGE THIS METHOD * * Default Constructor, lays out the GUI and initializes the * tree with 8 teams. */ public TournamentBracketFrame() { super("Tournament Bracket Frame"); setExtendedState(MAXIMIZED_BOTH); setDefaultCloseOperation(EXIT_ON_CLOSE); layoutGUI(); initTournamentTeams(); } /* * DO NOT CHANGE THIS METHOD * * This method simply picks 8 college basketball teams to * include in our tournament. This array is given to the tree * manager. */ public void initTournamentTeams() { Team[] teams = new Team[8]; teams[0] = new Team("Duke", 25, 3); teams[1] = new Team("Kansas", 26, 3); teams[2] = new Team("Memphis", 28, 1); teams[3] = new Team("North Carolina", 27, 2); teams[4] = new Team("Stanford", 24, 4); teams[5] = new Team("Stony Brook", 6, 22); teams[6] = new Team("Tennessee", 26, 3); teams[7] = new Team("UCLA", 26,3); treeManager.initTeams(teams); } /* * DO NOT CHANGE THIS METHOD * * This method responds to user input. */ public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source == startTournamentButton) { initTournamentTeams(); treeManager.runTournament(); bracketPanel.repaint(); } } /* * DO NOT CHANGE THIS METHOD * * This method lays out our GUI components. */ public void layoutGUI() { northPanel.add(startTournamentButton); add(northPanel, BorderLayout.NORTH); add(bracketPanel, BorderLayout.CENTER); startTournamentButton.addActionListener(this); } /* * DO NOT CHANGE THIS CLASS * * This class renders our tournament bracket. */ class BracketPanel extends JPanel { static final int RECT_WIDTH = 100; static final int RECT_HEIGHT = 20; public void paintComponent(Graphics g) { super.paintComponent(g); if ((getWidth() > 0) && (getHeight() > 0)) { int numTeams = treeManager.getNumTeams(); Iterator it = treeManager.bottomUpBracketIterator(); int numLevels = treeManager.logBase2(numTeams); int level = numLevels; int counter = 0; int maxForRow = numTeams; int rectX, rectY; while (it.hasNext()) { Team team = (Team)it.next(); int spacing = (int)(getHeight()/Math.pow(2, level+1)); rectX = 100 + (numLevels - level) * 200; rectY = (spacing + (spacing * 2 * counter)) - (RECT_HEIGHT/2); if (team == null) g.drawRect(rectX, rectY-RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT); else { String teamText = team.toString(); g.drawString(teamText, rectX, rectY); } counter++; if (counter == maxForRow) { counter = 0; level--; maxForRow /= 2; } } } } } public static void main(String[] args) { TournamentBracketFrame frame = new TournamentBracketFrame(); frame.setVisible(true); } }