// ArrayPlot.java // // Shows how to organize separate classes for computing and plottng. // Uses a static array to compute and pass an array to a PlotPanel. // Demonstrates use of shallow array copy (actually a pointer) to an array. // // Classes in this file: // ArrayPlot -- the public entry point // PlotFrame -- the JFrame container; must be instantiated // DoMath -- the static math library, never instantiated // PlotPanel -- the JPanel doing plots; must be instantiated // U -- a static library of utilities, never instantiated. // // M.Lampton UCB SSL 2015 // import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.lang.Math; import java.io.*; import java.util.*; import java.text.DecimalFormat; /** * This is the entrypoint of the program. * It does nothing except create a JFrame. * The JFrame does all the work. */ public class ArrayPlot { public static void main(String[] args) throws IOException { PlotFrame frame = new PlotFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible(true); } } /** * This class is the foundation of the program. * It creates and holds all static data structures, * and drives their loading and viewing. * It does no actual work itself, but it must set * and maintain the static (permanent) data structures. */ class PlotFrame extends JFrame { static int NPTS = 1000; static double a[] = new double[NPTS]; static double b[] = new double[NPTS]; public PlotFrame() // the constructor { setTitle("ArrayPlot"); setSize(900, 600); System.out.println("calling DoMath.fillArrays()"); DoMath.fillArrays(NPTS, a, b); System.out.println("now instantiating PlotPanel"); PlotPanel panel = new PlotPanel(NPTS, a, b); // triggers its constructor Container contentPane = getContentPane(); contentPane.add(panel); // makes it visible; OS calls repaint(). } } /** * This class does all the math work, filling the given arrays. * Never instantiated; static method is like a math library. * Has no visible appearance on screen. * Might be slow, but is called only once, at startup. */ class DoMath { static void fillArrays(int npts, double a[], double b[]) { for (int i=0; i w) { s = ""; for (int i=0; i0.0) return Math.log(arg)/Math.log(10.0); return -999.9; } static double dex(double arg) { if ((arg>-300.0) && (arg<300.0)) return Math.pow(10.0, arg); return -999.9; } }