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());
}
}
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));
}
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());
}
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));
}
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());
}
}
Aggregations