/* Programmer: Konstantin Lukin E-mail : lukink@ug.cs.sunysb.edu */ /* Modified by Jay Painter Mon Oct. 11, 2004 * * Modified to use GridBagLayout for AWT components, and replaced * the error label with a AWT Label widget instead of painting it. */ import java.awt.*; import java.applet.*; import java.net.*; public class UserInterface extends Applet { public TextField[] tf = { new TextField("3.0", 7), new TextField("3.0", 7), new TextField("30.0", 7) }; private Label[] label = { new Label("Lambda", Label.CENTER), new Label("Distance", Label.CENTER), new Label("Theta", Label.CENTER) }; private Message message; private Label message_label = new Label("", Label.CENTER); private Button[] button; private int PLACES = 2; private GraphCanvas graph; private Bragg bragg; private Box[] box = new Box[3]; private float[] value; private float[] delta = { 0.1f, 0.1f, 1.0f }; private float[] range = { 5.0f, 0.5f, 5.0f, 0.5f, 50.0f, 5.0f }; private GridBagLayout gb_layout; public void init() { setBackground(Color.lightGray); gb_layout = new GridBagLayout(); button = new Button[6]; value = new float[3]; message = new Message(Message.EMPTY); graph = new GraphCanvas(this); setLayout(gb_layout); loadLayout(); } public boolean action(Event evt, Object arg) { if((evt.target instanceof TextField)) { setGraph(); } else if(evt.target instanceof Button) { int i=0; while(i range[i]) { setCode(div+1); return true; } else tf[div].setText(Float.toString(d+delta[div])); } else { if(d < range[i]) { setCode(div+1); return true; } else tf[div].setText(Float.toString(d-delta[div])); } setGraph(); } else return super.action(evt, arg); return true; } private void loadLayout() { GridBagConstraints c = new GridBagConstraints(); Font label_font = new Font("Helvetica", Font.BOLD, 12); Font tf_font = new Font("Helvetica", Font.BOLD, 12); Font button_font = new Font("Courier", Font.BOLD, 12); setCode(Message.INIT); // setup some defaults for the GridBagConstraints c.fill = GridBagConstraints.BOTH; // add graphic section c.gridx = 0; c.gridy = 0; c.gridwidth = 6; add(graph, c); // setup the control box labels c.weightx = 1.0; c.fill = GridBagConstraints.NONE; c.gridy = 1; c.gridwidth = 2; for (int i=0; i"); button[2*i].setFont(button_font); button[2*i+1] = new Button("<"); button[2*i+1].setFont(button_font); // set label GridBagConstraints and add upper button c.gridx = c.gridx + 1; c.gridheight = 1; add(button[2*i], c); // lower button c.gridy = c.gridy + 1; add(button[2*i+1], c); } // add message lablel c.fill = GridBagConstraints.BOTH; c.gridx = 0; c.gridy = 4; c.gridwidth = 6; add(message_label, c); setGraph(); setCode(Message.EMPTY); } private void setGraph() { try { value[0] = Float.valueOf(tf[0].getText()).floatValue(); value[1] = Float.valueOf(tf[1].getText()).floatValue(); value[2] = Float.valueOf(tf[2].getText()).floatValue(); } catch (NumberFormatException e) { setCode(3); return; } if(Bragg.LambdaOutOfRange(value[0], Bragg.FORMULA)) { setCode(1); return; } if(Bragg.DistanceOutOfRange(value[1], Bragg.FORMULA)) { setCode(2); return; } if(Bragg.ThetaOutOfRange(value[2])) { setCode(3); return; } if(message.getType() == Message.ERROR_MSG) setCode(Message.EMPTY); bragg = new Bragg(Bragg.FORMULA, value[0], value[1], value[2]); graph.setGraph(bragg); } private void setCode(int code) { message.setCode(code); if (message.getType()==Message.ERROR_MSG) message_label.setForeground(Color.red); else if (message.getType()==Message.WARNING_MSG) message_label.setForeground(Color.yellow); else message_label.setForeground(Color.green); message_label.setText(message.toString()); } public void setDistTf(String s) { tf[1].setText(s); if(message.getType()==Message.ERROR_MSG) { message.setCode(Message.EMPTY); repaint(); } } public void setThetaTf(String s) { tf[2].setText(s); if(message.getType() ==Message.ERROR_MSG) { message.setCode(Message.EMPTY); repaint(); } } }