2D Plotting with PtPlot; classes and packages

[Pages:11]Chapter 11

2D Plotting with PtPlot; classes and packages

It only seems fair for us to keep programs simple while you are in the process of learning how to write them. However, we would like to encourage you to include plotting as part of your programs, both because it's a good way to visualize your results, and because its fun. Fortunately, there is an excellent Java plotting package called PtPlot that is both free and easy to use, and in ? ?? we will show you how to use it.

In order to use PtPlot, you will need to work with multiple classes. Yet understanding how to do this is rather advanced for beginners. Accordingly, you can either use the sample programs without understanding them (the "black box" or operational approach), or you can try to make some sense of the commands that you use, and in the process get familiar with some advanced Java notation and techniques. In this section we talk about multiple classes and packages.

We suggest that, in the least, you skim this section. You may well want to return to it after you have more experience with Java notation and objects. However, if the material to follow appears too advanced for you, you can still jump to the ? ?? on PtPlot and just follow the instructions there.

11.1 Classes and Packages*

Up until this point, your programs have contained a single class contained in a file with a .java extension. That class contained a main method and possibly a number of other methods. Just as we encourage you to modify old programs rather than writing all programs from scratch, so we encourage you to include methods you have already written and debugged when you write your new programs. In addition, Java contains a large number of libraries consisting of collections of methods for various purposes (for example, to draw a pull?down menu).

Accordingly, it is most efficient if you do not try to write all programs completely from scratch, but rather think of the existing libraries (your personal ones as well of others) as tool boxes from which you can extract the individual tools needed for you to construct your program. This will save you time, both because you do not have to write all the methods, and because the methods have already been debugged (usually the most time consuming part of programming). In fact, the object?oriented approach of Java is specifically designed to make reuse of components easier and safer.

3

4 CHAPTER 11. 2D PLOTTING WITH PTPLOT; CLASSES AND PACKAGES

11.1.1 Using Packages

We have already referred to a collection of related methods as a library. For example, the math library contains the methods that calculate various math functions like sines and cosines. In Java terminology, each method would be in a class file, so we could also say that a library is a collection of related classes. In Java terminology, libraries are called packages.

In general, there are two types of packages: the standard Java packages that constitute the Java language, and user?defined packages that extend standard Java. Some of the standard Java packages that we employ in this book are:

Java Package

java.lang java.util java.awt java.applet java.beans java.io

Classes for Basic elements of Java language. Utilities; random number generators, date, time, etc.. Abstract Windowing Toolkit; creating graphical user interfaces. Creating applets and interact with browsers Creating reusable software components. Data input and output

The PtPlot package is an example of a user?defined package. Because these Java packages contain hundreds or thousands of classes (sometimes with

methods of the same name), some organization is necessary to keep tract of what each method does. Java does this with a hierarchical directory structure in which there are parent packages containing sub-packages, with the sub-packages containing more sub-packages or various classes. To make the name of each class unique, one precedes it with the names of the package and sub-packages that contain this class.

For example, consider the command

System.out.println

that we have been using to print a line of output on the screen. Here System is the name of the class (classes begin with capital letters) containing many of Java's methods. When we give the combination System.out, we are referring to an object (or the class that creates it) that represents the standard output stream (what gets written on your screen). The final println that gets affixed to System.out is the name of a method in the class System that prints lines (in this case it adds a line to the object representing the output stream).

To repeat, by convention, the names of classes are capitalized, which helps set them apart from packages which have lowercase names. This is relevant here because the System.out.println command is in the java.lang package, and so the proper full name of the command is actually

java.lang.System.out.println,

which contains the package name as well. Because java.lang is the most basic package, the java compiler automatically looks there to find the methods we invoke, so we can (fortunately) leave off the java.lang prefix. (Presumably, java is the main package and lang is a sub-package, but it is easier to just say java.lang is the package.)

We must admit that we sometimes find Java's naming conventions confusing. Nevertheless, we will use them when importing packages and in using use methods from other classes, so some familiarity with it the conventions is helpful.

You include the classes from a package with the import command. The command can be given in one of two forms:

import .

import specific classes from packagenName

11.2. GRAPHING WITH PTPLOT

5

import .*

import all classes from packagenName

The import command tells the Java compiler to look in the package packageName for methods that it might not find otherwise. However, for this to work the compiler must know where the package of classes and their methods is stored on your particular computer. In other words, the compiler needs to know the path to follow through the local disk memory to get to the directory or folder where the classes are stored. Accordingly, each computer system, be it Windows, Unix, or MacOS, has an environmental variable named CLASSPATH that contains the explicit path to where the classes are stored on that particular computer. As we show in the PtPlot case below, you will need to modify this variable before a package can be imported.

Even though what follows is more advanced programming than we do in this book, for completeness we indicate how you could create your own packages. This is done by placing several classes in the same .java file, and then including a package command at the beginning of the file:

package ; public class

{ } public class { }

Note, your package may be a collection of methods without any main method, for example, mathematical subroutines that are called from all the programs that you write. However, there must be one main method someplace, if the program is to run since execution always begins in a main method. Likewise, the main method must be a public class (other classes can read only the public classes).

11.2 Graphing with PtPlot

One of the exceptional things about Java is that it contains commands at the programming level that permit you to draw graphs on your computer screen -- regardless of your operating system. Yet these commands are rather low level, as we shall in see Chapter ??, Web Computing, and you must attend to a number of details if you want to use truly elementary Java graphics to make your plots.

To avoid all that fuss, we recommend the use of PtPlot [PtPlot],

as a basic plotting package for 2-D graphs1. PtPlot is free, supported by the University of California, written in Java (and thus runs under Unix, Linux, Mac's and MS Windows), and is easy to use for 2-D data plotting or histograms2. It can be incorporated right into your programs or applets (applets are discussed in Chapter ??, Web Computing), or used as a stand?alone application or applet. We include on the disk a Java library (class file) containing the needed methods to plot graphs; alternatively, you may download the most recent version over the Web (we describe how to do that in a separate section).

1This section prepared with the help of Connelly Barnes. 2For our research use, and for many of the graphs in this book, we use AceGr/Xmgr for 2-D plotting and gnuplot for 3-D graphics. Yet AceGr is available only for Unix systems at present, while gnuplot, which is available for Unix and MS Windows, cannot be called from Java programs. In Chap. ??, Electric Potential of Realistic Capacitor, we present a short tutorial on the use of gnuplot.

6 CHAPTER 11. 2D PLOTTING WITH PTPLOT; CLASSES AND PACKAGES

Figure 11.1: Sample output from elementary use of PtPlot in which three data sets are placed on one plot. Note how error bars are included for two. The data file read by PtPlot here is in ptPlotdat.plt.

11.2.1 EasyPtPlot.java

As an example of how this works, here is a program EasyPlot.java that plots the function cos(x) versus x:

1. /* EasyPtPlot.java by D.McI, C.B, RHL, 8/03 OSU

2.

* Plot function using PtPlot package */

3.

4. import ptolemy.plot.*;

// Import plotting package.

5. public class EasyPtPlot

6. {

7.

public static void main(String[] args)

8.

{

9.

// Create ptplot ``Plot'' object

10.

Plot myPlot = new Plot();

11.

12.

myPlot.setTitle("f(x) vs x");

13.

myPlot.setXLabel("x");

14.

myPlot.setYLabel("f(x)");

15.

// Add (x,cos(x)) data points to Plot object using addPoint

16.

17.

for (double x = -5.; x ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download