20-CS-694 Advanced Programming Techniques Spring 2012
Check Boxes

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      Applet Source      A Threadless Solution      All lectures

Check Box Test

Answer the questions in order. Questions cannot be answered out of order.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

// Just draws the title of the Applet
class TitlePanel extends Canvas {
   Image oi;
   Graphics og;
   
   Font fnt = new Font("TimesRoman",Font.BOLD,18);
   Font sml = new Font("TimesRoman",Font.ITALIC,14);
   FontMetrics fm;
   String t1 = "Pick A Number Between 0 and 31";
   String t2 = "(and answer the following questions)";

   TitlePanel () {
      int maxsize = (t1.length() < t2.length()) ? t2.length() : t1.length();
      setSize(maxsize, 60);
   }
   
   void setImage() {
      oi = createImage(this.getSize().width, 60);
      og = oi.getGraphics();
      og.setFont(fnt);
      fm = og.getFontMetrics();
      og.drawString(t1, (this.getSize().width-fm.stringWidth(t1))/2, 20);
      og.setFont(sml);
      fm = og.getFontMetrics();      
      og.drawString(t2, (this.getSize().width-fm.stringWidth(t2))/2, 35);
      getGraphics().drawImage(oi, 0, 0, this);
   }
   
   public void paint (Graphics g) {
      g.drawImage(oi, 0, 0, this);
   }
}

// Writes the questions in the appropriate spaces
class QuestionCanvas extends Canvas {
   Image oi = null;
   Graphics og;

   Font fnt = new Font("TimesRoman", Font.BOLD, 16);
   String question = null;
   
   QuestionCanvas () { setSize(230,30); }
   
   void setQuestion (String question) {
      oi = createImage(this.getSize().width, this.getSize().height);
      og = oi.getGraphics();
      og.setFont(fnt);
      this.question = question;
      og.drawString(question, 10, 20);
      getGraphics().drawImage(oi, 0, 0, this);
   }
   
   public void paint (Graphics g) {
      if (oi != null) g.drawImage(oi, 0, 0, this);
   }
}

// Takes care of each check box and question pair
class QuestionPanel extends JPanel implements ItemListener {
   Checkbox y, n;
   CheckboxGroup c;
   QuestionCanvas q = new QuestionCanvas();
   Label label;
   boolean asking=false, asked=false, result=false, ystate=false, nstate=false;

   QuestionPanel () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.add (y = new Checkbox("Yes",c,false));
      p.add (n = new Checkbox("No",c,false));
      y.addItemListener(this);
      n.addItemListener(this);
      add ("West", p);
      add ("Center", q);
   }
   
   void setQuestion (String question) { q.setQuestion(question); }
      
   synchronized public boolean answer () {
      asking = true;
      try {  wait();  } catch (Exception e) { }
      return result;
   }

   synchronized public void itemStateChanged (ItemEvent evt) {
      if (!asking || asked) {
         y.setState(ystate);
         n.setState(nstate);
      } else {
         ystate = y.getState();
         nstate = n.getState();
         result = ystate;
         asked = true;
         if (asking) notify();
      }
   }
}

public class CheckBoxApplet extends Applet implements Runnable {
   TitlePanel tt;
   QuestionPanel q[] = new QuestionPanel[5];
   JLabel label;
   Thread runner;
   
   public void init() {
      setLayout(new BorderLayout());
      add ("North", tt = new TitlePanel());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(5,1));
      for (int i=0 ; i < 5 ; i++) p.add(q[i] = new QuestionPanel());
      add ("Center", p);
      add ("South", label = new JLabel("",JLabel.CENTER));
      repaint();
   }
   
   public void start() {
      tt.setImage();
      runner = new Thread(this);
      runner.start();
   }
   
   public void run () {
      int top=31, bottom = 0;

      for (int i=0 ; i < 5 ; i++) {
         int mid = (top+bottom)/2;
         q[i].setQuestion("Is the number less than or equal to "+mid+"?");
         if (q[i].answer() == true) top = mid; else bottom = mid+1;
      }
      label.setText("Your number is "+String.valueOf(top));
   }
}