1 9 10 package org.nfunk.jepexamples; 11 12 import java.awt.*; 13 14 import org.nfunk.jep.JEP; 15 16 19 public class GraphCanvas extends Canvas { 20 21 22 private int scaleX, scaleY; 23 24 25 private Dimension dimensions; 26 27 28 private Image buffer; 29 30 31 private boolean initializedBuffer, changedFunction, hasError; 32 33 34 private JEP myParser; 35 36 37 private java.awt.TextField exprField; 38 39 42 public GraphCanvas(String initialExpression, 43 java.awt.TextField exprField_in) { 44 scaleX = 1; 45 scaleY = 1; 46 dimensions = getSize(); 47 initializedBuffer = false; 48 changedFunction = true; 49 hasError = true; 50 exprField = exprField_in; 51 initParser(initialExpression); 52 } 53 54 57 private void initParser(String initialExpression) { 58 myParser = new JEP(); 60 61 myParser.setImplicitMul(true); 63 64 myParser.addStandardFunctions(); 66 67 myParser.addStandardConstants(); 69 myParser.addComplex(); 70 71 myParser.addVariable("x",0); 73 74 setExpressionString(initialExpression); 76 } 77 78 81 public void setExpressionString(String newString) { 82 myParser.parseExpression(newString); 84 85 hasError = myParser.hasError(); 87 if (hasError) 88 exprField.setForeground(Color.red); 89 else 90 exprField.setForeground(Color.black); 91 92 changedFunction = true; 93 } 94 95 98 private double getYValue(double xValue) { 99 myParser.addVariable("x", xValue); 101 102 return myParser.getValue(); 103 } 104 105 108 private void paintWhite(Graphics g) { 109 g.setColor(Color.white); 110 g.fillRect(0,0,dimensions.width,dimensions.height); 111 } 112 113 116 private void paintAxes(Graphics g) { 117 g.setColor(new Color(204,204,204)); 118 g.drawLine(0,dimensions.height/2,dimensions.width-1,dimensions.height/2); 119 g.drawLine(dimensions.width/2,0,dimensions.width/2,dimensions.height-1); 120 } 121 122 125 private void paintCurve(Graphics g) { 126 boolean firstpoint=true; 127 int lastX=0, lastY=0; 128 129 g.setColor(Color.black); 130 131 for (int xAbsolute = 0; xAbsolute <= (dimensions.width-1); xAbsolute++) 132 { 133 double xRelative = (xAbsolute - dimensions.width/2)/scaleX; 134 double yRelative = getYValue(xRelative); 135 int yAbsolute = (int)(-yRelative*scaleY + dimensions.height/2); 136 137 if (yAbsolute > dimensions.height) 138 yAbsolute = dimensions.height; 139 if (yAbsolute < -1) 140 yAbsolute = -1; 141 142 if (firstpoint != true) 143 g.drawLine(lastX, lastY, xAbsolute, yAbsolute); 144 else 145 firstpoint = false; 146 147 lastX = xAbsolute; 148 lastY = yAbsolute; 149 } 150 } 151 152 165 public void paint(Graphics g) { 166 boolean changedDimensions = !dimensions.equals(getSize()); 167 168 if (!initializedBuffer || changedDimensions) 170 { 171 dimensions = getSize(); 172 buffer = createImage(dimensions.width,dimensions.height); 173 initializedBuffer = true; 174 } 175 176 Graphics buffergc = buffer.getGraphics(); 178 179 if (changedFunction || changedDimensions) 181 { 182 paintWhite(buffergc); 183 paintAxes(buffergc); 184 if (!hasError) paintCurve(buffergc); 185 changedFunction = false; 186 } 187 188 g.drawImage(buffer, 0, 0, null); 190 } 191 } 192 | Popular Tags |