Home : Course Map : Chapter 6 : Java : Tech : Physics :
Display Text Data in Swing
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

  

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.