import java.awt.Graphics; public class SquareCube extends java.applet.Applet { double sq(double x) { double ax, ay, y; // ax = actual x, ay = actual y (not in pixels) // I want ax to range from -2 to 2 ax = (x - size().width/2)/(size().width/4); ay = ax*ax; y = (-ay*size().height/4 + size().height/2); return y; } double cu(double x) { double ax, ay, y; // ax = actual x, ay = actual y (not in pixels) // I want ax to range from -2 to 2 ax = (x - size().width/2)/(size().width/4); ay = ax*ax*ax; y = (-ay*size().height/4 + size().height/2); return y; } public void paint(Graphics g) { double y; // draw axes g.drawLine(0, size().height/2, size().width - 1, size().height/2); g.drawLine(size().width/2, 0, size().width/2, size().height - 1); // draw functions for (int x = 0 ; x < size().width ; x++) { y = sq(x); if (x >= 0 && y >= 0 && x < size().width && y < size().height) { g.drawLine(x, (int)sq(x), x + 1, (int)sq(x + 1)); } y = cu(x); if (x >= 0 && y >= 0 && x < size().width && y < size().height) { g.drawLine(x, (int)cu(x), x + 1, (int)cu(x + 1)); } } } public String getAppletInfo() { return "draws a x^2 and x^3 graph."; } }