Java Graphics Draw Curve drawCurve(Graphics2D g, Point p1, Point p2)

Here you can find the source of drawCurve(Graphics2D g, Point p1, Point p2)

Description

draws a nice looking curve between the two given points

License

Open Source License

Parameter

Parameter Description
g a parameter
p1 a parameter
p2 a parameter

Declaration

public static void drawCurve(Graphics2D g, Point p1, Point p2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;
import java.awt.Point;

public class Main {
    /**//from   ww  w  .j ava2s. c o  m
     * draws a nice looking curve between the two given points
     *
     * @param g
     * @param p1
     * @param p2
     */
    public static void drawCurve(Graphics2D g, Point p1, Point p2) {

        int count = (int) Math.pow(Math.hypot(p1.x - p2.x, p1.y - p2.y) * 1.3, 0.5);
        if (count < 1) {
            count = 1;
        }

        for (int i = 0; i < count; i++) {
            float factor = i / (float) count;
            float factor2 = (i + 1) / (float) count;
            Point d1 = interpol(p1, p2, factor);
            Point d2 = interpol(p1, p2, factor2);
            g.drawLine(d1.x, d1.y, d2.x, d2.y);
        }

        //g.drawLine(p1.x, p1.y, p2.x, p2.y);
    }

    /**
     * makes a curvic interpolation between points
     *
     * @param p1
     * @param p2
     * @param factor
     * @return
     */
    private static Point interpol(Point p1, Point p2, float factor) {
        Point p = new Point((int) (p1.x * (1 - factor) + p2.x * factor),
                (int) (p1.y * (1 - factor) + p2.y * factor));

        int targety = factor <= 0.5 ? p1.y : p2.y;

        float facy = Math.abs(factor - 0.5f) * 2;

        p.y = (int) (targety * facy + p.y * (1 - facy));

        return p;
    }
}

Related

  1. drawBevel(Graphics2D g, Rectangle r)
  2. drawBevel(Graphics2D g, Rectangle r)
  3. drawBezel(Graphics g, int x, int y, int w, int h, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
  4. drawBezel(Graphics g, int x, int y, int w, int h, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
  5. drawBezel(Graphics g, int x, int y, int width, int height, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
  6. drawQuadCurve(AffineTransform affine, Vector gPathPoints, Graphics g)