import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.*; import com.sun.image.codec.jpeg.*; import java.io.*; import java.util.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * * Class InstantReplay. This implements the graphics widget displayed to the * viewer at half-time and at the end of a match. It represents each goal scored * as a 2D graphic on a football pitch jpeg. Each goal is represented as a 2D * line. The viewer can toggle between each goal by pressing the subsequent * button. Each goal has the player number and distance in yards information * displayed. * * @author Adrien Martel * */ public class InstantReplay extends PitchPanel { private ArrayList lines = new ArrayList(); private JButton stepButton = new NextButton(); private int curIndex = -1; private InstantReplay ir; private Font font; /** * Constructor InstantReplay. Sets up the pitch, arraylist to hold all * GoalGraphic objects and places them within a JFrame for later display at * half time and full time. * */ public InstantReplay() { super("pitch.jpg"); font = new Font("lucida sans demibold", Font.BOLD, 12); f.add(this); f.setLayout(new BorderLayout()); f.add(this, BorderLayout.CENTER); // step button added to toggle through scored goals f.add(stepButton, BorderLayout.PAGE_END); f.setSize(305, 230); f.setTitle("Instant Replay Window"); ir = this; paintAll(); } /** * Method to display the widget that is hidden during the gameplay. * * @return true if successful * */ public boolean showWidget() { f.setVisible(true); return true; } /** * paintAll method repaints the panel causing a call to redraw the pitch and * draw the current goal requested by the viewer * */ private void paintAll() { f.paintAll(this.getGraphics()); } /** * Method addGoalVisio. Adds a goal and all its information to the arraylist * of goals by constructing a GoalGraphics object. * * @param playerNum - * the player that scored the goal * @param xS - * starting x pos of the goal * @param yS - * starting y pos of the goal * @param xE - * ending x pos of the goal * @param yE - * ending y pos of the goal * @param dist - * distance in yards of the goal */ public void addGoalVisio(int playerNum, int xS, int yS, int xE, int yE, int dist) { lines.add(new GoalGraphics(playerNum, (53 - xS) * 2.83, (34 - yS) * 2.68, (53 - xE) * 2.83, (34 - yE) * 2.68, dist)); this.repaint(); } /** * * Method paintComponent. Loads the jpeg then draws the current line * reference by the curIndex field. * */ public void paintComponent(Graphics g) { super.paintComponent(g); g2d.setFont(font); // check whether there are goals to draw and whether the curIndex is // valid. if (lines.size() > 0 && curIndex > -1) { GoalGraphics gg = lines.get(curIndex); g2d.drawLine(gg.getXS(), gg.getYS(), gg.getXE(), gg.getYE()); g2d.drawString("PL." + gg.getPlayerNum(), gg.getXS(), gg.getYS()); g2d.drawString(gg.getDistance() + " yds", gg.getXS(), gg.getYS()+12); } } /** * Inner class to implement the NextButton functionality. This button will * be used to toggle between each goal scored. * */ private class NextButton extends JButton implements ActionListener { public NextButton() { super("Click here to load first goal"); addActionListener(this); } public void actionPerformed(ActionEvent arg0) { if (lines.size() > 0) { curIndex++; curIndex = curIndex % lines.size(); this.setText("[Goal Number: " + (curIndex + 1) + "] - Click for Next"); ir.repaint(); } } } }