/****************************************************************************** * Copyright (C) 2007 Curran Kelleher * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . *******************************************************************************/ package sampleCode; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JApplet; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTextField; import mathParser.EvaluationTree; import mathParser.MathParser; import mathParser.MathParserException; import mathParser.Variable; /** * An animated 2D grapher which uses the math parser * * @author Curran Kelleher * */ @SuppressWarnings("serial") public class GrapherExample extends JApplet { /** * The number of points used to create the graphed curve */ int numberOfPoints = 300; /** * The initial function string */ String initialFunction = "y = sin(x*sin(t/10)+t)*x"; /** * The parser used to parse the function */ MathParser parser = new MathParser(); /** * The function which assigns y from the current value of x */ EvaluationTree function = parser.parse(initialFunction); public void init() { // create the text field final JTextField textField = new JTextField(initialFunction); textField.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyChar() == '\n') try { function = parser.parse(textField.getText()); } catch (MathParserException x) { JOptionPane.showMessageDialog(null, x.message, "Parse Error", JOptionPane.ERROR_MESSAGE); } } }); // create the graphing panel final JPanel panel = new JPanel() { Variable xVariable = parser.getVariable("x"); Variable yVariable = parser.getVariable("y"); public void paint(Graphics g) { int width = getWidth(); int height = getHeight(); g.clearRect(0, 0, width, height); int currentYPixel, previousYPixel = 0; for (int xPixel = 0; xPixel < width; xPixel++) { // translate from [0,width] to [-10,10] and // put the value in the x variable xVariable.value = ((double) xPixel / width) * 20 - 10; // calculate y by evaluating the function function.evaluate(); // translate from [-10,10] to [0,height] currentYPixel = (int) ((yVariable.value + 10) / 20 * height); // draw the line if we have calculated the previous y if (xPixel != 0) g.drawLine(xPixel - 1, previousYPixel, xPixel, currentYPixel); // make the previous y the current y previousYPixel = currentYPixel; } } }; // add the text field and panel to the applet using a split pane JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textField, panel); splitPane.setDividerSize(0); add(splitPane); // create and start the animation thread (new Thread(new Runnable() { public void run() { Variable tVariable = parser.getVariable("t"); while (true) { tVariable.value += 0.05; if (!panel.isPaintingTile()) panel.repaint(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } })).start(); } } /* * CVS Log * * $Log: GrapherExample.java,v $ * Revision 1.2 2007/11/30 01:33:02 curran * Added exception handling with error dialogs to the grapher example applet, and made the split pane have no border to it looks nicer * Revision 1.1 2007/11/27 23:55:21 curran Created * the grapher example applet * * */