Search in sources :

Example 1 with RootNodeGraphic

use of math_rendering.RootNodeGraphic 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());
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Node(expression.Node) Color(java.awt.Color) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) RootNodeGraphic(math_rendering.RootNodeGraphic) Cursor(math_rendering.Cursor) Point(java.awt.Point) NodeException(expression.NodeException) Graphics2D(java.awt.Graphics2D) Vector(java.util.Vector)

Example 2 with RootNodeGraphic

use of math_rendering.RootNodeGraphic 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);
    }
}
Also used : Node(expression.Node) RootNodeGraphic(math_rendering.RootNodeGraphic) Point(java.awt.Point) NodeException(expression.NodeException)

Aggregations

Node (expression.Node)2 NodeException (expression.NodeException)2 Point (java.awt.Point)2 RootNodeGraphic (math_rendering.RootNodeGraphic)2 StringAttribute (doc.attributes.StringAttribute)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 Vector (java.util.Vector)1 Cursor (math_rendering.Cursor)1