/** * hw2.java * COP 5025, Programming Languages (Fall 1998) * * An applet to graph a cubic equation: aX^3 + bX^2 + cX + d * * When the applet is run a window pops up with text fields to enter * the 4 coefficients (a,b,c,d). * * There are also 3 buttons: * PLOT : after changing the coefficients clicking on plot will * graphically plot the cubic equation * ZOOM IN and ZOOM OUT: These change the scale factor used to plot * the graph * @author (Viji) Vijayasree Sivasubramaniam * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class hw2 extends Applet { // the coeffient entry fields // these are made instance variables (as opposed to method variables // inside the method 'init' since these variables also need to be // accessed inside the method 'plot' int twidth = 5; TextField coef_x3 = new TextField("0",twidth); TextField coef_x2 = new TextField("1",twidth); TextField coef_x1 = new TextField("0",twidth); TextField coef_x0 = new TextField("0",twidth); Dimension dim; // the dimensions of the window double zoomFactor = 1.0; public void init () { setSize (500,500); setBackground (Color.white); dim = getSize(); add (coef_x3); add (coef_x2); add (coef_x1); add (coef_x0); // the buttons Button bPlot = new Button ("Plot"); Button bZoomOut = new Button ("Zoom Out"); Button bZoomIn = new Button ("Zoom In"); // event handlers for each of the buttons bPlot.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { repaint (); } }); bZoomOut.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { zoomFactor *=0.8; System.out.println ("zoomFactor= " + zoomFactor); repaint (); } }); bZoomIn.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { zoomFactor *=1.2; System.out.println ("zoomFactor= " + zoomFactor); repaint (); } }); add (bPlot); add (bZoomOut); add (bZoomIn); } // the main draing routine public void paint (Graphics g) { System.out.println (dim.width + " " + dim.height); // g.drawString(dim.width + " x " + dim.height, 100,100); // first draw the two major axis g.setColor(Color.black); g.drawLine (0, dim.height/2, dim.width, dim.height/2); // X-axis g.drawLine (dim.width/2, 0, dim.width/2, dim.height); // Y-axis // now lets draw a grid across the whole window g.setColor(new Color (200,255,255)); // a light cyan int step = 20; // We split the drawing of the grid lines into two parts // We first draw the grid lines on one side of an axis and then use // another loop to draw the grid lines on the other side. // draw the horizontal lines for (int y=dim.height/2-step; y>=0; y-=step) { g.drawLine (0, y, dim.width, y); } for (int y=dim.height/2+step; y<=dim.height; y+=step) { g.drawLine (0, y, dim.width, y); } //draw the vertical lines for (int x=dim.width/2-step; x>=0; x-=step) { g.drawLine (x, 0, x, dim.height); } for (int x=dim.width/2+step; x<=dim.width; x+=step) { g.drawLine (x, 0, x, dim.height); } g.setColor(Color.red); g.drawRect (10,10,dim.width-20,dim.height-20); // finally do the plotting double a = new Double(coef_x3.getText()).doubleValue(), b = new Double(coef_x2.getText()).doubleValue(), c = new Double(coef_x1.getText()).doubleValue(), d = new Double(coef_x0.getText()).doubleValue(); plot (g,a,b,c,d); } /** * plot takes the 4 coefficients of the cubic and does the actual * plotting of the graph. * The real world coordinate (0,0) is mapped onto the middle of the screen. * Suppose the screen size is 500x500. * The the real world origin corresponds to the screen coordinates of (250,250) * Starting with a real world coordinate value of x=-250. 'plot' calculates * the corresponding y value. * These values are then translated into screen coordinates * and the line segments are plotted * * @param g --- the graphics context * @param a,b,c,d --- the four coeeficients of the cubic * * @see scaleX * @see scaleY * @see onScreen * * @author (Viji) Vijayasree Sivasubramaniam * */ public void plot (Graphics g, double a, double b, double c, double d) { double x, y; int X_old, Y_old, X_new, Y_new; // a dubgging message System.out.println ("Ploting: " + a + " " + b + " " + c + " " + d); g.setColor(Color.magenta); // the color of the graph x = (int) (-dim.width/2*zoomFactor); y = a*Math.pow(x,3)+b*Math.pow(x,2)+c*x+d; X_old = scaleX ((int) x); Y_old = scaleY ((int) y); x++; for (; x<=(int)(dim.width/2*zoomFactor); x++) { y = a*Math.pow(x,3)+b*Math.pow(x,2)+c*x+d; X_new = scaleX ((int) x); Y_new = scaleY ((int) y); // draw the line segment only if atleast one of its end points // lies inside the screen area if (onScreen (X_old,Y_old) || onScreen (X_new,Y_new)) { g.drawLine (X_old, Y_old, X_new, Y_new); } X_old = X_new; Y_old = Y_new; } } public int scaleX (int x) { return (int) (zoomFactor*(x+dim.width/2/zoomFactor)); } public int scaleY (int y) { return (int) (zoomFactor*(-y+dim.height/2/zoomFactor)); } public boolean onScreen (int x, int y) { return ((0 <= x) && (x <= dim.width*zoomFactor)) && ((0 <= y) && (y <= dim.height*zoomFactor)) ; } }