Search in sources :

Example 76 with Graphics2D

use of java.awt.Graphics2D 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 77 with Graphics2D

use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.

the class LineObjectGUI method drawMathObject.

public void drawMathObject(LineObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPositionWithLineThickness(object, pageOrigin, zoomLevel, object.getThickness());
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(sap.getLineThickness()));
    GridPoint[] points = object.getAdjustedVertices();
    for (int i = 0; i < points.length; i++) {
        points[i] = new GridPoint((int) (points[i].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (points[i].gety() * sap.getHeight()) + sap.getyOrigin());
    }
    // TODO - Implements snapping to vertical or horizontal, this should probably be in the back-end
    if (Math.abs(points[0].getx() - points[1].getx()) < 5) {
        points[0].setx(points[1].getx());
    } else if (Math.abs(points[0].gety() - points[1].gety()) < 5) {
        points[0].sety(points[1].gety());
    }
    if (object.getLineColor() == null) {
        g.setColor(Color.BLACK);
    } else {
        g.setColor(object.getLineColor());
    }
    g2d.drawLine((int) points[0].getx(), (int) points[0].gety(), (int) points[1].getx(), (int) points[1].gety());
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) GridPoint(doc.GridPoint) GridPoint(doc.GridPoint) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 78 with Graphics2D

use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.

the class OvalObjectGUI method drawMathObject.

public void drawMathObject(OvalObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    g.setColor(Color.BLACK);
    ScaledSizeAndPosition sap = getSizeAndPositionWithLineThickness(object, pageOrigin, zoomLevel, object.getThickness());
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(sap.getLineThickness()));
    if (object.getColor() != null) {
        g2d.setColor(object.getColor());
        g2d.fill(new Ellipse2D.Double(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight()));
    }
    g2d.setColor(Color.BLACK);
    g2d.draw(new Ellipse2D.Double(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight()));
    //reset graphics object to draw without additional thickness
    g2d.setStroke(new BasicStroke());
}
Also used : BasicStroke(java.awt.BasicStroke) Ellipse2D(java.awt.geom.Ellipse2D) Graphics2D(java.awt.Graphics2D)

Example 79 with Graphics2D

use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.

the class PolygonObjectGUI method drawMathObject.

public void drawMathObject(PolygonObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    g.setColor(Color.BLACK);
    ScaledSizeAndPosition sap = getSizeAndPositionWithLineThickness(object, pageOrigin, zoomLevel, object.getThickness());
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(sap.getLineThickness()));
    Polygon p = new Polygon();
    GridPoint[] points = object.getAdjustedVertices();
    for (int i = 0; i < points.length; i++) {
        p.addPoint((int) (points[i].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (points[i].gety() * sap.getHeight()) + sap.getyOrigin());
    }
    if (object.getColor() != null) {
        g2d.setColor(object.getColor());
        g2d.fillPolygon(p);
        g2d.setColor(Color.BLACK);
    }
    g2d.drawPolygon(p);
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) GridPoint(doc.GridPoint) Polygon(java.awt.Polygon) GridPoint(doc.GridPoint) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 80 with Graphics2D

use of java.awt.Graphics2D in project OpenNotebook by jaltekruse.

the class TextObjectGUI method drawMathObject.

public void drawMathObject(TextObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
    if (!object.getText().equals("")) {
        Font f = g.getFont();
        String message = object.getText();
        g.setFont(f.deriveFont(sap.getFontSize()));
        g.setColor(Color.BLACK);
        Graphics2D graphics2D = (Graphics2D) g;
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        AttributedString messageAS = new AttributedString(message);
        messageAS.addAttribute(TextAttribute.FONT, g.getFont());
        AttributedCharacterIterator messageIterator = messageAS.getIterator();
        FontRenderContext messageFRC = graphics2D.getFontRenderContext();
        LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
        Insets insets = new Insets(2, 2, 2, 2);
        float wrappingWidth = sap.getWidth() - insets.left - insets.right;
        float x = sap.getxOrigin() + insets.left;
        float y = sap.getyOrigin() + insets.top;
        try {
            TextLayout textLayout;
            while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
                textLayout = messageLBM.nextLayout(wrappingWidth);
                y += textLayout.getAscent();
                if (object.getAlignment().equals(TextObject.LEFT)) {
                    textLayout.draw(graphics2D, x, y);
                } else if (object.getAlignment().equals(TextObject.RIGHT)) {
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()), y);
                } else {
                    //centered
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()) / 2, y);
                }
                y += textLayout.getDescent() + textLayout.getLeading();
                x = sap.getxOrigin() + insets.left;
            }
        } catch (Exception e) {
        // TODO - logging and error reporting to user
        //				System.out.println("error with text rendering");
        }
        object.setHeight((int) ((y - sap.getyOrigin()) / zoomLevel));
        g.setFont(f);
    } else {
        // draw the black box around the text box if nothing is in it
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
    }
    if (((BooleanAttribute) object.getAttributeWithName(TextObject.SHOW_BOX)).getValue()) {
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
    }
}
Also used : BooleanAttribute(doc.attributes.BooleanAttribute) Insets(java.awt.Insets) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext)

Aggregations

Graphics2D (java.awt.Graphics2D)777 BufferedImage (java.awt.image.BufferedImage)379 Color (java.awt.Color)180 Font (java.awt.Font)77 BasicStroke (java.awt.BasicStroke)75 Rectangle (java.awt.Rectangle)65 Point (java.awt.Point)63 Rectangle2D (java.awt.geom.Rectangle2D)59 AffineTransform (java.awt.geom.AffineTransform)55 GradientPaint (java.awt.GradientPaint)48 Dimension (java.awt.Dimension)46 Paint (java.awt.Paint)44 FontMetrics (java.awt.FontMetrics)35 IOException (java.io.IOException)33 Graphics (java.awt.Graphics)31 Stroke (java.awt.Stroke)30 Image (java.awt.Image)29 RadialGradientPaint (java.awt.RadialGradientPaint)26 VolatileImage (java.awt.image.VolatileImage)26 File (java.io.File)26