C:\Users\notebook\Documents\NetBeansProjects\CS2\src\frames\SV1Frame.java
  1 /*
  2  * Frame for the SV1 program, the first version of "Squaresville".
  3  */
  4 package frames;
  5 
  6 import java.awt.BorderLayout;
  7 import java.awt.Color;
  8 import java.awt.Container;
  9 import java.awt.Dimension;
 10 import java.awt.FlowLayout;
 11 import java.awt.GridLayout;
 12 import java.awt.event.ActionEvent;
 13 import java.awt.event.ActionListener;
 14 import javax.swing.JButton;
 15 import javax.swing.JFrame;
 16 import javax.swing.JPanel;
 17 import javax.swing.JTextArea;
 18 import javax.swing.JTextField;
 19 
 20 /**
 21  *
 22  * @author notebook
 23  */
 24 public class SV1Frame extends JFrame implements ActionListener {
 25 
 26     JButton blueButton;
 27     JButton redButton;
 28     JButton yellowButton;
 29     JButton colorButton;
 30     JButton mfdButton;
 31     JButton mbkButton;
 32     JButton mrtButton;
 33     JButton mltButton;
 34     JButton lrtButton;
 35     JButton lltButton;
 36     JButton lmfButton;
 37     JButton srtButton;
 38     JButton sltButton;
 39     JButton nopButton;
 40     JButton clearButton;
 41     JButton cleanButton;
 42     JButton resetButton;
 43     JButton thing1Button;
 44     JButton thing2Button;
 45     JButton thing3Button;
 46     JButton thing4Button;
 47     JButton thing5Button;
 48     JPanel reflector;
 49     JTextArea output;
 50     JTextField input;
 51 
 52     public SV1Frame(String title) {
 53         super(title);
 54         setSize(900, 700);
 55         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 56         addComponents(getContentPane());
 57         addListeners();
 58         setVisible(true);
 59         setResizable(false);
 60     }
 61 
 62     private void addComponents(Container contentPane) {
 63         // northern region
 64         clearButton = new JButton("Clear");
 65         cleanButton = new JButton("Clean");
 66         resetButton = new JButton("Reset");
 67         JPanel northernRegion = new JPanel();
 68         northernRegion.setLayout(new FlowLayout());
 69         northernRegion.add(clearButton);
 70         northernRegion.add(cleanButton);
 71         northernRegion.add(resetButton);
 72 
 73         // western region
 74         blueButton = new JButton("BLUE");
 75         redButton = new JButton("RED");
 76         yellowButton = new JButton("YELLOW");
 77         colorButton = new JButton("COLOR");
 78         mfdButton = new JButton("MFD");
 79         mbkButton = new JButton("MBK");
 80         mrtButton = new JButton("MRT");
 81         mltButton = new JButton("MLT");
 82         lrtButton = new JButton("LRT");
 83         lltButton = new JButton("LLT");
 84         lmfButton = new JButton("LMF");
 85         srtButton = new JButton("SRT");
 86         sltButton = new JButton("SLT");
 87         nopButton = new JButton("NOP");
 88         JPanel westernRegion = new JPanel();
 89         westernRegion.setLayout(new GridLayout(14, 1));
 90         westernRegion.add(blueButton);
 91         westernRegion.add(redButton);
 92         westernRegion.add(yellowButton);
 93         westernRegion.add(colorButton);
 94         westernRegion.add(mfdButton);
 95         westernRegion.add(mbkButton);
 96         westernRegion.add(mrtButton);
 97         westernRegion.add(mltButton);
 98         westernRegion.add(lrtButton);
 99         westernRegion.add(lltButton);
100         westernRegion.add(lmfButton);
101         westernRegion.add(srtButton);
102         westernRegion.add(sltButton);
103         westernRegion.add(nopButton);
104 
105         // eastern region
106         thing1Button = new JButton("THING1");
107         thing2Button = new JButton("THING2");
108         thing3Button = new JButton("THING3");
109         thing4Button = new JButton("THING4");
110         thing5Button = new JButton("THING5");
111         JPanel easternRegion = new JPanel();
112         easternRegion.setLayout(new GridLayout(5, 1));
113         easternRegion.add(thing1Button);
114         easternRegion.add(thing2Button);
115         easternRegion.add(thing3Button);
116         easternRegion.add(thing4Button);
117         easternRegion.add(thing5Button);
118 
119         // central region
120         int width = 730;
121         int height = 315; // adjust these two number if you need to
122         reflector = new JPanel();
123         reflector.setSize(new Dimension(width, height));
124         reflector.setBackground(Color.WHITE);
125         output = new JTextArea(width, height);
126         output.setBackground(Color.WHITE);
127         output.setWrapStyleWord(true);
128         output.setLineWrap(true);
129         Container centralRegion = new Container();
130         centralRegion.setLayout(new GridLayout(2, 1, 6, 6));
131         centralRegion.add(reflector);
132         centralRegion.add(output);
133 
134         // southern region
135         input = new JTextField();
136 
137         // place the components in the frame
138         contentPane.setLayout(new BorderLayout());
139         contentPane.add(northernRegion, BorderLayout.NORTH);
140         contentPane.add(input, BorderLayout.SOUTH);
141         contentPane.add(easternRegion, BorderLayout.EAST);
142         contentPane.add(westernRegion, BorderLayout.WEST);
143         contentPane.add(centralRegion, BorderLayout.CENTER);
144     }
145 
146     private void addListeners() {
147         blueButton.addActionListener(this);
148         redButton.addActionListener(this);
149         yellowButton.addActionListener(this);
150         colorButton.addActionListener(this);
151         mfdButton.addActionListener(this);
152         mbkButton.addActionListener(this);
153         mrtButton.addActionListener(this);
154         mltButton.addActionListener(this);
155         lrtButton.addActionListener(this);
156         lltButton.addActionListener(this);
157         lmfButton.addActionListener(this);
158         srtButton.addActionListener(this);
159         sltButton.addActionListener(this);
160         nopButton.addActionListener(this);
161         clearButton.addActionListener(this);
162         cleanButton.addActionListener(this);
163         resetButton.addActionListener(this);
164         thing1Button.addActionListener(this);
165         thing2Button.addActionListener(this);
166         thing3Button.addActionListener(this);
167         thing4Button.addActionListener(this);
168         thing5Button.addActionListener(this);
169         input.addActionListener(this);
170     }
171 
172     // This method serves to implement the ActionListener interface
173     public void actionPerformed(ActionEvent event) {
174         String command = event.getActionCommand();
175         processCommand(command);
176         if (event.getSource() instanceof JTextField) {
177             input.setText("");
178         }
179     }
180 
181     private void processCommand(String command) {
182         // output.append(command.toUpperCase() + " ");
183         if (command.equalsIgnoreCase("RED")) {
184             output.append("RED ");
185         } else if (command.equalsIgnoreCase("YELLOW")) {
186             output.append("YELLOW ");
187         } else if (command.equalsIgnoreCase("BLUE")) {
188             output.append("BLUE ");
189         } else if (command.equalsIgnoreCase("COLOR")) {
190             output.append("COLOR ");
191         } else if (command.equalsIgnoreCase("MFD")) {
192             output.append("MFD ");
193         } else if (command.equalsIgnoreCase("MBK")) {
194             output.append("MBK ");
195         } else if (command.equalsIgnoreCase("MRT")) {
196             output.append("MRT ");
197         } else if (command.equalsIgnoreCase("MLT")) {
198             output.append("MLT ");
199         } else if (command.equalsIgnoreCase("LMF")) {
200             output.append("LMF ");
201         } else if (command.equalsIgnoreCase("SRT")) {
202             output.append("SRT ");
203         } else if (command.equalsIgnoreCase("SLT")) {
204             output.append("SLT ");
205         } else if (command.equalsIgnoreCase("LRT")) {
206             output.append("LRT ");
207         } else if (command.equalsIgnoreCase("LLT")) {
208             output.append("LLT ");
209         } else if (command.equalsIgnoreCase("NOP")) {
210             output.append("NOP ");
211         } else if (command.equalsIgnoreCase("CLEAN")) {
212             output.append("CLEAN ");
213         } else if (command.equalsIgnoreCase("CLEAR")) {
214             output.append("CLEAR ");
215         } else if (command.equalsIgnoreCase("RESET")) {
216             output.append("RESET ");
217         } else if (command.equalsIgnoreCase("THING1")) {
218             output.append("THING1 ");
219         } else if (command.equalsIgnoreCase("THING2")) {
220             output.append("THING2 ");
221         } else if (command.equalsIgnoreCase("THING3")) {
222             output.append("THING3 ");
223         } else if (command.equalsIgnoreCase("THING4")) {
224             output.append("THING4 ");
225         } else if (command.equalsIgnoreCase("THING5")) {
226             output.append("THING5 ");
227         } else {
228             output.append("### UNRECOGNIZED COMMAND: " + command.toUpperCase() + "###");
229         }
230     }
231 
232 }
233