Example usage for java.awt.geom GeneralPath lineTo

List of usage examples for java.awt.geom GeneralPath lineTo

Introduction

In this page you can find the example usage for java.awt.geom GeneralPath lineTo.

Prototype

public abstract void lineTo(double x, double y);

Source Link

Document

Adds a point to the path by drawing a straight line from the current coordinates to the new specified coordinates specified in double precision.

Usage

From source file:org.jcurl.math.ShaperUtils.java

/**
 * Compute the control point and add one <a
 * href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Quadratic Bezier
 * Curve</a> to a {@link GeneralPath}. Does <b>no</b> initial
 * {@link GeneralPath#moveTo(float, float)}.
 * /*from  w  w  w  .j  a va  2  s  .  com*/
 * <h3>Approximation algorithm</h3>
 * <p>
 * This ansatz uses no adaptive optimisation but only
 * <ul>
 * <li>the start- and endpoint of each interval als control points k0 and
 * k2</li>
 * <li>the directions (normalised velocities) in the control points k0 and
 * k2. The intersection is used as k1.</li>
 * </ul>
 * <p>
 * TODO maybe re-use endpoint location and velocity. This can cause pain at
 * C1 discontinuous t's (collissions).
 * </p>
 * <h3><a href="http://en.wikipedia.org/wiki/Maxima_(software)">Maxima</a>
 * Solution</h3>
 * 
 * <pre>
 * radsubstflag: true$
 * k0_0 + l * v0_0 = k2_0 + m * v2_0;
 * k0_1 + l * v0_1 = k2_1 + m * v2_1;
 * solve([%th(2),%th(1)],[l,m]);
 * subst(q, v0_1 * v2_0 - v0_0 * v2_1, %);
 * subst(dx_0 + k0_0, k2_0, %);
 * subst(dx_1 + k0_1, k2_1, %);
 * ratsimp(%);
 * </pre>
 */
static final void quadTo(final R1RNFunction f, final double tmin, final double tmax, final GeneralPath gp,
        final float zoom) {
    final double eps = 1e-6;

    // first control point (startpoint). The same as gp.getCurrentPoint()
    final double k0_0 = f.at(tmin, 0, 0);
    final double k0_1 = f.at(tmin, 0, 1);
    // startpoint velocity
    double v0_0 = f.at(tmin, 1, 0);
    double v0_1 = f.at(tmin, 1, 1);
    if (v0_0 * v0_0 + v0_1 * v0_1 < eps) {
        v0_0 = f.at(tmin + eps, 1, 0);
        v0_1 = f.at(tmin + eps, 1, 1);
    }

    // 3rd control point (endpoint).
    final double k2_0 = f.at(tmax, 0, 0);
    final double k2_1 = f.at(tmax, 0, 1);
    // endpoint velocity
    double v2_0 = f.at(tmax, 1, 0);
    double v2_1 = f.at(tmax, 1, 1);
    if (v2_0 * v2_0 + v2_1 * v2_1 < eps) {
        v2_0 = f.at(tmax - eps, 1, 0);
        v2_1 = f.at(tmax - eps, 1, 1);
    }

    // compute the 2nd control point
    final double dx_0 = k2_0 - k0_0;
    final double dx_1 = k2_1 - k0_1;
    final double q = v0_1 * v2_0 - v0_0 * v2_1;
    final double m = -(dx_0 * v0_1 - dx_1 * v0_0) / q;

    // 2nd control point is
    final float k1_0 = (float) (k2_0 + m * v2_0);
    final float k1_1 = (float) (k2_1 + m * v2_1);

    if (true)
        gp.quadTo(zoom * k1_0, zoom * k1_1, zoom * (float) k2_0, zoom * (float) k2_1);
    else {
        gp.lineTo(zoom * k1_0, zoom * k1_1);
        gp.lineTo(zoom * (float) k2_0, zoom * (float) k2_1);
    }
}

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

private void paintThumbnail(Graphics2D g2, PhotoInfo photo, int startx, int starty, boolean isSelected) {
    log.debug("paintThumbnail entry " + photo.getUuid());
    long startTime = System.currentTimeMillis();
    long thumbReadyTime = 0;
    long thumbDrawnTime = 0;
    long endTime = 0;
    // Current position in which attributes can be drawn
    int ypos = starty + rowHeight / 2;
    boolean useOldThumbnail = false;

    Thumbnail thumbnail = null;//from   w  w w  . j  av  a  2s  . com
    log.debug("finding thumb");
    boolean hasThumbnail = photo.hasThumbnail();
    log.debug("asked if has thumb");
    if (hasThumbnail) {
        log.debug("Photo " + photo.getUuid() + " has thumbnail");
        thumbnail = photo.getThumbnail();
        log.debug("got thumbnail");
    } else {
        /*
         Check if the thumbnail has been just invalidated. If so, use the 
         old one until we get the new thumbnail created.
         */
        thumbnail = photo.getOldThumbnail();
        if (thumbnail != null) {
            useOldThumbnail = true;
        } else {
            // No success, use default thumnail.
            thumbnail = Thumbnail.getDefaultThumbnail();
        }

        // Inform background task scheduler that we have some work to do
        ctrl.getBackgroundTaskScheduler().registerTaskProducer(this, TaskPriority.CREATE_VISIBLE_THUMBNAIL);
    }
    thumbReadyTime = System.currentTimeMillis();

    log.debug("starting to draw");
    // Find the position for the thumbnail
    BufferedImage img = thumbnail.getImage();
    if (img == null) {
        thumbnail = Thumbnail.getDefaultThumbnail();
        img = thumbnail.getImage();
    }

    float scaleX = ((float) thumbWidth) / ((float) img.getWidth());
    float scaleY = ((float) thumbHeight) / ((float) img.getHeight());
    float scale = Math.min(scaleX, scaleY);
    int w = (int) (img.getWidth() * scale);
    int h = (int) (img.getHeight() * scale);

    int x = startx + (columnWidth - w) / (int) 2;
    int y = starty + (rowHeight - h) / (int) 2;

    log.debug("drawing thumbnail");

    // Draw shadow
    int offset = isSelected ? 2 : 0;
    int shadowX[] = { x + 3 - offset, x + w + 1 + offset, x + w + 1 + offset };
    int shadowY[] = { y + h + 1 + offset, y + h + 1 + offset, y + 3 - offset };
    GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, shadowX.length);
    polyline.moveTo(shadowX[0], shadowY[0]);
    for (int index = 1; index < shadowX.length; index++) {
        polyline.lineTo(shadowX[index], shadowY[index]);
    }
    ;
    BasicStroke shadowStroke = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
    Stroke oldStroke = g2.getStroke();
    g2.setStroke(shadowStroke);
    g2.setColor(Color.DARK_GRAY);
    g2.draw(polyline);
    g2.setStroke(oldStroke);

    // Paint thumbnail
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(img, new AffineTransform(scale, 0f, 0f, scale, x, y), null);
    if (useOldThumbnail) {
        creatingThumbIcon.paintIcon(this, g2,
                startx + (columnWidth - creatingThumbIcon.getIconWidth()) / (int) 2,
                starty + (rowHeight - creatingThumbIcon.getIconHeight()) / (int) 2);
    }
    log.debug("Drawn, drawing decorations");
    if (isSelected) {
        Stroke prevStroke = g2.getStroke();
        Color prevColor = g2.getColor();
        g2.setStroke(new BasicStroke(3.0f));
        g2.setColor(Color.BLUE);
        g2.drawRect(x, y, w, h);
        g2.setColor(prevColor);
        g2.setStroke(prevStroke);
    }

    thumbDrawnTime = System.currentTimeMillis();

    boolean drawAttrs = (thumbWidth >= 100);
    if (drawAttrs) {
        // Increase ypos so that attributes are drawn under the image
        ypos += ((int) h) / 2 + 3;

        // Draw the attributes

        // Draw the qualoity icon to the upper left corner of the thumbnail
        int quality = photo.getQuality();
        if (showQuality && quality != 0) {
            int qx = startx + (columnWidth - quality * starIcon.getIconWidth()) / (int) 2;
            for (int n = 0; n < quality; n++) {
                starIcon.paintIcon(this, g2, qx, ypos);
                qx += starIcon.getIconWidth();
            }
            ypos += starIcon.getIconHeight();
        }
        ypos += 6;

        if (photo.getRawSettings() != null) {
            // Draw the "RAW" icon
            int rx = startx + (columnWidth + w - rawIcon.getIconWidth()) / (int) 2 - 5;
            int ry = starty + (columnWidth - h - rawIcon.getIconHeight()) / (int) 2 + 5;
            rawIcon.paintIcon(this, g2, rx, ry);
        }
        if (photo.getHistory().getHeads().size() > 1) {
            // Draw the "unresolved conflicts" icon
            int rx = startx + (columnWidth + w - 10) / (int) 2 - 20;
            int ry = starty + (columnWidth - h - 10) / (int) 2;
            g2.setColor(Color.RED);
            g2.fillRect(rx, ry, 10, 10);
        }

        Color prevBkg = g2.getBackground();
        if (isSelected) {
            g2.setBackground(Color.BLUE);
        } else {
            g2.setBackground(this.getBackground());
        }
        Font attrFont = new Font("Arial", Font.PLAIN, 10);
        FontRenderContext frc = g2.getFontRenderContext();
        if (showDate && photo.getShootTime() != null) {
            FuzzyDate fd = new FuzzyDate(photo.getShootTime(), photo.getTimeAccuracy());

            String dateStr = fd.format();
            TextLayout txt = new TextLayout(dateStr, attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();
            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        String shootPlace = photo.getShootingPlace();
        if (showPlace && shootPlace != null && shootPlace.length() > 0) {
            TextLayout txt = new TextLayout(photo.getShootingPlace(), attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();

            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        g2.setBackground(prevBkg);
    }
    endTime = System.currentTimeMillis();
    log.debug("paintThumbnail: exit " + photo.getUuid());
    log.debug("Thumb fetch " + (thumbReadyTime - startTime) + " ms");
    log.debug("Thumb draw " + (thumbDrawnTime - thumbReadyTime) + " ms");
    log.debug("Deacoration draw " + (endTime - thumbDrawnTime) + " ms");
    log.debug("Total " + (endTime - startTime) + " ms");
}

From source file:savant.view.tracks.ContinuousTrackRenderer.java

@Override
public void render(Graphics2D g2, GraphPaneAdapter gp) throws RenderingException {

    renderPreCheck();/* ww w .  j  ava 2  s .co  m*/

    AxisRange axisRange = (AxisRange) instructions.get(DrawingInstruction.AXIS_RANGE);
    gp.setXRange(axisRange.getXRange());
    gp.setYRange(axisRange.getYRange());

    if (gp.needsToResize())
        return;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);
    Color fillcolor = cs.getColor(ColourKey.CONTINUOUS_FILL);
    Color linecolor = cs.getColor(ColourKey.CONTINUOUS_LINE);

    GeneralPath path = new GeneralPath();
    double xFormXPos = Double.NaN, xFormYPos = Double.NaN;

    double yPixel0 = gp.transformYPos(0.0);
    LOG.debug("h=" + gp.getHeight() + ", yMin=" + gp.getYRange().getFrom() + ", unitHeight="
            + gp.getUnitHeight() + " \u27A4 yPixel0=" + yPixel0);

    double maxData = 0;
    boolean haveOpenPath = false;
    boolean haveData = false;
    if (data != null) {
        for (int i = 0; i < data.size(); i++) {
            ContinuousRecord continuousRecord = (ContinuousRecord) data.get(i);
            int xPos = continuousRecord.getPosition();
            float yPos = continuousRecord.getValue();
            if (Float.isNaN(yPos)) {
                // Hit a position with no data.  May need to close off the current path.
                if (haveOpenPath) {
                    path.lineTo(xFormXPos, yPixel0);
                    path.closePath();
                    haveOpenPath = false;
                }
            } else {
                haveData = true;
                xFormXPos = gp.transformXPos(xPos);//+gp.getUnitWidth()/2;
                xFormYPos = gp.transformYPos(yPos);
                if (!haveOpenPath) {
                    // Start our path off with a vertical line.
                    path.moveTo(xFormXPos, yPixel0);
                    haveOpenPath = true;
                }
                path.lineTo(xFormXPos, xFormYPos);
                Rectangle2D rec = new Rectangle2D.Double(
                        xFormXPos - ((xFormXPos - path.getCurrentPoint().getX()) / 2), 0,
                        Math.max(xFormXPos - path.getCurrentPoint().getX(), 1), gp.getHeight());
                recordToShapeMap.put(continuousRecord, rec);
                xFormXPos = gp.transformXPos(xPos + 1);
                path.lineTo(xFormXPos, xFormYPos);
            }
            if (yPos > maxData) {
                maxData = yPos;
            }
        }
    }
    if (!haveData) {
        throw new RenderingException("No data in range", RenderingException.INFO_PRIORITY);
    }
    if (haveOpenPath) {
        // Path needs to be closed.
        path.lineTo(xFormXPos, yPixel0);
        path.closePath();
    }

    g2.setColor(fillcolor);
    g2.fill(path);
    g2.setColor(linecolor);
    g2.draw(path);

    if (axisRange.getYRange().getFrom() < 0) {
        g2.setColor(Color.darkGray);
        g2.draw(new Line2D.Double(0.0, yPixel0, gp.getWidth(), yPixel0));
    }
}