Home : Course Map : Chapter 6 : Java : Tech : Physics :
Plot Data with Java Graphics
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

Now that we introduced graphics capabilities, we can plot the data from our physics demos rather than just printing data to the console or displaying numbers in a text area. In the the applet we take advantage of the DrawPanel component discussed in Chapter 6: Tech. As in that example, we create an instance of DrawPoints (subclass of DrawFunction) to overlay the points on the panel.

We again use code from the Euler method demo program in Chapter 2: Physics : Falling Object and fill arrays that are passed to the DrawPoints object. When repaint() is invoked for the applet, the paintContents() method in DrawPanel invokes the draw() method of the DrawPoints object to plot the points along with error bars.

The plot displays the position of a falling object on the vertical axis versus time on the horizontal axes.

DataPlotApplet.java - Display the data - time vs. position - for a dropped mass by using a DataPanel instance.

+ Previous classes:
Ch. 6:Tech: DrawFunction.java, DrawPoints.java, DrawPanel.java
Ch. 6:Tech: PlotPanel.java, PlotFormat.java

import javax.swing.*;
import java.awt.*;

/**
  * Display the data - time vs. position - for a dropped mass
  * by using an instance of DataPanel .
  *
**/
public class DataPlotApplet extends JApplet
{
  DrawPoints fDrawPoints;

  /**
    * Create a DataPanel object and add it to the content pane.
   **/
  public void init () {

    // Create the points draw function
    fDrawPoints = new DrawPoints ();

    // and a DrawFunction array to pass to the DrawPanel
    DrawFunction [] draw_functions = new DrawFunction[1];
    draw_functions[0] = fDrawPoints;

    // Create an instance of a JPanel sub-class
    // but don't plot anything yet.
    DrawPanel draw_panel = new DrawPanel (-5.0, 1.0,
                   0.0, 1.2, draw_functions);

    draw_panel.setTitle ("Dropped Mass");
    draw_panel.setXLabel ("Y (m) vs Time (sec)");

    // And add one or more panels to the JApplet panel.
    add (draw_panel);
  } // init

  /**
    * Create some data to illustrate the drawing
    * of error bars with this PlotPanel subclass. Pass
    * the data points and error values to the DataPanel
    * object.
   **/
  public void start () {
    // Variables
    double 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;
    vy = v0;

    double [] y = new double[n_steps];
    double [] t = new double[n_steps];
    double [] y_err = new double[n_steps];
    double [] t_err = new double[n_steps];

    y[0] = y0;
    t[0] = 0.0;

    for (n=1; n < n_steps; n++)  {
        y[n] = y[n-1] + vy * dt;
        vy = vy + g * dt;
        t[n] = n * dt;

        // Create some dummy error data for
        // the display.
        y_err[n] = 0.3 * y[n];
        t_err[n] = 0.3 * t[n];
    }

    total_t = n_steps * dt;

    // Create a 2d array to pass the points data
    // to the DrawPoints object.
    double [][] data = new double[4][];
    data[0] = y;
    data[1] = t;
    data[2] = y_err;
    data[3] = t_err;

    fDrawPoints.setParameters (null, data);

    // The DrawPanel will now repaint itself
    // using the DrawPoints object.
    repaint ();

    System.out.println ("Version 1, dt=0.1 ");
    System.out.println ("Time = "+ total_t);
    System.out.println ("y = "+ y[n_steps-1] + ", vy = " + vy);
    System.out.println ("");

  } // start

} // DataPlotApplet

 

References & Web Resources

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.