/**** * PlotFunction draws a function f(x) in a graphics window. * * See exercise 9 on page 296 of King's book "Java Programming." * * @author JimMahoney * @version Nov 5, 2002 * ***/ import java.awt.*; import jpb.*; class PlotFunction { // The various dimensions and what-not. private final static int WINDOW_SIZE = 400; private final static double X_MAX = 6.0; private final static double X_MIN = -2.0; private final static double Y_MAX = 1.5; private final static double Y_MIN = -1.5; private final static double DX = 0.05; private final static double X_TICKS = 1.0; private final static double Y_TICKS = 0.5; private final static Color AXES_COLOR = Color.darkGray; private final static Color PLOT_COLOR = Color.red; private final static String WINDOW_TITLE = "Plot Function"; // These two variables are used by all the various subroutines. // Make sure they're initialized (in main()) before doing anything else. private static DrawableFrame df; private static Graphics g; // f(x) is the function to be plotted. private static double f(double x){ return Math.sin( 5.0*x ) * Math.cos( x/5.0 ); } // The same function as a string, for the graph label. private static String fAsString(){ String s = "f(x) = sin(5x) cos(x/5)"; return s; } // Convert from (x,y) coordinates to pixels in the horizontal direction. private static int xToPix(double x){ return (int)( ( x - X_MIN ) / ( X_MAX - X_MIN ) * WINDOW_SIZE); } // Convert from (x,y) coordinates to pixels in the vertical direction. private static int yToPix(double y){ return (int)( ( Y_MAX - y ) / ( Y_MAX - Y_MIN ) * WINDOW_SIZE); } private static void plot(){ g.setColor(PLOT_COLOR); double x1 = X_MIN; double y1 = f(x1); double x2; double y2; while ( x1 < X_MAX ){ x2 = x1+DX; y2 = f(x2); g.drawLine( xToPix(x1), yToPix(y1), xToPix(x2), yToPix(y2) ); x1 = x2; y1 = y2; } } private static void drawAxes(){ g.setColor(AXES_COLOR); g.drawLine( xToPix(X_MIN), yToPix(0.0), xToPix(X_MAX), yToPix(0.0) ); // X axis g.drawLine( xToPix(0.0), yToPix(Y_MIN), xToPix(0.0), yToPix(Y_MAX) ); // Y axis int fontSize = WINDOW_SIZE/30; // as specified in the assigment on pg 296. g.setFont(new Font("SansSerif", Font.PLAIN, (int)(WINDOW_SIZE/30) )); // This method assumes that (0,0) is in the graph. for (double x=X_TICKS; xX_MIN; x-=X_TICKS){ g.drawLine( xToPix(x), yToPix(0.0)-5, xToPix(x), yToPix(0.0)+5); g.drawString(""+x, xToPix(x)-10, yToPix(0.0)+20); } for (double y=Y_TICKS; yY_MIN; y-=Y_TICKS){ g.drawLine( xToPix(0.0)-5, yToPix(y), xToPix(0.0)+5, yToPix(y)); g.drawString(""+y, xToPix(0.0)-30, yToPix(y)+4); } } public static void main(String[] args){ // Create window "df", set its size, and make it white. df = new DrawableFrame(WINDOW_TITLE); df.show(); df.setSize(WINDOW_SIZE, WINDOW_SIZE); // Create the graphics object "g", and fill it with white. g = df.getGraphicsContext(); g.setColor(Color.white); g.fillRect(0,0,WINDOW_SIZE,WINDOW_SIZE); // Plot the function. drawAxes(); plot(); // Label the graph g.setColor(Color.blue); g.drawString( fAsString(), WINDOW_SIZE/2, 40 ); // Make sure that all the changes to the window are now visible. df.repaint(); } }