import ifi.math.function.DiscreteFunction; import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* * Applet which tests the PlotArea class. */ public class SimpleGraphApplet extends Applet implements KeyListener, ItemListener{ // Range double start = 0.1; double stop = 6.28; // GUI elements PlotArea plotarea = new PlotArea(); Choice function_menu = new Choice(); TextField start_prompt = new TextField(8); TextField stop_prompt = new TextField(8); TextField arb_func = new TextField(30); TextArea result_area = new TextArea("", 4, 30, TextArea.SCROLLBARS_VERTICAL_ONLY); /* * The init method builds the user interface. It is also responsible * for initializing the sampled functions. */ public void init(){ // BorderLayout is a good choice of LayoutManager for this // applet. We use the CENTER panel to hold the PlotArea, and the // the NORTH panel to hold the other components. setLayout(new BorderLayout()); Panel north = new Panel(); north.setLayout(new GridLayout(2, 3)); add(north, BorderLayout.NORTH); north.add(new Label("Choose function: ")); function_menu.add("Arbitrary function"); function_menu.add("sin(x)"); function_menu.add("cos(x)"); function_menu.add("tan(x)"); function_menu.add("atn(x)"); function_menu.add("exp(x)"); function_menu.add("log(x)"); function_menu.add("sqrt(x)"); function_menu.add("abs(x)"); function_menu.select(1); north.add(function_menu); function_menu.addItemListener(this); north.add(new Label(" Function: ")); north.add(arb_func); arb_func.addKeyListener(this); north.add(new Label(" start =")); start_prompt.setText("0.1"); start_prompt.addKeyListener(this); north.add(start_prompt); north.add(new Label(" stop =")); stop_prompt.setText("6.28"); stop_prompt.addKeyListener(this); north.add(stop_prompt); result_area.setEditable(false); add(result_area, BorderLayout.SOUTH); add(plotarea, BorderLayout.CENTER); // Calculate default function reComputeFunction(); } public void keyPressed(KeyEvent event) {} public void keyTyped(KeyEvent event) {} public void keyReleased(KeyEvent event){ // Check that the key-press came from the start_prompt, // and that the user pressed the enter key. if(event.getComponent() == start_prompt && event.getKeyCode() == KeyEvent.VK_ENTER){ // Get start value from the prompt and recompute the function. try{ start = Double.valueOf(start_prompt.getText()).doubleValue(); }catch(Exception e){ result_area.append("Illigal input in start_prompt!\n" + " Please enter a valid number.\n\n"); start_prompt.setText("0.1"); return; } reComputeFunction(); }else if(event.getComponent() == stop_prompt && event.getKeyCode() == KeyEvent.VK_ENTER){ // Get stop value from the prompt and recompute the function. try{ stop = Double.valueOf(stop_prompt.getText()).doubleValue(); }catch(Exception e){ result_area.append("Illigal input in stop_prompt!\n" + " Please enter a valid number.\n\n"); stop_prompt.setText("0.1"); return; } reComputeFunction(); }else if(event.getComponent() == arb_func && event.getKeyCode() == KeyEvent.VK_ENTER) reComputeFunction(); } public void reComputeFunction(){ //Fixed for simplisity, but could use: = getSize().width - 120; int no_samples = 201; DiscreteFunction f = new DiscreteFunction(no_samples); double x, y, delta = (stop - start) / (no_samples - 1); // Get function type String function; if(function_menu.getSelectedIndex() == 0) function = arb_func.getText(); else function = function_menu.getSelectedItem(); //I use my Interpreter class try{ function = Interpreter.parse(function); }catch(Exception e){ result_area.append("Interpreter Exception: " + e.getMessage() + "\n"); return; } // Compute y=F(x) for all samples for(int i = 0; i < no_samples; i++){ x = start + i * delta; try{ y = Interpreter.eval(function, x); }catch(Exception e){ y = 0; } f.setX(i, x); f.setY(i, y); } // Substitute old plot with new. plotarea.removeAllFunctions(); plotarea.addFunction(f); plotarea.repaint(); } //Recompute when a different function is selected. public void itemStateChanged(ItemEvent e){ reComputeFunction(); } }