import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; /** * This class represents a GUI for the basic bouncing ball program. * Not much work is done here; the majority of the interesting behavior is * found in the BallCanvas class. * @author Marty Stepp * @version CSE 331 Spring 2011, 5/11/2011 */ public class BallGui implements ActionListener { private static final int FPS = 60; private JFrame frame; private BallCanvas canvas; private JButton start, stop; private Timer timmy; /** * Constructs the GUI and displays it on the screen. */ public BallGui() { canvas = new BallCanvas(); start = new JButton("Start"); stop = new JButton("Stop"); frame = new JFrame("CSE 331 Ball"); timmy = new Timer(1000 / FPS, canvas); start.addActionListener(this); stop.addActionListener(this); Container south = new JPanel(); south.add(start); south.add(stop); frame.add(south, BorderLayout.SOUTH); frame.setLocation(300, 100); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(canvas); frame.setVisible(true); } /** * Handles action events (button clicks) in this GUI. */ public void actionPerformed(ActionEvent event) { if (event.getSource() == start) { timmy.start(); } else { // stop timmy.stop(); } } }