/****************************************************************************/ /* Import all the modules we will use -- Don't Change!!*/ import java.io.*; import java.util.*; import java.awt.*; import java.applet.*; /****************************************************************************/ /*Declare the applet - Don't Change this line!*/ public class CS111Applet extends Applet { /* Declare all the buttons, knobs, text fields, and labels we will use*/ /*************************************************************************** Don't change, if you add new buttons you must declare them in this section, this is for extra credit only ****************************************************************************/ private Label Lop1; private TextField TFop1; private Label Lop2; private TextField TFop2; private Label Lans; private TextField TFans; private CheckboxGroup CBcolors; private Checkbox CBblack; private Checkbox CBred; private Checkbox CBgreen; private Label Lcolor; private TextField TFcolor; private Label LaddToList; private TextField TFaddToList; private Choice Clist; private Label LselectedFromList; private TextField TFselectedFromList; private Label Lfrom; private TextField TFfrom; private Label Lto; private TextField TFto; private TextArea TAcount; private Button Badd; private Button Bgcd; private Button BcolorButton; private Button BaddToList; private Button Bcount; // if you need extra buttons declare their use here // something like private Button myButton /**************************************************************************** Make the applet constructor, do not change this line ****************************************************************************/ public CS111Applet() { } /********************************************************************* Construct all the buttons we made, should only have to add stuff here if you do the extra credit **********************************************************************/ public void init() { // Construct the objects that we use Lop1 = new Label("op1"); TFop1 = new TextField(); Lop2 = new Label("op2"); TFop2 = new TextField(); Lans = new Label("ans"); TFans = new TextField(); CBcolors = new CheckboxGroup(); CBblack = (new Checkbox("Black", CBcolors, true)); CBred = (new Checkbox("Red", CBcolors, false)); CBgreen = (new Checkbox("Green", CBcolors, false)); Lcolor = new Label("Type in Selected Color"); TFcolor = new TextField(); Lfrom = new Label("From"); TFfrom = new TextField(); Lto = new Label("to"); TFto = new TextField(); TAcount = new TextArea(); LaddToList = new Label("Add to list"); TFaddToList = new TextField(); Clist = new Choice(); LselectedFromList = new Label ("Selected in list"); TFselectedFromList = new TextField(); Badd = new Button("Add"); Bgcd = new Button("GCD"); BcolorButton = new Button ("Change Color to RED"); BaddToList = new Button("Add to List"); Bcount = new Button("Count"); // if you need extra buttons construct them here // something like BmyButton = new Button("What it says"); /**************************************************************************** Add the buttons etc. to the screen, dont change, add only if you do the extra credit ****************************************************************************/ setLayout(new java.awt.GridBagLayout()); addComponent(this, Lop1, "Lop1", 1, 1, 1, 1); addComponent(this, TFop1, "TFop1", 1, 1, 1, 1); addComponent(this, Lop2, "Lop1", 1, 1, 1, 1); addComponent(this, TFop2, "TFop1", 1, 1, 1, 1); addComponent(this, Lans, "Lop1", 1, 1, 1, 1); addComponent(this, TFans, "TFans",1,1,GridBagConstraints.REMAINDER,1); addComponent(this, CBblack, "CBblack", 1, 1, 1, 2); addComponent(this, CBred, "CBred", 1, 1, 1, 2); addComponent(this, CBgreen, "CBgreen", 1, 1,GridBagConstraints.REMAINDER,2); addComponent(this, Lcolor, "Lcolor", 1, 1, 1, 3); addComponent(this, TFcolor, "Lop1", 1, 1, GridBagConstraints.REMAINDER,2); addComponent(this, LaddToList, "LaddToList", 1, 1, 1, 3); addComponent(this, TFaddToList, "TFaddToList", 1, 1, GridBagConstraints.REMAINDER,3); addComponent(this, Clist , "Clist", 1, 1, 1, 2); addComponent(this, LselectedFromList , "LselectedFromList", 1, 1, 1, 2); addComponent(this,TFselectedFromList , "TFselectedFromList", 1, 1,GridBagConstraints.REMAINDER,2); addComponent(this, Lfrom, "Lfrom", 1, 1, 1, 1); addComponent(this, TFfrom, "TFfrom", 1, 1, 1, 1); addComponent(this, Lto, "Lto", 1, 1, 1, 1); addComponent(this, TFto, "TFto", 1, 1, GridBagConstraints.REMAINDER,1) ; addComponent(this, TAcount, "TAcount", 1, 1, GridBagConstraints.REMAINDER,1); addComponent(this,Badd , "Badd", 1, 1, 1, 1); addComponent(this, Bgcd, "Bgcd", 1, 1, 1, 1); addComponent(this, BcolorButton, "BcolorButton", 1, 1, 1, 1); addComponent(this, BaddToList, "BaddToList", 1, 1, 1,1); addComponent(this, Bcount, "Bcount", 1, 1, GridBagConstraints.REMAINDER,1); // if you make extra buttons add them to the screen here // remove line above and add something like // addComponent(this, Bcount, "Bcount", 1, 1, 1,1); // addComponent(this, myButton, "myButton", 1, 1, GridBagConstraints.REMAINDER,1); Window w = getWindow(); w.pack(); w.show(); } /****************************************************************************************************** START OF SECTION YOU HAVE TO CHANGE IF YOU ARE NOT DOING EXTRA CREDIT ******************************************************************************************************/ /********************************************************************* This section of the code handles events. Events are things like button presses or menu selections, this is where all you additions will go. You should not have to remove anything from here, only add to whats here. We have filled in the add button's lines so you can use them as an example **********************************************************************/ public synchronized boolean action(java.awt.Event e, Object arg) { // If the ADD button was pressed if(e.target == Badd) { int x,y; // declare integer variables x and y x = textToNumber(TFop1.getText()); // Set x to the integer value of // what Text Field op1's text is y = textToNumber(TFop2.getText()); // Set y to the integer value of // what Text Field op2's text is TFans.setText(numberToText(x+y)); // Tell Text Field ans to set its // text as the string represenatation // of x+y return true; } // If the black button was pressed else if(e.target == CBblack) { //??tell Text Field FColor to set its foreground color to black //??tell Text Field Color to clear itself return true; } // if the red button was pressed else if(e.target == CBred) { //??tell Text Field FColor to set its foreground color to red //??tell Text Field Color to clear itself return true; } // if the green button was pressed else if(e.target == CBgreen) { //??tell Text Field FColor to set its foreground color to green //??tell Text Field Color to clear itself return true; } // if the color red button at the bottom was pressed else if(e.target == BcolorButton) { //??tell Text Field FColor to set its foreground color to red //??tell Text Field Color to clear itself //??tell Check Box Red to set itself as pressed in return true; } // if the add to the list button was pressed else if(e.target == BaddToList) { //?? Tell Clist to add an item with the name the same as Text Field addToList's Text //?? Tell Text Field addToList to clear itself return true; } // if a selection was made from the list else if (e.target == Clist) { //??Tell Text Field selectedFromList to set its text to whatever Clist's selected item is return true; } // if the count button was pressed else if (e.target == Bcount) { //??Declare some integer variables x,y, and i //??Set x to the integer value of what Text Field from's text is //??Set y to the integer value of what Text Field to's text is //??Tell Text Area count to clear itself //?? Set the value of i //?? Make a loop to print the numbers, use i as a counter variable //??inside loop tell Text Area Count to append the string representation of i followed by a newline to its text return true; } // If the GCD button was pressed else if(e.target == Bgcd) { //??declare some varaibles //??Set m to the number value of what Text Field op1's text is //??Set n to the number value of what Text Field op2's text is //??use Euclid's algorithm to calculate GCD of m and n; this is more than one line most likely //??put the answer in Text Field ans return true; } else return super.action(e, arg); } /****************************************************************************************************** END OF SECTION YOU HAVE TO CHANGE IF YOU ARE NOT DOING EXTRA CREDIT ******************************************************************************************************/ /* below are functions that we have provided for you you should not have to change any of this */ private String numberToText(int x) { return(String.valueOf(x)); } private int textToNumber(String S) { int i; try { i = Integer.parseInt(S); } catch (NumberFormatException X) { return(-1); } return(i); } /********************************************************************* getWindow - Provided for you, dont change it!! **********************************************************************/ public synchronized Window getWindow() { Component w; for(w=this; w!=null && !(w instanceof java.awt.Window); w=w.getParent()); return (java.awt.Window)w; } /********************************************************************* GridBagLayout helper **********************************************************************/ public static void addComponent(Container cont, Component comp, String key, int weightx, int weighty, int gridw, int gridh) { java.awt.GridBagConstraints cnst = new java.awt.GridBagConstraints(); // cnst.gridx = 0; cnst.gridy = 0; cnst.fill = GridBagConstraints.BOTH; cnst.ipadx = 0; cnst.ipady = 0; cnst.insets = new Insets(10,10,10,10); cnst.anchor=GridBagConstraints.CENTER; cnst.weightx = weightx; cnst.weighty = weighty; cnst.gridwidth = gridw; cnst.gridheight = gridh; ((GridBagLayout)cont.getLayout()).setConstraints(comp, cnst); cont.add(key, comp); } }