Search in sources :

Example 71 with Graphics2D

use of java.awt.Graphics2D in project jmonkeyengine by jMonkeyEngine.

the class CombinedTexture method scale.

/**
     * This method scales the given texture to the given size.
     * 
     * @param texture
     *            the texture to be scaled
     * @param width
     *            new width of the texture
     * @param height
     *            new height of the texture
     */
private void scale(Texture2D texture, int width, int height) {
    // first determine if scaling is required
    boolean scaleRequired = texture.getImage().getWidth() != width || texture.getImage().getHeight() != height;
    if (scaleRequired) {
        Image image = texture.getImage();
        BufferedImage sourceImage = ImageToAwt.convert(image, false, true, 0);
        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();
        BufferedImage targetImage = new BufferedImage(width, height, sourceImage.getType());
        Graphics2D g = targetImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(sourceImage, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
        g.dispose();
        Image output = new ImageLoader().load(targetImage, false);
        image.setWidth(width);
        image.setHeight(height);
        image.setData(output.getData(0));
        image.setFormat(output.getFormat());
    }
}
Also used : Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 72 with Graphics2D

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

the class DocPrinter method print.

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    Page p;
    p = doc.getPage(page);
    if (p == null) {
        return NO_SUCH_PAGE;
    }
    PageGUI pageDrawer = new PageGUI();
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    pageDrawer.drawPage(g, p, new Point(0, 0), new Rectangle(p.getWidth(), p.getHeight()), 1);
    return PAGE_EXISTS;
}
Also used : Rectangle(java.awt.Rectangle) Page(doc.Page) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 73 with Graphics2D

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

the class DocViewerPanel method makeDocPanel.

private JPanel makeDocPanel() {
    return new JPanel() {

        private static final long serialVersionUID = 1L;

        @Override
        public void paint(Graphics g) {
            // TODO - Stopwatch and logging
            //				System.out.println("painting doc:" + (new java.util.Date().getTime() - notebook.timeAtStart));
            //set the graphics object to render text and shapes with smooth lines
            Graphics2D g2d = (Graphics2D) g;
            if (!OpenNotebook.isMinimalGraphicsMode()) {
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            }
            //pixels needed to render a page, and the minimum border space around it
            int pageXSize = (int) (getDoc().getWidth() * zoomLevel);
            int pageYSize = (int) (getDoc().getHeight() * zoomLevel);
            Rectangle viewPortRect = new Rectangle((int) docScrollPane.getViewport().getViewPosition().getX(), (int) docScrollPane.getViewport().getViewPosition().getY(), docScrollPane.getViewport().getWidth(), docScrollPane.getViewport().getHeight());
            //fill background with gray
            g.setColor(Color.GRAY.brighter());
            ((Graphics2D) g).fill(viewPortRect);
            Rectangle currPageRect;
            Point pageOrigin = null;
            //used to store origin of the part of the page that can be seen
            //and the sections overall width and height, both are in the user space at 72 dpi
            int xShowing, yShowing, xSizeShowing, ySizeShowing;
            for (int i = 0; i < doc.getNumPages(); i++) {
                //need to modify rectangle to properly calculate the portion of the page currently displayed
                //which will be given in the user space starting with the origin of the printable area at 0,0
                //and at 72 dpi
                pageOrigin = getPageOrigin(i);
                currPageRect = new Rectangle((int) pageOrigin.getX(), (int) pageOrigin.getY(), pageXSize, pageYSize);
                if (viewPortRect.intersects(currPageRect)) {
                    //render page only if it is in the current section of the document showing
                    if (viewPortRect.getX() < currPageRect.getX()) {
                        xShowing = 0;
                    } else {
                        xShowing = (int) ((viewPortRect.getX() - currPageRect.getX()) / zoomLevel);
                    }
                    if (viewPortRect.getY() < currPageRect.getY()) {
                        yShowing = 0;
                    } else {
                        yShowing = (int) ((viewPortRect.getY() - currPageRect.getY()) / zoomLevel);
                    }
                    pageGUI.drawPageWithDecorations(g, doc.getPage(i), new Point((int) pageOrigin.getX(), (int) pageOrigin.getY()), new Rectangle(xShowing, yShowing, 10, 0), zoomLevel);
                }
            }
            if (selectionRect != null) {
                pageGUI.polygonGUI.drawMathObject(selectionRect, g2d, getPageOrigin(selectionRect.getParentPage()), zoomLevel);
            }
            g.dispose();
        }
    };
}
Also used : Graphics(java.awt.Graphics) JPanel(javax.swing.JPanel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 74 with Graphics2D

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

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

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