package freegraph; import java.awt.*; import java.awt.event.*; import java.applet.*; import freegraph.GraphPlaneListener; /*** * Applet for GraphPlane. Provides an text field to enter functions, and * a list box to view them. The applet can either be "in line," or drawn on * the HTML page, or can be just a button which will create a separate * frame for the graphing. (Because of re-draw problems on IE, it works better * as a separate frame...plus it can be resized.) */ public class GraphingApplet extends Applet implements ActionListener, GraphPlaneListener { private GraphPlane graphPlane; private Panel pnlGraph; private Panel pnlXYLocation; private Panel pnlToolbar; private Panel pnlMain; private Panel pnlList; private Panel pnlWestList; private Panel pnlSouthList; private Label lblXLocation; private Label lblYLocation; private Button btnAdd; private Button btnReset; private Button btnZoomin; private Button btnZoomout; private Button btnClear; private Button btnRemove; private Button btnJumpOutClose; private Button btnSeparateWindow; private TextField txtFunction; private List lstFunctions; private ExpressionEvaluator expr[] = new ExpressionEvaluator[5]; private Color clrs[] = {Color.red, Color.blue, Color.darkGray, Color.green, Color.orange}; private boolean isInFrame = false; private boolean nostub = false; /** * Creates a new GraphingApplet, with the provided InFrame property. * @param inFrame sets the InFrame property for the Applet. If inFrame * is true, the applet does not try to access the Applet's stub because * it is null */ public GraphingApplet(boolean inFrame) { if (inFrame) nostub = true; this.isInFrame = inFrame; } /** * constructs a new GraphingApplet */ public GraphingApplet() { } /** * initializes the Applet by calling internalInit which sets up the GUI */ public void init() { try { internalInit(); } catch (Exception e) { e.printStackTrace(); } } /** * sets up GUI. Applet parameter "justwindowbutton" is used to * determine if only a button to launch the separate window should be * displayed (if set to "true" then just the button is displayed). * The parameter "bgcolor" is used to set the color as a HEX number. */ private void internalInit() throws Exception { if (!isInFrame) setBackground( new Color(getParameterAsInt("bgcolor", 0xFFFFFF, 16))); if (getParameter("justwindowbutton", "false").equals("true")) { btnSeparateWindow = new Button("Start Grapher"); btnSeparateWindow.setFont(new Font("SansSerif", Font.PLAIN, 16)); this.setLayout(new BorderLayout()); this.add(btnSeparateWindow, BorderLayout.CENTER); btnSeparateWindow.addActionListener(this); } else { graphPlane = new GraphPlane(); graphPlane.setBackground(Color.white); btnAdd = new Button("Add"); btnReset = new Button("Reset"); btnZoomin = new Button("+"); btnZoomout = new Button("-"); btnClear = new Button("Clear All"); btnRemove = new Button("Remove"); txtFunction = new TextField(); pnlGraph = new Panel(); pnlMain = new Panel(); pnlList = new Panel(); pnlWestList = new Panel(); pnlSouthList = new Panel(); pnlXYLocation = new Panel(); pnlToolbar = new Panel(); lblXLocation = new Label("X = "); lblYLocation = new Label("Y = "); lstFunctions = new List(); btnJumpOutClose = new Button("Close"); btnSeparateWindow = new Button("Separate Window"); pnlToolbar.setLayout(new BorderLayout()); pnlToolbar.add(btnZoomout, BorderLayout.WEST); pnlToolbar.add(btnReset, BorderLayout.CENTER); pnlToolbar.add(btnZoomin, BorderLayout.EAST); pnlXYLocation.setLayout(new GridLayout(1, 2)); pnlXYLocation.add(lblXLocation); pnlXYLocation.add(lblYLocation); pnlGraph.setLayout(new BorderLayout()); pnlGraph.add(pnlXYLocation, BorderLayout.NORTH); pnlGraph.add(btnAdd, BorderLayout.WEST); pnlGraph.add(txtFunction, BorderLayout.CENTER); pnlMain.setLayout(new BorderLayout()); pnlMain.add(pnlGraph,BorderLayout.SOUTH); pnlMain.add(graphPlane, BorderLayout.CENTER); pnlMain.add(pnlToolbar, BorderLayout.NORTH); pnlWestList.setLayout(new GridLayout(2, 1)); pnlWestList.add(btnRemove); pnlWestList.add(btnClear); pnlSouthList.setBackground(Color.lightGray); pnlSouthList.setLayout(new BorderLayout()); if (isInFrame) pnlSouthList.add(btnJumpOutClose, BorderLayout.EAST); else pnlSouthList.add(btnSeparateWindow, BorderLayout.EAST); pnlList.setLayout(new BorderLayout()); pnlList.add(pnlWestList, BorderLayout.WEST); pnlList.add(lstFunctions, BorderLayout.CENTER); pnlList.add(pnlSouthList, BorderLayout.SOUTH); this.setLayout(new BorderLayout()); this.add(pnlMain, BorderLayout.CENTER); this.add(pnlList, BorderLayout.SOUTH); btnAdd.addActionListener(this); btnReset.addActionListener(this); btnZoomin.addActionListener(this); btnZoomout.addActionListener(this); btnClear.addActionListener(this); btnRemove.addActionListener(this); btnSeparateWindow.addActionListener(this); btnJumpOutClose.addActionListener(this); txtFunction.addActionListener(this); for (int i=0; i= expr.length) lstFunctions.remove(0); lstFunctions.add(function); redrawGraph(); } /** * redraws the graph with all the function in the list box. */ void redrawGraph() { for (int i=0; i