| For the programs up till now we used the simple console print methods 
              since we did not yet introduce graphical display techniques. Here 
              we demonstrate how we can convert out console outputs to an applet 
              display using the TextOutputPanel 
              component and Outputable interfaced discussed 
              in Chapter 6: Java 
              : Text Display.  Below we show a modified version of the Euler method demo program 
              in Chapter 2: Physics : Falling 
              Object. We use a Swing JApplet 
              component and an instance of the TextOutputPanel 
              class to display our text output:  
              
                 
                  |  
                      FallingObjectApplet.java 
                        - rather than sending print output to the 
                        console as in the examples up till this chapter, we send 
                        print string output to a text area in the graphical user 
                        interface. Algorithm is same as FallingObject_Applet1.java 
                        in Chapter 2: Physics.
 + previous classes:
 Ch. 
                        6: Java:  
                         
                        TextOutputPanel.java, 
                        Outputable.java
 
 |   
                  | Java Techniques 
                      & Other Programming Notes 
                      This program illustrates the use of an interface, Outputable 
                        in this case, as a callback mechanism. Passing Outputable 
                        references to method A 
                        allows the method to execute print operations without 
                        needing to know where the print output actuallly goes. 
                        That depends upon the particular methods that invoke method 
                        A. 
                        
 
We use a JTextArea 
                        for displaying text output. 
 
The JTextArea 
                        also comes in a JScrollPane 
                        to provide scrollbars when the text exceeds the available 
                        area.   |   
                  |  import 
                      java.awt.*;import javax.swing.*;
 
 /**
 *  Same as FallingObject_Applet1 class 
                      from chapter 2 except
 *  that output goes to a text area 
                      displayed on a GUI. We
 *  use Swing components including 
                      JApplet and JTextArea.
 *
 *  The TextOutputPanel is a subclass 
                      of JPanel. It holds a
 *  JTextArea in a JScrollPane area. 
                      It also implements our
 *  Outputable interface that provides 
                      print (String) and
 *  println (String) methods similar 
                      to those in System.out
 *  but sends the strings to the TextOutputPanel 
                      instead of
 *  to the console.
 **/
 public class FallingObjectApplet extends JApplet
 {
 /** Set up the UI panel for displaying the text 
                      output of
 * the algorithm. **/
 public void init () {
 
 TextOutputPanel output_panel = new 
                      TextOutputPanel ();
 
 // Add panel with JTextArea to show 
                      output
 add (output_panel);
 compute (output_panel);
 
 } // init
 
 void compute (Outputable out) {
 
 // Variables
 double t, y, vy;
 double total_t;
 int n;
 
 // Constants
 double y0 =  0.0;
 double v0 =  0.0;
 double g  = -9.80;// meter 
                      per sec**2
 
 // Version 1
 double dt =  0.1;
 int n_steps = 10;
 y = y0;
 vy = v0;
 
 for (n=0; n < n_steps; n++) {
 y = y + vy * dt;
 vy = vy + g * dt;
 }
 
 total_t = n * dt;
 
 out.println ("Version 1, dt=0.1 
                      ");
 out.println ("Time = " + total_t);
 out.println ("y = " + y + ", vy 
                      = " + vy);
 out.println ("");
 
 // Version 2
 dt =  0.01;
 n_steps = 100;
 y = y0;
 vy = v0;
 
 for (n=0; n< n_steps; n++) {
 y = y + vy * dt;
 vy = vy + g * dt;
 }
 
 total_t = n * dt;
 
 out.println ("Version 2, dt=0.01 
                      ");
 out.println ("Time = " + total_t);
 out.println ("y = " + y + ", vy 
                      = " + vy);
 out.println ("");
 
 // Version 3
 dt =  0.001;
 n_steps = 1000;
 y = y0;
 vy = v0;
 
 for ( n=0; n< n_steps; n++) {
 y = y + vy * dt;
 vy = vy + g * dt;
 }
 
 total_t = n * dt;
 
 out.println ("Version 3, dt=0.001 
                      ");
 out.println ("Time = "+ total_t);
 out.println ( + y + ", vy = " + 
                      vy);
 out.println ("");
 
 // Version 40
 dt =  0.0001;
 n_steps = 10000;
 y = y0;
 vy = v0;
 
 for ( n=0; n< n_steps; n++) {
 y = y + vy * dt;
 vy = vy + g * dt;
 }
 
 total_t = n * dt;
 
 out.println ("Version 4, dt=0.0001 
                      ");
 out.println ("Time = "+ total_t);
 out.println ( + y + ", vy = " + 
                      vy);
 out.println ("");
 
 
 vy = v0 + g;
 y = y0 + 0.5 * g;
 out.println ("Formulas for tTotal 
                      = 1.0sec");
 out.println ( + y + ", vy = " + 
                      vy);
 
 } // compute
 
 } // FallingObjectApplet
 |    Last update: Oct. 22, 2005 |