Search in sources :

Example 31 with Color

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

the class ColorAttribute method readValueFromString.

@Override
public Color readValueFromString(String s) throws AttributeException {
    try {
        if (s.equals("null")) {
            return null;
        }
        StringTokenizer st = new StringTokenizer(s, ",");
        int red = Integer.parseInt(st.nextToken());
        int green = Integer.parseInt(st.nextToken());
        int blue = Integer.parseInt(st.nextToken());
        return new Color(red, green, blue);
    } catch (Exception e) {
        throw new AttributeException("error setting color");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Color(java.awt.Color)

Example 32 with Color

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

the class ColorAdjustmentPanel method addPanelContent.

@Override
public void addPanelContent() {
    setLayout(new GridBagLayout());
    if (mAtt.getName().equals(MathObject.FILL_COLOR)) {
        checkbox = new JCheckBox("fill");
        if (mAtt.getValue() != null)
            checkbox.setSelected(true);
        else
            checkbox.setSelected(false);
        checkbox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    if (justChangedColor) {
                        justChangedColor = false;
                        return;
                    }
                    if (mAtt.getValue() == null) {
                        mAtt.setValue(Color.WHITE);
                    }
                    colorLabel.setBackground(mAtt.getValue());
                    ColorAdjustmentPanel.this.repaint();
                    notebookPanel.getCurrentDocViewer().addUndoState();
                    notebookPanel.getCurrentDocViewer().repaintDoc();
                    notebookPanel.getCurrentDocViewer().updateObjectToolFrame();
                } else {
                    mAtt.setValue(null);
                    colorLabel.setBackground(mAtt.getValue());
                    ColorAdjustmentPanel.this.repaint();
                    notebookPanel.getCurrentDocViewer().addUndoState();
                    notebookPanel.getCurrentDocViewer().repaintDoc();
                    notebookPanel.getCurrentDocViewer().updateObjectToolFrame();
                }
            }
        });
    }
    colorLabel = new JLabel("    ");
    colorLabel.setBorder(new LineBorder(Color.BLACK));
    colorLabel.setOpaque(true);
    colorLabel.setBackground(mAtt.getValue());
    colorLabel.addMouseListener(new MouseListener() {

        public void mouseClicked(MouseEvent arg0) {
        }

        public void mouseEntered(MouseEvent arg0) {
        }

        public void mouseExited(MouseEvent arg0) {
        }

        public void mousePressed(MouseEvent arg0) {
            Color c = JColorChooser.showDialog(ColorAdjustmentPanel.this, "Choose color", ((ColorAttribute) mAtt).getValue());
            ((ColorAttribute) mAtt).setValue(c);
            colorLabel.setBackground(mAtt.getValue());
            if (checkbox != null) {
                if (((ColorAttribute) mAtt).getValue() != null) {
                    justChangedColor = true;
                    checkbox.setSelected(true);
                } else {
                    checkbox.setSelected(false);
                }
            }
            notebookPanel.getCurrentDocViewer().addUndoState();
            notebookPanel.getCurrentDocViewer().repaintDoc();
        }

        public void mouseReleased(MouseEvent arg0) {
        }
    });
    GridBagConstraints con = new GridBagConstraints();
    con.fill = GridBagConstraints.HORIZONTAL;
    con.weightx = 1;
    con.gridx = 0;
    con.gridy = 0;
    con.insets = new Insets(0, 0, 0, 3);
    if (mAtt.getName().equals(MathObject.FILL_COLOR)) {
        add(checkbox, con);
    } else {
        add(new JLabel(mAtt.getName()), con);
    }
    con.gridy = 0;
    con.gridx = 1;
    con.gridheight = 1;
    add(colorLabel, con);
//		con.gridx = 2;
//		add(setColor, con);
}
Also used : ItemEvent(java.awt.event.ItemEvent) GridBagConstraints(java.awt.GridBagConstraints) MouseEvent(java.awt.event.MouseEvent) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) LineBorder(javax.swing.border.LineBorder) Color(java.awt.Color) JLabel(javax.swing.JLabel) JCheckBox(javax.swing.JCheckBox) MouseListener(java.awt.event.MouseListener) ItemListener(java.awt.event.ItemListener) ColorAttribute(doc.attributes.ColorAttribute)

Example 33 with Color

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

the class CubeObjectGUI method drawMathObject.

public void drawMathObject(CubeObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    g.setColor(Color.BLACK);
    ScaledSizeAndPosition sap = getSizeAndPositionWithLineThickness(object, pageOrigin, zoomLevel, object.getThickness());
    Graphics2D g2d = (Graphics2D) g;
    Color fillColor = object.getColor();
    Polygon fillShape;
    GridPoint[] outsidePoints = new GridPoint[6];
    int index = 0;
    for (GridPoint p : CubeObject.getOutsidePoints()) {
        outsidePoints[index] = new GridPoint(p.getx(), p.gety());
        index++;
    }
    GridPoint innerPt = new GridPoint(CubeObject.getInnerPoint().getx(), CubeObject.getInnerPoint().gety());
    GridPoint side1Pt = new GridPoint(CubeObject.getSide1Pt().getx(), CubeObject.getSide1Pt().gety());
    GridPoint side2Pt = new GridPoint(CubeObject.getSide2Pt().getx(), CubeObject.getSide2Pt().gety());
    GridPoint cornerPt = new GridPoint(CubeObject.getCornerPt().getx(), CubeObject.getCornerPt().gety());
    if (object.isFlippedVertically()) {
        for (GridPoint p : outsidePoints) {
            flipPointVertically(p);
        }
        flipPointVertically(innerPt);
        flipPointVertically(cornerPt);
        flipPointVertically(side1Pt);
        flipPointVertically(side2Pt);
    }
    if (object.isFlippedHorizontally()) {
        for (GridPoint p : outsidePoints) {
            flipPointHorizontally(p);
        }
        flipPointHorizontally(innerPt);
        flipPointHorizontally(cornerPt);
        flipPointHorizontally(side1Pt);
        flipPointHorizontally(side2Pt);
    }
    if (fillColor != null) {
        fillShape = new Polygon();
        fillShape.addPoint((int) (outsidePoints[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[0].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[1].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[1].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[2].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[2].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
        g2d.setColor(fillColor);
        g2d.fillPolygon(fillShape);
        fillShape = new Polygon();
        fillShape.addPoint((int) (outsidePoints[2].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[2].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[3].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[3].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[4].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[4].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
        g2d.setColor(MathObjectGUI.brightenColor(fillColor));
        g2d.fillPolygon(fillShape);
        fillShape = new Polygon();
        fillShape.addPoint((int) (outsidePoints[4].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[4].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[5].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[5].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (outsidePoints[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[0].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
        g2d.setColor(MathObjectGUI.brightenColor(fillColor));
        g2d.fillPolygon(fillShape);
    }
    g2d.setStroke(new BasicStroke(sap.getLineThickness(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    g2d.setColor(Color.BLACK);
    Polygon p = new Polygon();
    for (int i = 0; i < outsidePoints.length; i++) {
        p.addPoint((int) (outsidePoints[i].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (outsidePoints[i].gety() * sap.getHeight()) + sap.getyOrigin());
    }
    g.drawLine((int) (cornerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (cornerPt.gety() * sap.getHeight()) + sap.getyOrigin(), (int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
    g.drawLine((int) (side1Pt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (side1Pt.gety() * sap.getHeight()) + sap.getyOrigin(), (int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
    g.drawLine((int) (side2Pt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (side2Pt.gety() * sap.getHeight()) + sap.getyOrigin(), (int) (innerPt.getx() * sap.getWidth()) + sap.getxOrigin(), (int) (innerPt.gety() * sap.getHeight()) + sap.getyOrigin());
    g2d.drawPolygon(p);
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) GridPoint(doc.GridPoint) Color(java.awt.Color) Polygon(java.awt.Polygon) GridPoint(doc.GridPoint) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 34 with Color

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

the class CylinderObjectGUI method drawMathObject.

public void drawMathObject(CylinderObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    g.setColor(Color.BLACK);
    ScaledSizeAndPosition sap = getSizeAndPositionWithLineThickness(object, pageOrigin, zoomLevel, object.getThickness());
    Graphics2D g2d = (Graphics2D) g;
    Color fillColor = object.getColor();
    Polygon fillShape;
    if (fillColor != null) {
        fillShape = new Polygon();
        fillShape.addPoint((int) (object.getSide1pts()[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide1pts()[0].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (object.getSide1pts()[1].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide1pts()[1].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (object.getSide2pts()[1].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide2pts()[1].gety() * sap.getHeight()) + sap.getyOrigin());
        fillShape.addPoint((int) (object.getSide2pts()[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide2pts()[0].gety() * sap.getHeight()) + sap.getyOrigin());
        g2d.setColor(fillColor);
        g2d.fillPolygon(fillShape);
        if (object.isFlippedVertically()) {
            g2d.fillOval(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), (int) (object.getInsideEdgeOfDisk().gety() * sap.getHeight()));
            g2d.setColor(MathObjectGUI.brightenColor(fillColor));
            g2d.fillOval(sap.getxOrigin(), (int) (object.getPointBehindCylinder().gety() * sap.getHeight()) + sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()));
        } else {
            g2d.fillOval(sap.getxOrigin(), (int) (object.getPointBehindCylinder().gety() * sap.getHeight()) + sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()));
            g2d.setColor(MathObjectGUI.brightenColor(fillColor));
            g2d.fillOval(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), (int) (object.getInsideEdgeOfDisk().gety() * sap.getHeight()));
        }
    }
    g2d.setStroke(new BasicStroke(sap.getLineThickness(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    g2d.setColor(Color.BLACK);
    g.drawLine((int) (object.getSide1pts()[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide1pts()[0].gety() * sap.getHeight()) + sap.getyOrigin(), (int) (object.getSide1pts()[1].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide1pts()[1].gety() * sap.getHeight()) + sap.getyOrigin());
    g.drawLine((int) (object.getSide2pts()[0].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide2pts()[0].gety() * sap.getHeight()) + sap.getyOrigin(), (int) (object.getSide2pts()[1].getx() * sap.getWidth()) + sap.getxOrigin(), (int) (object.getSide2pts()[1].gety() * sap.getHeight()) + sap.getyOrigin());
    if (object.isFlippedVertically()) {
        g.drawArc(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()), 0, 180);
        g.drawOval(sap.getxOrigin(), (int) (object.getPointBehindCylinder().gety() * sap.getHeight()) + sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()));
    } else {
        g.drawOval(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()));
        g.drawArc(sap.getxOrigin(), (int) (object.getPointBehindCylinder().gety() * sap.getHeight()) + sap.getyOrigin(), sap.getWidth(), (int) (object.getHalfDiskHeight() * sap.getHeight()), 180, 180);
    }
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

Example 35 with Color

use of java.awt.Color 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)

Aggregations

Color (java.awt.Color)895 Graphics2D (java.awt.Graphics2D)117 BufferedImage (java.awt.image.BufferedImage)75 Font (java.awt.Font)63 Dimension (java.awt.Dimension)51 Point (java.awt.Point)47 ArrayList (java.util.ArrayList)46 JLabel (javax.swing.JLabel)41 BasicStroke (java.awt.BasicStroke)38 JPanel (javax.swing.JPanel)38 JScrollPane (javax.swing.JScrollPane)33 GradientPaint (java.awt.GradientPaint)31 Rectangle (java.awt.Rectangle)31 JButton (javax.swing.JButton)31 ActionEvent (java.awt.event.ActionEvent)30 Paint (java.awt.Paint)29 Insets (java.awt.Insets)26 ActionListener (java.awt.event.ActionListener)26 Component (java.awt.Component)24 GridBagConstraints (java.awt.GridBagConstraints)23