import java.awt.*; import java.awt.event.MouseEvent; import java.applet.*; import java.net.*; import java.util.*; import java.io.*; import java.lang.*; import java.text.*; public class reg extends java.applet.Applet implements Runnable{ boolean InApplet = true; TextDialog dl; String msg1 = "Incorrect degree"; StringTokenizer st; boolean degreeOK; private boolean WarningIsShowing; Integer Idegree; //GUI Components CheckboxGroup ChkGrp1 = new CheckboxGroup(); Checkbox linear = new Checkbox("Linear", ChkGrp1, true); Checkbox polynome = new Checkbox("Polynomial", ChkGrp1, false); CheckboxGroup ChkGrp2 = new CheckboxGroup(); Checkbox Constant = new Checkbox("Yes", ChkGrp2, true); Checkbox NoConstant = new Checkbox("No", ChkGrp2, false); CheckboxGroup ChkGrp3 = new CheckboxGroup(); Checkbox xy = new Checkbox("x's; y", ChkGrp3, true); Checkbox yx = new Checkbox("y; x's", ChkGrp3, false); TextField degree = new TextField("1"); Label label1 = new Label("Type of fit:",Label.LEFT); Label label2 = new Label("Degree:", Label.LEFT); Label label3 = new Label("Compute constant:",Label.LEFT); Label label4 = new Label("Data ordering:",Label.LEFT); Label blank1 = new Label("",Label.LEFT); Label blank2 = new Label("",Label.LEFT); Label blank3 = new Label("",Label.LEFT); Button DataButton = new Button("Data"); Hashtable AllowedMethods, AllowedCompConstants, AllowedOrders; private Thread windowThread=null; private boolean Create = false; public final void DoLayout(){ degree.setEditable(true); setFont(new Font ("Arial", Font.BOLD, 12)); linear.setFont(new Font ("Arial", Font.PLAIN, 12)); polynome.setFont(new Font ("Arial", Font.PLAIN, 12)); Constant.setFont(new Font ("Arial", Font.PLAIN, 12)); degree.setFont(new Font ("Arial", Font.PLAIN, 12)); NoConstant.setFont(new Font ("Arial", Font.PLAIN, 12)); xy.setFont(new Font ("Arial", Font.PLAIN, 12)); yx.setFont(new Font ("Arial", Font.PLAIN, 12)); this.setBackground(Color.lightGray); this.setForeground(Color.black); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridbag); //Row 1 buildConstraints(constraints, 0, 0, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(label1, constraints); add(label1); buildConstraints(constraints, 1, 0, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(linear, constraints); add(linear); //Row 2 buildConstraints(constraints, 1, 1, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(polynome, constraints); add(polynome); //Row 3 buildConstraints(constraints, 0, 2, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(label2, constraints); add(label2); //Degree field buildConstraints(constraints, 1, 2, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(degree, constraints); add(degree); //Blank card buildConstraints(constraints, 0, 3, 2, 1, 0, 1); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.ipadx = 40; gridbag.setConstraints(blank1, constraints); add(blank1); //Row 4 buildConstraints(constraints, 0, 4, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(label3, constraints); add(label3); buildConstraints(constraints, 1, 4, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(Constant, constraints); add(Constant); //Row 5 buildConstraints(constraints, 1, 5, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(NoConstant, constraints); add(NoConstant); //Blank card buildConstraints(constraints, 0, 6, 2, 1, 0, 1); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.ipadx = 40; gridbag.setConstraints(blank2, constraints); add(blank2); //Row 6 buildConstraints(constraints, 0, 7, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(label4, constraints); add(label4); buildConstraints(constraints, 1, 7, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(xy, constraints); add(xy); //Row 7 buildConstraints(constraints, 1, 8, 1, 1, 50, 3); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; gridbag.setConstraints(yx, constraints); add(yx); //Blank card buildConstraints(constraints, 0, 9, 2, 1, 0, 1); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.ipadx = 40; gridbag.setConstraints(blank3, constraints); add(blank3); //Row 8 buildConstraints(constraints, 0, 10, 2, 1, 0, 3); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.ipadx = 40; gridbag.setConstraints(DataButton, constraints); add(DataButton); } public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){ gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; } public Insets insets(){ return new Insets(20, 30, 20, 30); } public final void init(){ preLoadClasses(); DoLayout(); SetupOptions(); setWarningIsShowing(false); } public synchronized void start(){ if(windowThread == null){ windowThread = new Thread(this); windowThread.start(); } } public synchronized void run(){ while(windowThread != null){ while(Create == false){ try{ wait(); }catch(InterruptedException e){ } } //Notification has been given to Create Create = false; DataWindow window = new DataWindow(" Input data", this); } } public void stop(){ if(windowThread != null){ windowThread.stop(); windowThread = null; } } public void SetupOptions() { AllowedMethods = new Hashtable(); AllowedMethods.put(linear, new String("linear")); AllowedMethods.put(polynome, new String("polynome")); AllowedCompConstants = new Hashtable(); AllowedCompConstants.put(Constant, new Boolean(true)); AllowedCompConstants.put(NoConstant, new Boolean(false)); AllowedOrders = new Hashtable(); AllowedOrders.put(xy, new String("xy")); AllowedOrders.put(yx, new String("yx")); } public String getMethod() { Checkbox Selection = ChkGrp1.getCurrent(); String strValue = (String)AllowedMethods.get(Selection); if(strValue == null){ // System.out.println("Illegal method " +strValue +" - EXIT -"); // System.exit(0); } return(strValue); } public boolean ComputeConstant() { Checkbox Selection = ChkGrp2.getCurrent(); Boolean boolValue = (Boolean)AllowedCompConstants.get(Selection); if(boolValue == null){ // System.out.println("Illegal constant - EXIT -"); // System.exit(0); } return(boolValue.booleanValue()); } public String getOrder() { Checkbox Selection = ChkGrp3.getCurrent(); String strValue = (String)AllowedOrders.get(Selection); if(strValue == null){ // System.out.println("Illegal order " +strValue +" - EXIT -"); // System.exit(0); } return(strValue); } public final void CheckDegree(){ Idegree=null; degreeOK = true; String strDegree = degree.getText(); st = new StringTokenizer(strDegree, " \t\r\n"); int Nwords = st.countTokens(); if(Nwords != 1){ degreeOK = false; return; } try{ Idegree = new Integer(st.nextToken()); } catch(Exception e){ degreeOK = false; // AppletTextDialog("", "Incorrect degree", DataButton); return; } return; } public int getDegree(){ return(Idegree.intValue()); } public synchronized boolean action(Event e, Object arg){ Object theSource; theSource = e.target; //Handle clicking of the Data button, and also the Submit //button of one of the possibly multiple text areas that //have been successfully instanciated. if((theSource == DataButton) || theSource instanceof DataWindow){ CheckDegree(); //The algorithm used ensures that once a warning window //is showing, no more can be popped-up. Since creation //of a warning window always disables the 'Data' button, //the following first condition (theSource=DataButton)can //never be satisfied if a warning window is already showing. if(!degreeOK){ if(theSource == DataButton){ AppletTextDialog("", "Incorrect degree", DataButton); }else if(theSource instanceof DataWindow){ if(!getWarningIsShowing()) AppletTextDialog("", "Incorrect degree", DataButton); } }else{ if(theSource == DataButton){ Create = true; notifyAll(); } } return(true); } return(false); } public boolean getWarningIsShowing(){ return(WarningIsShowing); } public void setWarningIsShowing(boolean showing){ WarningIsShowing = showing; } //It does not seem possible to create a modal dialog box here //(true argument in TextDialog constructor)when run in stand-alone, //so we create a non-modal dialog but prevent the user from popping //up new windows by disabling a component (usually a button) //until the dialog box is dismissed. Bug? public void AppletTextDialog(String title, String message, Component Comp){ Comp.disable(); dl = new TextDialog(this, getAnchorpoint(), title, message, false, Comp); return; } //Get the Frame ancestor of this applet (Cf. Lemay p.387). public Frame getAnchorpoint(){ Object anchorpoint = getParent(); while(! (anchorpoint instanceof Frame) ){ anchorpoint = ((Component)anchorpoint).getParent(); } return((Frame)anchorpoint); } // Make sure that pressing 'Enter' on Data Button is handled in // the same way as clicking on it (action event). public boolean keyDown(Event e, int key){ Object theSource; theSource = e.target; if(key == Event.ENTER){ if(theSource == DataButton){ //Create an "ACTION_EVENT" associated with the Data //button (target=DataButton) and post it here //for processing. e = new Event(DataButton, Event.ACTION_EVENT, DataButton.getLabel()); //this.postEvent(e); postEvent(e); return(true); } } return(false); } public synchronized boolean handleEvent(Event e){ Object theSource; int id; theSource = e.target; id = e.id; switch(id){ default: return super.handleEvent(e); } } // This function attempts to load an array of classes. public synchronized final void preLoadClasses(){ Hashtable classes; String className; boolean debug=false; classes = new Hashtable(); // classes.put("1", "outScreen0"); // classes.put("2", "outScreen1"); // classes.put("3", "outText0"); // classes.put("4", "outText1"); classes.put("5", "StringBuf0"); classes.put("6", "StringBuf1"); classes.put("7", "graphUtil"); classes.put("8", "NumFormat"); classes.put("9", "strUtil"); classes.put("10", "Util"); classes.put("11", "Fit"); classes.put("12", "TextDialog"); classes.put("13", "DataWindow"); classes.put("14", "OutputWindow"); classes.put("15", "graphWindow"); classes.put("16", "function2D"); classes.put("17", "graph2D"); for(Enumeration classVec=classes.elements(); classVec.hasMoreElements() ;){ className = (String)classVec.nextElement(); try{ if(InApplet){ // getAppletContext().showStatus("Loading " // +className); }else if(debug){ System.out.println("Loading " +className); } Class ClassObject = Class.forName(className); } catch(ClassNotFoundException e){ if(InApplet){ // getAppletContext().showStatus("Could not load " // +className); }else if(debug){ System.out.println("Could not load " +className); } } } } } class jv extends Frame{ jv(){} public static void main(String args[]) { jv window = new jv(); window.setBackground(Color.lightGray); window.addNotify(); window.resize(380, 280); window.setTitle(" Comeau Viewer"); reg panel = new reg(); panel.InApplet = false; window.add(panel); window.validate(); panel.init(); panel.start(); window.show(); } public boolean handleEvent(Event e){ Object theSource; int id; theSource = e.target; id = e.id; switch(id){ case(Event.WINDOW_DESTROY): System.exit(0); return(true); default: return super.handleEvent(e); } } }