001 package swingui; 002 003 import java.awt.event.ActionEvent; 004 import java.awt.event.ActionListener; 005 006 import javax.swing.JButton; 007 import javax.swing.JPanel; 008 009 /** The ModePanel contains a Play/Pause button that allows the user to toggle between 010 * build and play modes. 011 */ 012 public class ModePanel extends JPanel implements ActionListener { 013 /** a serial version uid **/ 014 private static final long serialVersionUID = 1L; 015 016 /** a reference to the gui whose mode we are changing **/ 017 private MainGUI gui; 018 019 //component 020 private JButton play_pause; 021 022 /** Create a ModePanel, initialize in pause/build mode **/ 023 public ModePanel(MainGUI gui) { 024 this.gui = gui; 025 play_pause = new JButton("Play"); 026 play_pause.addActionListener(this); 027 add(play_pause); 028 } 029 030 /** toggle play/pause in the gui when the button is pressed **/ 031 public void actionPerformed(ActionEvent e) { 032 JButton src = (JButton)e.getSource(); 033 if (src.getText().equals("Play")) { 034 src.setText("Pause"); 035 gui.play(); 036 } else if (src.getText().equals("Pause")) { 037 src.setText("Play"); 038 gui.pause(); 039 } else { 040 throw new RuntimeException("invalid mode: " + src.getText()); 041 } 042 } 043 044 /** Set the mode panel to be in play mode **/ 045 public void setPlayMode() { 046 play_pause.setText("Pause"); 047 } 048 /** Set the mode panel to be in build mode **/ 049 public void setBuildMode() { 050 play_pause.setText("Play"); 051 } 052 }