//Mult rev .26 //February 13, 1999 //Copyright (c) 1996 - 1999 Philip Lin // // HTTPClient Copyright (C) 1996 Ronald Tschalaer // for more information: // http://www.innovation.ch/java/classes.html // import java.applet.*; import java.awt.*; import java.util.Date; import HTTPClient.*; import java.io.*; import java.net.*; public class Mult extends Applet implements Runnable{ static long counter = 0; Font font1, font2; Frame Frame1 = null; URL u = null; private Thread timer_thread = null; public void init() { setLayout(new BorderLayout()); } public void start() { Panel p = new Panel(); p.add(new Button("Start Mult")); add("South", p); p.add(new Button("See Mult Hall of Fame")); add("South", p); if (timer_thread == null){ timer_thread = new Thread(this); timer_thread.start(); } } public void paint(Graphics g){ g.setColor(Color.black); font1 = new Font("TimesRoman", Font.BOLD, 24); font2 = new Font("TimesRoman", Font.PLAIN, 18); g.setFont(font1); g.drawString("The Mult Applet",10,20); g.setFont(font2); g.drawString("This is a trainer for learning the multiplication tables. The tables from 2 X to 9 X are", 30, 60); g.drawString("scrambled and displayed in a window. You must enter the answer in the yellow prompt", 30, 80); g.drawString("and press the 'Enter' key. Before you press the 'Enter' key, you may correct a mistake by", 30, 100); g.drawString("backspacing by pressing the 'Backspace' key but once you press 'Enter' you will not be", 30, 120); g.drawString("permitted to change an answer. After you have completed all 64 multiplications, you will", 30, 140); g.drawString("be told how many (if any) you got wrong and given a chance to correct the mistakes. (The", 30, 160); g.drawString("prompt will turn to red) If you get them all right the first time, you will be told the time ", 30, 180); g.drawString("it took you. You may then submit your score to the Mult Hall of Fame.", 30, 200); } public boolean action(Event evt, Object arg) { if("Start Mult".equals(arg)) { if (Frame1 != null) Frame1.dispose(); Frame1 = new Frame1(this); return true; } if("See Mult Hall of Fame".equals(arg)) { try { u = getDocumentBase(); u = new URL("http://www.islandnet.com/~dsfa/java/multhalloffame.html"); } catch(MalformedURLException u) { showStatus("Error"); } getAppletContext().showDocument(u); return true; } return false; } public void stop(){ if((timer_thread != null) && timer_thread.isAlive()) timer_thread.stop(); timer_thread = null; } public void run(){ while(true){ try { Thread.sleep(200); } catch (InterruptedException e){}; counter ++; } } } class Dialog1 extends Dialog { static final int H_SIZE = 300; static final int V_SIZE = 200; Date d; int time, min, sec; int errors; submitWindow window = null; Applet ap; public Dialog1(Frame1 parent, int n, int t, Applet applet ) { super(parent, "Mult", true); pack(); Panel p = new Panel(); if (n == 0) { p.add(new Button("Cancel")); add("South", p); p.add(new Button("Submit your time to Mult Hall of Fame")); add("South", p); } else { p.add(new Button("Close")); add("South", p); } time = t / 5; min = time / 60; sec = time - min * 60; errors = n; ap = applet; resize(H_SIZE, V_SIZE); setBackground(Color.white); setForeground(Color.black); } public void paint(Graphics g){ StringBuffer str = new StringBuffer(); g.setFont(new Font("TimesRoman", Font.BOLD, 24)); g.setColor(Color.red); if (errors == 0) { g.drawString("Congatulations!", 40, 50); g.drawString("You got them all right.", 30, 70); str.append("Time: "); if (min != 0) { str.append(String.valueOf(min)); str.append(" mins "); } str.append(String.valueOf(sec)); str.append(" secs"); g.drawString(str.toString(), 50, 90); } else { str.append("You got "); str.append(String.valueOf(errors)); str.append(" wrong."); g.drawString(str.toString(), 30, 50); } } public boolean handleEvent(Event evt) { switch(evt.id) { case Event.WINDOW_DESTROY: { dispose(); return true; } case Event.ACTION_EVENT: { if("Cancel".equals(evt.arg) || "Close".equals(evt.arg)) { dispose(); return true; } else if ("Submit your time to Mult Hall of Fame".equals(evt.arg)) { window = new submitWindow(time, ap); window.setTitle("Submit"); window.pack(); window.show(); dispose(); return true; } } default: } return false; } } class submitWindow extends Frame { static final int FONTHEIGHT = 12; static final String FONTSTRING="System"; TextArea txt8=null; TextField[] txt; Applet ap; int i, time; public submitWindow(int t, Applet applet) { time = t; ap = applet; Label[] lbl; Button button1, button2; String[] label = {"All Fields are Optional", "FirstName:", "LastName:", "Age:", "City:", "Province or State:", "Country:", "URL and/or Email:", "Comments:" }; txt = new TextField[8]; for (i = 0; i < 7; i++) txt[i] = null; lbl = new Label[9]; GridBagLayout gridbag=new GridBagLayout(); GridBagConstraints c=new GridBagConstraints(); setFont(new Font(FONTSTRING,Font.BOLD,FONTHEIGHT)); setLayout(gridbag); setBackground(Color.lightGray); c.fill=GridBagConstraints.NONE; c.weightx=1.0; c.weighty=1.0; c.ipadx=4; c.ipady=4; c.insets=new Insets(5,5,5,5); c.gridwidth=GridBagConstraints.REMAINDER; lbl[0] = new Label(label[0]); gridbag.setConstraints(lbl[0],c); add(lbl[0]); for (i = 1; i < 8; i++) { c.gridwidth=1; lbl[i] = new Label(label[i]); gridbag.setConstraints(lbl[i],c); add(lbl[i]); c.gridwidth=GridBagConstraints.REMAINDER; txt[i] = new TextField("", 16); gridbag.setConstraints(txt[i] ,c); add(txt[i]); } c.gridwidth=GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; lbl[8] = new Label(label[8]); gridbag.setConstraints(lbl[8],c); add(lbl[8]); c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.BOTH; txt8 = new TextArea(5,34); gridbag.setConstraints(txt8,c); add(txt8); c.gridwidth=1; c.anchor=GridBagConstraints.WEST; button1 = new Button("Cancel"); gridbag.setConstraints(button1,c); add(button1); c.anchor=GridBagConstraints.EAST; button2 = new Button("Submit"); gridbag.setConstraints(button2,c); add(button2); } public boolean action(Event evt, Object arg) { if (arg.equals("Cancel")) { hide(); return true; } else if (arg.equals("Submit")) { NVPair form_data[] = new NVPair[11]; form_data[0] = new NVPair("TO", "dsfa"); form_data[1] = new NVPair("SUBJECT", "Mult Result"); form_data[2] = new NVPair("NAME1", txt[1].getText()); form_data[3] = new NVPair("NAME2", txt[2].getText()); form_data[4] = new NVPair("AGE", txt[3].getText()); form_data[5] = new NVPair("CITY", txt[4].getText()); form_data[6] = new NVPair("PROV", txt[5].getText()); form_data[7] = new NVPair("COUNTRY", txt[6].getText()); form_data[8] = new NVPair("URL", txt[7].getText()); form_data[9] = new NVPair("BODY", txt8.getText()); form_data[10] = new NVPair("TIME", String.valueOf(time)); try { HTTPConnection con = new HTTPConnection(ap); HTTPResponse rsp = con.Post("/cgi-bin/mail", form_data); if (rsp.StatusCode >= 300) System.err.println("Received Error: "+rsp.ReasonLine); } catch (AuthTypeNotImplementedException ae) { System.err.println("AuthTypeNotImplementedException: " + ae); } catch (IOException ioe) { System.err.println("IOException: " + ioe); } dispose(); return true; } return false; } public synchronized boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) { hide(); return true; } return super.handleEvent(evt); } } class Frame1 extends Frame { static final int H_SIZE = 750; static final int V_SIZE = 480; long startTime; Font font; int x, y; int count, errors, mode; StringBuffer InputString = new StringBuffer(); StringBuffer str = new StringBuffer(); boolean cr; int[][] inorder; int[] error; int[] answer; boolean[] where; Applet ap; Dialog D1; public Frame1(Applet applet ) { int i, j, n; int temp0, temp1; setTitle("Mult"); pack(); resize(H_SIZE, V_SIZE); show(); ap = applet; count = 0; errors = 0; mode = 1; inorder = new int [2][64]; error = new int[64]; answer = new int[64]; where = new boolean[64]; for (i = 0; i < 64; i++) { inorder[0][i] = i / 8 + 2; inorder[1][i] = i % 8 + 2; where[i] = false; answer[i] = -1; } where[0] = true; for (i = 0; i < 64; i++) { n = (int)Math.rint(Math.random() * (63 - i) + i); temp0 = inorder[0][n]; temp1 = inorder[1][n]; inorder[0][n] = inorder[0][i]; inorder[1][n] = inorder[1][i]; inorder[0][i] = temp0; inorder[1][i] = temp1; } font = new java.awt.Font("Courier", Font.PLAIN, 16); requestFocus(); startTime = Mult.counter; } public void paint(Graphics g) { int i, j; int m; int x, y; Color c; g.setFont(font); for (i = 0; i < 64; i++) { x = i % 6; y = i / 6; str.setLength(0); str.append(String.valueOf(inorder[0][i])); str.append(" x "); str.append(String.valueOf(inorder[1][i])); str.append(" = "); g.drawString(str.toString(), 20 + x * 120, 48 + y * 35); if (where[i] == true){ c = g.getColor(); if (mode == 1) g.setColor(Color.yellow); else g.setColor(Color.red); g.fillRect(100 + x * 120, 35 + y * 35, 28, 14); g.setColor(c); } if (answer[i] != -1) g.drawString(String.valueOf(answer[i]).toString(), 100 + x * 120, 48 + y * 35); } } public boolean handleEvent(Event e) { int a, i, l, time; boolean flag; char ch; Date d; switch(e.id) { case Event.WINDOW_DESTROY: { dispose(); return true; } case Event.KEY_PRESS: { if (e.key == 0x0a) { if (mode == 1) { repaint(100 + (count % 6) * 120, 35 + (count / 6) * 35, 28, 14); where[count] = false; if (answer[count] != inorder[0][count] * inorder[1][count]) { errors++; error[errors] = count; } count++; InputString.setLength(0); if (count == 64) { time = (int)(Mult.counter - startTime); D1 = new Dialog1(this, errors, time, ap); D1.show(); requestFocus(); if (errors > 0) { mode = 2; count = error[errors]; errors--; where[count] = true; } } else where[count] = true; } else if (mode == 3) if (answer[count] == inorder[0][count] * inorder[1][count]) { where[count] = false; if (errors > 0) { mode = 2; count = error[errors]; where[count] = true; errors--; repaint(); } else D1.dispose(); } else mode = 2; } else if ((e.key == 0x08) & (InputString.length() > 0)) { InputString.setLength(InputString.length() - 1); if (InputString.length() > 0 ) answer[count] = Integer.parseInt(InputString.toString()); else answer[count] = -1; } else if ((e.key > 0x2f) & (e.key < 0x3a)) { if (mode == 2) { InputString.setLength(0); answer[count] = -1; repaint(); mode = 3; } InputString.append((char)e.key); if (InputString.length() > 2) InputString.setLength(2); answer[count] = Integer.valueOf(InputString.toString()).intValue(); } repaint(100 + (count % 6) * 120, 35 + (count / 6) * 35, 28, 14); return true; } default: } return false; } }