home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / plot2d / g2d6.jav < prev    next >
Encoding:
Text File  |  1996-01-11  |  9.5 KB  |  337 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.URL;
  4.  
  5. public class g2d6 extends Applet {
  6.       Controls control;
  7.       Button   show;
  8.       Button   redraw;
  9.  
  10.       String base = "http://www-igpp.llnl.gov/leigh-cgi/driver";
  11.  
  12.       String   material = "stone";
  13.       String   plot     = "velocity";
  14.       String   height   = "100";
  15.       String   radius   = "30";
  16.       String   velocity = "15";
  17.       String   trajectory = "45";
  18.  
  19.       G2Dint   graph;
  20.       Axis     xaxis;
  21.       Axis     yaxis;
  22.       DataSet  data1;
  23.  
  24.  
  25.       public void init() {
  26.          Rectangle r = bounds();
  27.          Panel p = new Panel();
  28.          control = new Controls();
  29.          control.resize(550,150);
  30.          control.setTitle("Controls");
  31.          control.setHeight(height);
  32.          control.setRadius(radius);
  33.          control.setVelocity(velocity);
  34.          control.setTrajectory(trajectory);
  35.          show = new Button("Show Controls");
  36.          redraw =  new Button("Calculate");
  37.          p.add(show);
  38.          p.add(redraw);
  39.       
  40.  
  41.  
  42.          graph = new G2Dint();
  43.  
  44.          setLayout( new BorderLayout() );
  45.          add("South", p);
  46.          add("Center", graph);
  47.  
  48.          graph.setFont(new Font("TimesRoman",Font.PLAIN,16));
  49.  
  50.          xaxis = graph.createXAxis();
  51.          xaxis.title_color = Color.magenta;
  52.          xaxis.minor_tic_count = 4;
  53.  
  54.          yaxis = graph.createYAxis();
  55.          yaxis.title = new String("Altitude (km)");
  56.          yaxis.title_offset = new Dimension(-10,-10);
  57.          yaxis.title_color = Color.magenta;
  58.          yaxis.minor_tic_count = 4;
  59.  
  60.  
  61.          getData();
  62.  
  63.  
  64.       }
  65.  
  66.       public URL buildURL() {
  67.             URL url;
  68.             String t;
  69.  
  70.             StringBuffer s = new StringBuffer(base);
  71.             s.append("?material=");
  72.             s.append(control.getMaterial());
  73.             s.append("&plot=");
  74.             s.append(control.getPlot());
  75.             s.append("&velocity=");
  76.             s.append(control.getVelocity());
  77.             s.append("&radius=");
  78.             s.append(control.getRadius());
  79.             s.append("&height=");
  80.             s.append(control.getHeight());
  81.             s.append("&trajectory=");
  82.             s.append(control.getTrajectory());
  83.  
  84.  
  85.             try {
  86.                   url = new URL(s.toString());
  87.             } catch (Exception e) {
  88.                   url = null;
  89.             }
  90.  
  91.             return url;
  92.       }
  93.  
  94.       public void getData() {
  95.               String s = control.getPlot();
  96.  
  97.               graph.deleteDataSet(data1);
  98.               data1 = graph.loadFile( buildURL() );
  99.               xaxis.attachDataSet(data1);
  100.               yaxis.attachDataSet(data1);
  101.               data1.linecolor = new Color(255,255,0);
  102.  
  103.  
  104.               if( s.equals("velocity") ) {
  105.                            xaxis.title = "Velocity (km/sec)";
  106.               }
  107.               else
  108.               if( s.equals("mass") ) {
  109.                            xaxis.title = "Mass (kg)";
  110.               }
  111.               else
  112.               if( s.equals("trajectory") ) {                                  
  113.                            xaxis.title = "Trajectory (Degrees)";
  114.               }
  115.               else
  116.               if( s.equals("radius") ) {
  117.                            xaxis.title = "Radius (m)"; 
  118.               }
  119.               else
  120.               if( s.equals("energy1") ) {
  121.                            xaxis.title = "Cumulative Energy Deposition (MT)"; 
  122.  
  123.               }
  124.               else
  125.               if( s.equals("energy2") ){
  126.                            xaxis.title = "Energy Deposition (MT/km)"; 
  127.           }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.       }
  134.  
  135.       public boolean action(Event ev, Object arg) {
  136.            if ("Calculate".equals(arg)) {
  137.               getData();              
  138.               graph.repaint();
  139.               return true;
  140.            } else
  141.            if ("Show Controls".equals(arg)) {
  142.                 control.show();
  143.                 show.setLabel("Hide Controls");
  144.                 return true;
  145.            } else
  146.            if ("Hide Controls".equals(arg)) {
  147.                 control.hide();
  148.                 show.setLabel("Show Controls");
  149.                 return true;
  150.            }
  151.  
  152.            return false;
  153.       }
  154.  
  155.  
  156. }
  157.  
  158.  
  159.  
  160. class Controls extends Frame {
  161.  
  162.        CheckboxGroup group1    = new CheckboxGroup();
  163.        CheckboxGroup group2    = new CheckboxGroup();
  164.        TextField TFheight     = new TextField(6);
  165.        TextField TFradius     = new TextField(6);
  166.        TextField TFvelocity   = new TextField(6);
  167.        TextField TFtrajectory = new TextField(6);
  168.  
  169.        String Lvelocity    = "Velocity";     
  170.        String Lmass        = "Mass";     
  171.        String Ltrajectory  = "Trajectory";
  172.        String Lradius      = "Radius";    
  173.        String Lenergy1     = "Energy";     
  174.        String Lenergy2     = "Energy/km";     
  175.  
  176.        String Liron        = "Iron";
  177.        String Lstone       = "Stone";
  178.        String Lchondrite   = "Chondrite";
  179.        String Lice         = "Ice";
  180.  
  181.        public Controls() {
  182.              setFont(new Font("TimesRoman", Font.PLAIN, 14));
  183.  
  184.              Panel p1 = new Panel();
  185.              p1.setLayout( new GridLayout(4,1,0,0) );
  186.              p1.add( new Checkbox(Liron,      group1, false) );
  187.              p1.add( new Checkbox(Lstone,     group1, true)  );
  188.              p1.add( new Checkbox(Lchondrite, group1, false) );
  189.              p1.add( new Checkbox(Lice,       group1, false) );
  190.  
  191.              Panel p3 = new Panel();
  192.              p3.setLayout( new GridLayout(6,1,0,0) );
  193.  
  194.              p3.add( new Checkbox(Lvelocity,           group2, false) );
  195.              p3.add( new Checkbox(Lmass,               group2, false) );
  196.              p3.add( new Checkbox(Ltrajectory,         group2, false) );
  197.              p3.add( new Checkbox(Lradius,             group2, false) );
  198.              p3.add( new Checkbox(Lenergy1,            group2, false) );
  199.              p3.add( new Checkbox(Lenergy2,            group2, true) );
  200.  
  201.              Panel p2 = new Panel();
  202.              p2.setLayout( new GridLayout(4,2,0,0) );
  203.       
  204.              p2.add( TFheight ); 
  205.              p2.add(new Label("Height (km)"));
  206.    
  207.              p2.add( TFradius );
  208.              p2.add(new Label("Radius (m)"));       
  209.     
  210.              p2.add( TFvelocity );
  211.              p2.add(new Label("Velocity (km/sec)"));       
  212.     
  213.              p2.add( TFtrajectory );
  214.              p2.add(new Label("Trajectory (Degrees)"));       
  215.  
  216.  
  217.              setLayout( new FlowLayout() );
  218.              add(p1);
  219.              add(p2);
  220.              add(p3);
  221.  
  222.        }
  223.  
  224.  
  225.        public String getMaterial() {
  226.           String s = group1.getCurrent().getLabel();
  227.           if( s.equals(Liron) ) {
  228.                                  return "iron";
  229.           }
  230.           else
  231.           if( s.equals(Lstone) ) {
  232.                                  return "stone";
  233.           }
  234.           else
  235.           if( s.equals(Lchondrite) ) {
  236.                                  return "chondrite";
  237.           }
  238.           else
  239.           if( s.equals(Lice) ) {
  240.                                  return "ice";
  241.           }
  242.           else {
  243.                                  return "stone";
  244.           }
  245.        }
  246.  
  247.        public String getPlot() {
  248.           String s = group2.getCurrent().getLabel();
  249.           if( s.equals(Lvelocity) ) {
  250.                                   return "velocity";
  251.           }
  252.           else
  253.           if( s.equals(Lmass) ) {
  254.                                   return "mass";
  255.           }
  256.           else
  257.           if( s.equals(Ltrajectory) ) {                                  
  258.                                   return "trajectory";
  259.           }
  260.           else
  261.           if( s.equals(Lradius) ) {
  262.                                   return "radius";
  263.           }
  264.           else
  265.           if( s.equals(Lenergy1) ) {
  266.                                   return "energy1";
  267.           }
  268.           else
  269.           if( s.equals(Lenergy2) ) {
  270.                                   return "energy2";
  271.           }
  272.           else {
  273.                                   return "energy2";
  274.           }
  275.        }
  276.  
  277.        public void setHeight(String s) {
  278.             TFheight.setText(s);
  279.        }
  280.  
  281.        public void setRadius(String s) {
  282.             TFradius.setText(s);
  283.        }
  284.  
  285.        public void setVelocity(String s) {
  286.             TFvelocity.setText(s);
  287.        }
  288.  
  289.        public void setTrajectory(String s) {
  290.             TFtrajectory.setText(s);
  291.        }
  292.  
  293.        public String getHeight() {
  294.           double d;
  295.           String s = TFheight.getText();
  296.           try {
  297.              d = Double.valueOf(s).doubleValue();
  298.           } catch(Exception e) {
  299.              s = null;
  300.           }
  301.           return s;
  302.        }
  303.  
  304.        public String getRadius() {
  305.           double d;
  306.           String s = TFradius.getText();
  307.           try {
  308.              d = Double.valueOf(s).doubleValue();
  309.           } catch(Exception e) {
  310.              s = null;
  311.           }
  312.           return s;
  313.        } 
  314.        public String getVelocity() {
  315.           double d;
  316.           String s = TFvelocity.getText();
  317.           try {
  318.              d = Double.valueOf(s).doubleValue();
  319.           } catch(Exception e) {
  320.              s = null;
  321.           }
  322.           return s;
  323.        }
  324.        public String getTrajectory() {
  325.           double d;
  326.           String s = TFtrajectory.getText();
  327.           try {
  328.              d = Double.valueOf(s).doubleValue();
  329.           } catch(Exception e) {
  330.              s = null;
  331.           }
  332.           return s;
  333.        } 
  334.  
  335.  
  336.  
  337. }