// Import der Java-Klassen import java.awt.*; import java.awt.event.*; import javax.swing.*; import JSci.awt.*; import JSci.swing.*; // Vector aus Java 1.5 ! import java.util.Vector; // Rhino-Klassen import org.mozilla.javascript.*; /* * GraphDemo * * Beispiel für Embedded JavaScript mit Rhino * http://www.linkwerk.com/pub/javascript/rhino/ * (c) Linkwerk GmbH 2005 * */ public class GraphDemo extends Frame { private DefaultGraph2DModel valueModel; private static final float DEFAULT_MINX = -10.0f; private static final float DEFAULT_MAXX = -10.0f; private static final float DEFAULT_STEPX = -0.25f; private Context cx; private Scriptable scope; // ++ main public static void main(String args[]) { // Erzeuge einen String aus den Argumenten String s = ""; for (int i=0; i < args.length; i++) { s += args[i] + " "; } new GraphDemo(s.trim()); } // ++ GraphDemo public GraphDemo(String script) { super("JSci Graph Demo"); // Setze einen WindowListener für das "windowClosing" Ereignis addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); System.exit(0); } }); // Rhino: Context erzeugen und betreten cx = Context.enter(); try { // Rhino: Initialisierung der Standard-Objekte (Object, Function, etc.) scope = cx.initStandardObjects(); // Rhino: Evaluiere das JavaScript Skript und ignoriere das Ergebnis cx.evaluateString(scope, script, "", 1, null); // Rhino: Funktion f prüfen if(! (scope.get("f", scope) instanceof Function)) { throw new EvaluatorException("f is undefined or not a function."); } // Rhino: Variable minX, maxX und stepX holen und ggf. Default-Werte setzen float minX = scope.get("minX", scope) == Scriptable.NOT_FOUND ? DEFAULT_MINX : (float)cx.toNumber(scope.get("minX", scope)); float maxX = scope.get("maxX", scope) == Scriptable.NOT_FOUND ? DEFAULT_MAXX : (float)cx.toNumber(scope.get("maxX", scope)); float stepX = scope.get("stepX", scope) == Scriptable.NOT_FOUND ? DEFAULT_STEPX : (float)cx.toNumber(scope.get("stepX", scope)); // Rhino: Funktion f holen Function f = (Function)scope.get("f", scope); // JSci, Rhino: Datenmodell erzeugen valueModel = createValueData(minX, maxX, stepX, f); // JSci: Line graph konstruieren und Datenmodell setzen setSize(700,600); final Font titleFont=new Font("Default",Font.BOLD,14); Label title=new Label("Function plot",Label.CENTER); title.setFont(titleFont); JLineGraph lineGraph=new JLineGraph(valueModel); lineGraph.setGridLines(true); lineGraph.setMarker(new Graph2D.DataMarker.Circle(1)); final Panel lineGraphPanel=new Panel(new JGraphLayout()); lineGraphPanel.add(title, JGraphLayout.TITLE); lineGraphPanel.add(lineGraph, JGraphLayout.GRAPH); lineGraphPanel.add(new Label("x-axis",Label.CENTER), JGraphLayout.X_AXIS); lineGraphPanel.add(new Label("y-axis",Label.CENTER), JGraphLayout.Y_AXIS); // JSci: Layout erzeugen GridBagLayout gb=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); setLayout(gb); gbc.weightx=0.5;gbc.weighty=0.5; gbc.fill=GridBagConstraints.HORIZONTAL; gbc.gridx=0;gbc.gridy=1; gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.fill=GridBagConstraints.BOTH; gbc.gridx=0;gbc.gridy=2; gbc.gridwidth=1; gb.setConstraints(lineGraphPanel, gbc); add(lineGraphPanel); setVisible(true); } catch(EvaluatorException e) { System.err.println("Error in function script: " + e.getMessage()); } catch(Exception e) { System.err.println(e.toString()); } finally { // Context verlassen Context.exit(); } } // ++ createValueData private DefaultGraph2DModel createValueData(float minX, float maxX, float stepX, Function f) { Vector fv = new Vector(); for(float i = minX; i <= maxX; i += stepX) { // Rhino: Aufruf der Funktion f Object functionArgs[] = { i }; Object result = f.call(cx, scope, scope, functionArgs); // Ergebnis in Vektor speichern fv.add((float)cx.toNumber(result)); } // JSci: Erzeuge Datenmodell DefaultGraph2DModel model=new DefaultGraph2DModel(); model.setXAxis(minX, maxX, fv.size()); model.addSeries(GraphDemo.convertVectorToArray(fv)); model.setSeriesVisible(0, false); return model; } // ++ convertVectorToArray public static float[] convertVectorToArray(Vector vector) { float[] floatArray = new float[vector.size()]; for(int i = 0; i < vector.size(); i++) { floatArray[i] = vector.get(i); } return floatArray; } }