use of expression.Node in project OpenNotebook by jaltekruse.
the class ImpliedMultGraphic method requestSize.
// @Override
// public void sendCursorInFromEast(int yPos, NodeGraphic vg) throws NodeException {
// super.getRootNodeGraphic().getCursor().setValueGraphic(getLeftGraphic().getMostInnerEast());
// super.getRootNodeGraphic().getCursor().setPos(getLeftGraphic().getMostInnerEast().getMaxCursorPos() - 1);
// }
//
// @Override
// public void sendCursorInFromWest(int yPos, NodeGraphic vg){
// super.getRootNodeGraphic().getCursor().setValueGraphic(getRightGraphic().getMostInnerWest());
// super.getRootNodeGraphic().getCursor().getValueGraphic().sendCursorInFromWest(yPos, vg);
// }
@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
g.setFont(f);
setFont(f);
space = (int) Math.round(2 * super.getRootNodeGraphic().getFontSizeAdjustment());
if (space == 0) {
space = 2;
}
Node tempLeft = (super.getValue()).getChild(0);
Node tempRight = (super.getValue()).getChild(1);
NodeGraphic leftValGraphic = null;
NodeGraphic rightValGraphic = null;
int[] rightSize = { 0, 0 };
int[] leftSize = { 0, 0 };
int[] symbolSize = { 0, 0 };
int[] totalSize = { 0, 0 };
leftValGraphic = makeNodeGraphic(tempLeft);
super.getRootNodeGraphic().getComponents().add(leftValGraphic);
leftSize = leftValGraphic.requestSize(g, f, x1, y1);
rightValGraphic = makeNodeGraphic(tempRight);
rightSize = rightValGraphic.requestSize(g, f, x1, y1);
super.getRootNodeGraphic().getComponents().add(rightValGraphic);
// set the west and east fields for inside an outside of the expression
setMostInnerWest(leftValGraphic.getMostInnerWest());
leftValGraphic.getMostInnerEast().setEast(this);
this.setWest(leftValGraphic.getMostInnerEast());
setMostInnerNorth(this);
setMostInnerSouth(this);
setMostInnerEast(rightValGraphic.getMostInnerEast());
rightValGraphic.getMostInnerWest().setWest(this);
this.setEast(rightValGraphic.getMostInnerWest());
symbolSize[0] = space;
symbolSize[1] = getRootNodeGraphic().getFontHeight(f);
rightValGraphic.shiftToX1(leftSize[0] + symbolSize[0] + x1);
int height = 0;
if (leftValGraphic.getUpperHeight() > rightValGraphic.getUpperHeight()) {
height = leftValGraphic.getUpperHeight();
symbolY1 = leftValGraphic.getUpperHeight() + y1 - (int) (Math.round((symbolSize[1] / 2.0)));
symbolY2 = symbolY1 + symbolSize[1];
setUpperHeight(leftValGraphic.getUpperHeight());
rightValGraphic.shiftToY1(y1 - rightValGraphic.getUpperHeight() + leftValGraphic.getUpperHeight());
} else {
height = rightValGraphic.getUpperHeight();
symbolY1 = rightValGraphic.getUpperHeight() + y1 - (int) (Math.round((symbolSize[1] / 2.0)));
symbolY2 = symbolY1 + symbolSize[1];
setUpperHeight(rightValGraphic.getUpperHeight());
leftValGraphic.shiftToY1(y1 - leftValGraphic.getUpperHeight() + rightValGraphic.getUpperHeight());
}
if (leftValGraphic.getLowerHeight() > rightValGraphic.getLowerHeight()) {
height += leftValGraphic.getLowerHeight();
setLowerHeight(leftValGraphic.getLowerHeight());
} else {
height += rightValGraphic.getLowerHeight();
setLowerHeight(rightValGraphic.getLowerHeight());
}
symbolX1 = x1 + leftSize[0];
symbolX2 = x1 + leftSize[0] + symbolSize[0];
setLeftGraphic(leftValGraphic);
setRightGraphic(rightValGraphic);
totalSize[0] = symbolX2 + rightSize[0] - x1;
totalSize[1] = height;
super.setX1(x1);
super.setY1(y1);
super.setX2(x1 + totalSize[0]);
super.setY2(y1 + totalSize[1]);
return totalSize;
}
use of expression.Node in project OpenNotebook by jaltekruse.
the class ExpressionObjectGUI method drawInteractiveComponents.
public void drawInteractiveComponents(ExpressionObject object, Graphics g, Point pageOrigin, float zoomLevel) {
g.setColor(Color.BLACK);
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
int outerBufferSpace = (int) (5 * zoomLevel);
int stepBufferSpace = (int) (10 * zoomLevel);
Graphics2D g2d = (Graphics2D) g;
RootNodeGraphic rootGraphic;
if (!(object.getExpression().equals("") && object.allAnswersBlank())) {
// if any of the steps cannot be rendered, this information will allow
// space to be left for printing an error message in its place
g.setFont(g.getFont().deriveFont(sap.getFontSize()));
int errorMessageHeight = g.getFontMetrics().getHeight();
int errorMessageWidth = g.getFontMetrics().stringWidth(EX_ERROR);
Node n = null;
int totalHeight = 0;
int greatestWidth = 0;
Vector<Integer> indeciesInError = new Vector<>();
Vector<Integer> yPosOfSteps = new Vector<>();
int currentIndex = 0;
Vector<RootNodeGraphic> expressions = new Vector<>();
// add the expression
try {
n = parser.parseNode(object.getExpression());
boolean objectChanged = currentSelectionStep || current == null || currentExObj != object || !object.getDecimalRectangleBounds().equals(currentPosSize) || !currentEx.equals(object.getExpression());
if (objectChanged) {
rootGraphic = new RootNodeGraphic(n);
rootGraphic.generateExpressionGraphic(g, outerBufferSpace + sap.getxOrigin(), outerBufferSpace + sap.getyOrigin(), (int) sap.getFontSize(), zoomLevel);
} else if (currentZoom != zoomLevel) {
rootGraphic = current;
rootGraphic.requestSize(g, outerBufferSpace + sap.getxOrigin(), outerBufferSpace + sap.getyOrigin(), (int) sap.getFontSize(), zoomLevel);
} else {
rootGraphic = current;
rootGraphic.setGraphics((Graphics2D) g);
}
// keep these next three lines in the try catch block, they should only happen
// if they line above does not throw an error
expressions.add(rootGraphic);
yPosOfSteps.add(rootGraphic.yPos);
totalHeight = rootGraphic.getHeight();
greatestWidth = rootGraphic.getWidth();
currentSelectionStep = false;
current = rootGraphic;
currentExObj = object;
currentEx = object.getExpression();
currentPosSize = object.getDecimalRectangleBounds();
currentZoom = zoomLevel;
} catch (Exception e) {
indeciesInError.add(currentIndex);
yPosOfSteps.add(outerBufferSpace + sap.getyOrigin());
expressions.add(null);
totalHeight += errorMessageHeight;
greatestWidth = errorMessageWidth;
}
Vector<StringAttribute> steps = (Vector<StringAttribute>) object.getListWithName(ExpressionObject.STEPS).getValues();
// TODO - confirm if this works with multiple correct answers
for (StringAttribute strAtt : object.getCorrectAnswers()) {
if (!strAtt.getValue().equals("")) {
steps.add(strAtt);
}
}
// add the steps to the list of expressions to render
String s;
int i = 0;
for (StringAttribute mAtt : steps) {
s = mAtt.getValue();
currentIndex++;
totalHeight += stepBufferSpace;
try {
n = parser.parseNode(s);
rootGraphic = new RootNodeGraphic(n);
// current = rootGraphic;
rootGraphic.generateExpressionGraphic(g, sap.getxOrigin() + outerBufferSpace, outerBufferSpace + sap.getyOrigin() + totalHeight, (int) sap.getFontSize(), zoomLevel);
expressions.add(rootGraphic);
yPosOfSteps.add(rootGraphic.yPos);
if (rootGraphic.getWidth() > greatestWidth) {
greatestWidth = rootGraphic.getWidth();
}
totalHeight += rootGraphic.getHeight();
i++;
} catch (Exception e) {
indeciesInError.add(currentIndex);
totalHeight += errorMessageHeight;
if (errorMessageWidth > greatestWidth) {
greatestWidth = errorMessageWidth;
}
yPosOfSteps.add(outerBufferSpace + sap.getyOrigin() + totalHeight);
}
}
// remove the correct answers, so they are not permanently added as steps
steps.removeAll(object.getCorrectAnswers());
if (object.getColor() != null) {
g.setColor(object.getColor());
} else {
g.setColor(Color.white);
}
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), greatestWidth + 2 * outerBufferSpace, totalHeight + 2 * outerBufferSpace);
g.setColor(Color.BLACK);
int index = 0;
int numberOfSteps = steps.size() + 1;
for (RootNodeGraphic r : expressions) {
try {
if (indeciesInError.contains(index)) {
g.setFont(g.getFont().deriveFont(sap.getFontSize()));
g.setColor(Color.RED);
g.drawString(EX_ERROR, sap.getxOrigin() + outerBufferSpace, yPosOfSteps.get(index) + errorMessageHeight);
g.setColor(Color.BLACK);
index++;
continue;
} else {
if (index >= numberOfSteps) {
// draw the answers with a highlight
g.setColor(new Color(180, 255, 100));
g.fillRect(r.xPos - 4, r.yPos - 4, r.getWidth() + 8, r.getHeight() + 8);
g.setColor(Color.BLACK);
}
if (index != 0)
r.setCursor(new Cursor(null, 0));
r.draw();
}
} catch (NodeException e) {
e.printStackTrace();
g.setFont(g.getFont().deriveFont(sap.getFontSize()));
g.setColor(Color.RED);
g.drawString(EX_ERROR, r.xPos, r.yPos + errorMessageHeight);
}
index++;
}
if (greatestWidth > 0 && totalHeight > 0) {
object.setWidth((int) ((greatestWidth + 2 * outerBufferSpace) / zoomLevel));
object.setHeight((int) ((totalHeight + 2 * outerBufferSpace) / zoomLevel));
}
} else {
g2d.setStroke(new BasicStroke());
g2d.setPaint(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
}
use of expression.Node in project OpenNotebook by jaltekruse.
the class ExpressionObjectGUI method drawMathObject.
public void drawMathObject(ExpressionObject object, Graphics g, Point pageOrigin, float zoomLevel) {
if (object.isAlwaysShowingSteps()) {
drawInteractiveComponents(object, g, pageOrigin, zoomLevel);
return;
}
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
int bufferSpace = (int) (5 * zoomLevel);
// if any of the steps cannot be rendered, this information will allow
// space to be left to print an error message in its place
g.setFont(g.getFont().deriveFont(sap.getFontSize()));
int errorMessageHeight = g.getFontMetrics().getHeight();
int errorMessageWidth = g.getFontMetrics().stringWidth(EX_ERROR);
RootNodeGraphic ceg;
try {
g.setColor(object.getColor());
if (!object.getExpression().equals("")) {
Node n = parser.parseNode(object.getExpression());
ceg = new RootNodeGraphic(n);
ceg.generateExpressionGraphic(g, bufferSpace + sap.getxOrigin(), bufferSpace + sap.getyOrigin(), (int) sap.getFontSize(), zoomLevel);
object.setWidth((int) (ceg.getWidth() / zoomLevel) + 10);
object.setHeight((int) (ceg.getHeight() / zoomLevel) + 10);
if (object.getColor() != null) {
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
}
g.setColor(Color.BLACK);
ceg.getCursor().setValueGraphic(null);
ceg.draw();
} else {
if (object.getColor() != null) {
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
}
g.setColor(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
} catch (Exception e) {
// TODO Auto-generated catch block
object.setHeight((int) (errorMessageHeight / zoomLevel) + 10);
object.setWidth((int) (errorMessageWidth / zoomLevel) + 10);
g.setFont(g.getFont().deriveFont(sap.getFontSize()));
g.setColor(Color.RED);
g.drawString(EX_ERROR, sap.getxOrigin() + bufferSpace, sap.getyOrigin() + bufferSpace + errorMessageHeight);
}
}
use of expression.Node in project OpenNotebook by jaltekruse.
the class DivisionGraphic method requestSize.
@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
g.setFont(f);
setFont(f);
sizeOverhang = (int) (3 * getRootNodeGraphic().getFontSizeAdjustment());
spaceAroundBar = (int) (5 * getRootNodeGraphic().getFontSizeAdjustment());
Node tempLeft = (super.getValue()).getChild(0);
Node tempRight = (super.getValue()).getChild(1);
NodeGraphic leftValGraphic = null;
NodeGraphic rightValGraphic = null;
int[] rightSize = { 0, 0 };
int[] leftSize = { 0, 0 };
int[] symbolSize = { 0, 0 };
int[] totalSize = { 0, 0 };
if (style == Style.SLASH) {
leftValGraphic = makeNodeGraphic(tempLeft);
super.getRootNodeGraphic().getComponents().add(leftValGraphic);
leftSize = leftValGraphic.requestSize(g, f, x1, y1);
symbolSize[0] = getRootNodeGraphic().getStringWidth(getValue().getOperator().getSymbol(), f);
symbolSize[1] = getRootNodeGraphic().getFontHeight(f);
symbolX1 = x1 + leftSize[0];
symbolY1 = y1 + ((int) Math.round(leftSize[1] / 2.0) - (int) (Math.round(symbolSize[1]) / 2.0));
symbolX2 = x1 + leftSize[0] + symbolSize[0];
symbolY2 = symbolSize[1] + symbolY1;
rightValGraphic = makeNodeGraphic(tempRight);
rightSize = rightValGraphic.requestSize(g, f, symbolX2, y1);
super.getRootNodeGraphic().getComponents().add(rightValGraphic);
// set the west and east fields for inside an outside of the expression
setMostInnerWest(leftValGraphic.getMostInnerWest());
leftValGraphic.getMostInnerEast().setEast(this);
this.setWest(leftValGraphic.getMostInnerEast());
setMostInnerEast(rightValGraphic.getMostInnerEast());
rightValGraphic.getMostInnerWest().setWest(this);
this.setEast(rightValGraphic.getMostInnerWest());
setLeftGraphic(leftValGraphic);
setRightGraphic(rightValGraphic);
totalSize[0] = symbolX2 + rightSize[0] - x1;
totalSize[1] = symbolY2 - y1;
setUpperHeight((int) Math.round((totalSize[1] / 2.0)));
setLowerHeight(getUpperHeight());
super.setX1(x1);
super.setY1(y1);
super.setX2(x1 + totalSize[0]);
super.setY2(y1 + totalSize[1]);
return totalSize;
} else if (style == Style.HORIZONTAL) {
// prevent parenthesis from displaying if not used twice to show they are explicitly wanted
tempLeft.setDisplayParentheses(false);
leftValGraphic = makeNodeGraphic(tempLeft);
leftSize = leftValGraphic.requestSize(g, f, x1, y1);
super.getRootNodeGraphic().getComponents().add(leftValGraphic);
// other if statements for checking the left, decimal, imaginary, other val types
// prevent parenthesis from displaying if not used twice to show they are explicitly wanted
tempRight.setDisplayParentheses(false);
rightValGraphic = makeNodeGraphic(tempRight);
rightSize = rightValGraphic.requestSize(g, f, symbolX2, y1);
super.getRootNodeGraphic().getComponents().add(rightValGraphic);
setHeightNumer(leftSize[1]);
setHeightDenom(rightSize[1]);
// set the west and east fields for inside an outside of the expression
setMostInnerWest(this);
setMostInnerNorth(leftValGraphic.getMostInnerNorth());
leftValGraphic.getMostInnerEast().setEast(this);
leftValGraphic.getMostInnerWest().setWest(this);
leftValGraphic.getMostInnerSouth().setSouth(this);
// this.setWest(leftValGraphic.getMostInnerEast());
setMostInnerEast(this);
// setSouth(rightValGraphic.getMostInnerNorth());
setMostInnerSouth(rightValGraphic.getMostInnerSouth());
rightValGraphic.getMostInnerNorth().setNorth(this);
rightValGraphic.getMostInnerWest().setWest(this);
rightValGraphic.getMostInnerEast().setEast(this);
// this.setEast(rightValGraphic.getMostInnerWest());
south = rightValGraphic;
north = leftValGraphic;
symbolX1 = x1;
if (leftSize[0] > rightSize[0]) {
symbolX2 = x1 + leftSize[0] + 2 * sizeOverhang;
} else {
symbolX2 = x1 + rightSize[0] + 2 * sizeOverhang;
}
leftValGraphic.shiftToX1((int) (Math.round(((symbolX2 - symbolX1) - leftSize[0])) / 2.0) + x1);
// leftValGraphic.shiftToX2(leftValGraphic.getX1() + leftSize[0]);
symbolY1 = leftSize[1] + y1;
symbolY2 = symbolY1 + 1 + 2 * spaceAroundBar;
setLeftGraphic(leftValGraphic);
setRightGraphic(rightValGraphic);
rightValGraphic.shiftToX1((int) (Math.round(((symbolX2 - symbolX1) - rightSize[0])) / 2.0) + x1);
// rightValGraphic.shiftToX2(rightValGraphic.getX1() + rightSize[0]);
rightValGraphic.shiftToY1(symbolY2);
// rightValGraphic.shiftToY2(rightValGraphic.getY1() + rightSize[1]);
setUpperHeight(leftSize[1] + spaceAroundBar);
setLowerHeight(rightSize[1] + spaceAroundBar);
totalSize[0] = symbolX2 - x1;
totalSize[1] = symbolY2 + rightSize[1] - y1;
super.setX1(x1);
super.setY1(y1);
super.setX2(x1 + totalSize[0]);
super.setY2(y1 + totalSize[1]);
return totalSize;
}
return null;
}
use of expression.Node in project OpenNotebook by jaltekruse.
the class BasicDistributiveProperty method generateExpression.
@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
Node[] n = new Node[2];
if (difficulty == ProblemGenerator.EASY) {
double bigNum = ExUtil.randomInt(2, 10, true) * 10, smallNum = ExUtil.randomInt(1, 5, true), multiplier = ExUtil.randomInt(2, 9, true);
if (ExUtil.randomBoolean()) {
n[0] = new Expression(new Operator.Addition(), new Number(bigNum), new Number(smallNum));
} else {
n[0] = new Expression(new Operator.Subtraction(), new Number(bigNum), new Number(smallNum));
}
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
} else if (difficulty == ProblemGenerator.MEDIUM) {
double multiplier = ExUtil.randomInt(2, 9, true);
n[0] = ExUtil.randomLinearExpression(ExUtil.randomVarName(), 1, 8);
n[0].setDisplayParentheses(true);
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
} else {
double multiplier = ExUtil.randomInt(2, 14, true);
String varName = ExUtil.randomVarName();
n[0] = ExUtil.randomLinearExpression(varName, 1, 12);
Node newNode = null;
if (ExUtil.randomBoolean())
newNode = new Number(ExUtil.randomInt(2, 15, true));
else
newNode = ExUtil.randomTerm(1, varName, 2, 9);
if (ExUtil.randomBoolean()) {
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), n[0], newNode);
} else {
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Subtraction(), n[0], newNode);
}
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
}
n[1] = n[0].cloneNode().smartNumericSimplify().standardFormat();
return n;
}
Aggregations