/* SnellDemo.java * Jan. 10, 1998 * by Douglas Beeson */ import java.awt.*; import java.applet.*; /** An applet to demonstrate Snell's Law of refraction. It uses a * data model (SnellModel.java) and a separate subclass of Panel (Display.java) * to do the work of calculating theta values and displaying the light rays. * This class is the one directly called by the browser. It also handles all * mouse events and places all GUI objects on the screen. */ public class SnellDemo extends java.applet.Applet { Checkbox air1, water1, glass1, diamond1, other1; Checkbox air2, water2, glass2, diamond2, other2; Scrollbar tb, custom1, custom2; Panel controls, angleP; SnellModel model = new SnellModel(); //the data model Display display; //the drawing engine final String theta = "\u03b8"; //a Unicode character for the Greek theta symbol (not supported on all platforms) public SnellDemo() { super(); } //constructor /** The first method called when the applet is started. It is called * only once. If the applet is restarted (upon the browser coming back to * the page), only the start() method will be called. * * This class is responsible for instantiating and laying out * all the GUI components, and for linking the data model and display * classes together using the Observer/Observable pattern. */ public void init() { //Use a BorderLayout layout manager as the top-level GUI component arrangement setLayout(new BorderLayout()); setBackground(Color.white); //Instantiate the display class and connect it to the model display = new Display(); model.addObserver(display); //put the display in the middle of the GUI add("Center",display); //this sub panel holds the left-side scrollbar and theta symbol angleP = new Panel(); //Ouch! The Gridbag is a hard layout manager for beginners to learn, but it is useful... GridBagLayout gbl = new GridBagLayout(); angleP.setLayout(gbl); //angleP.setLayout(new BorderLayout()); // GridBagConstraints gbc1 = new GridBagConstraints(); //constrains the label GridBagConstraints gbc2 = new GridBagConstraints(); //constrins the scrollbar /* gbc1.gridx = 0; * gbc1.gridy = 0; * gbc1.anchor = GridBagConstraints.NORTH; * gbc1.fill = GridBagConstraints.NONE; * gbc1.weighty = 1; * gbc1.weightx = 1; */ gbc2.gridx = 0; gbc2.gridy = 0; gbc2.anchor = GridBagConstraints.CENTER; gbc2.fill = GridBagConstraints.VERTICAL; gbc2.weightx = 1; gbc2.weighty = 1; /* * Label angleL = new Label(theta, Label.CENTER); * gbl.setConstraints(angleL,gbc1); * angleP.add(angleL); */ tb = new Scrollbar(Scrollbar.VERTICAL,45,5,0,90); tb.setBackground(Color.white); gbl.setConstraints(tb,gbc2); angleP.add(tb); //Put the scrollbar and theta symbol on the left side add("West",angleP); //the controls subpanel holds the radio buttons and custom n scrollbars controls = new Panel(); //the grid layout is simple (only one column here) controls.setLayout(new GridLayout(0,1)); controls.setBackground(Color.white); Label l1 = new Label(" Material 1", Label.LEFT); /* all the top radio buttons are linked logically to a checkbox group that * ensures that only one of them is "true" at any given time. */ CheckboxGroup cb1 = new CheckboxGroup(); air1 = new Checkbox("Air", cb1, true); water1 = new Checkbox("Water", cb1, false); glass1 = new Checkbox("Glass", cb1, false); diamond1 = new Checkbox("Diamond", cb1, false); other1 = new Checkbox("Other", cb1, false); custom1 = new Scrollbar(Scrollbar.HORIZONTAL,125,10,100,250); Label space = new Label("____________", Label.LEFT); Label l2 = new Label(" Material 2", Label.LEFT); //Same with the bottom radio buttons CheckboxGroup cb2 = new CheckboxGroup(); air2 = new Checkbox("Air", cb2, false); water2 = new Checkbox("Water", cb2, true); glass2 = new Checkbox("Glass", cb2, false); diamond2 = new Checkbox("Diamond", cb2, false); other2 = new Checkbox("Other", cb2, false); custom2 = new Scrollbar(Scrollbar.HORIZONTAL,125,10,100,250); controls.add(l1); controls.add(air1); controls.add(water1); controls.add(glass1); controls.add(diamond1); controls.add(other1); controls.add(custom1); controls.add(space); controls.add(l2); controls.add(air2); controls.add(water2); controls.add(glass2); controls.add(diamond2); controls.add(other2); controls.add(custom2); // Force all Checkboxes in the "controls" subpanel to be white. Component c; for (int i=0;i