Java Code Examples for java.awt.geom.QuadCurve2D#Double

The following examples show how to use java.awt.geom.QuadCurve2D#Double . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TabbedPaneUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates the shape for a top tab.
 *
 * @param x
 * @param y
 * @param w
 * @param h
 * @param rTop
 * @param addBottom
 *            if {@code false}, the bottom line below the tab will not be added to the shape
 * @return
 */
private static Path2D createTopTabShape(int x, int y, int w, int h, double rTop, boolean addBottom) {
	Path2D path = new Path2D.Double();
	path.append(new Line2D.Double(x, y + h - 1, x, y + rTop), true);

	QuadCurve2D curve = new QuadCurve2D.Double(x, y + rTop, x, y, x + rTop, y);
	path.append(curve, true);

	path.append(new Line2D.Double(x + rTop, y, x + w - rTop, y), true);

	curve = new QuadCurve2D.Double(x + w - rTop, y, x + w, y, x + w, y + rTop);
	path.append(curve, true);

	path.append(new Line2D.Double(x + w, y + rTop, x + w, y + h), true);

	if (addBottom) {
		path.append(new Line2D.Double(x + w, y + h - 1, x, y + h - 1), true);
	}
	return path;
}
 
Example 2
Source File: TabbedPaneUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates the shape for a left tab.
 *
 * @param x
 * @param y
 * @param w
 * @param h
 * @param rLeft
 * @param addSide
 *            if {@code false}, the closing side line right of the tab will not be added to the
 *            shape
 * @return
 */
private static Path2D createLeftTabShape(int x, int y, int w, int h, double rLeft, boolean addSide) {
	Path2D path = new Path2D.Double();
	path.append(new Line2D.Double(x + w, y + h, x + rLeft, y + h), true);

	QuadCurve2D curve = new QuadCurve2D.Double(x + rLeft, y + h, x, y + h, x, y + h - rLeft);
	path.append(curve, true);

	path.append(new Line2D.Double(x, y + h - rLeft, x, y + rLeft), true);

	curve = new QuadCurve2D.Double(x, y + rLeft, x, y, x + rLeft, y);
	path.append(curve, true);

	path.append(new Line2D.Double(x + rLeft, y, x + w, y), true);

	if (addSide) {
		path.append(new Line2D.Double(x + w, y, x + w, y + h - 1), true);
	}
	return path;
}
 
Example 3
Source File: TMAbstractEdge.java    From ontopia with Apache License 2.0 6 votes vote down vote up
protected QuadCurve2D getCurvedLine(int index) {
  double x1 = from.drawx;
  double x2 = to.drawx;
  double y1 = from.drawy;
  double y2 = to.drawy;
  double midx = calculateMidPointBetween(x1, x2);
  double midy = calculateMidPointBetween(y1, y2);
  
  int weight = index / 2;
  if (index % 2 == 1) {
    weight++;
    weight = -weight;
  }
  Dimension offset = calculateOffset(x1, x2, y1, y2, LOADING * weight);
  QuadCurve2D curve = new QuadCurve2D.Double(x1, y1,
      midx-offset.width, midy+offset.height,
      x2, y2);
  return curve;
}
 
Example 4
Source File: IntersectionsTest.java    From pumpernickel with MIT License 5 votes vote down vote up
private Shape getShape(int i) {
	random.setSeed(i * 1000);
	String type = (String) shape.getSelectedItem();
	if (type.equals("All")) {
		int j = random.nextInt(3);
		if (j == 0) {
			type = "Lines";
		} else if (j == 1) {
			type = "Quads";
		} else if (j == 2) {
			type = "Cubics";
		}
	}
	if (type.equals("Lines")) {
		return new Line2D.Double(random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100);
	} else if (type.equals("Quads")) {
		return new QuadCurve2D.Double(random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100);
	} else if (type.equals("Cubics")) {
		return new CubicCurve2D.Double(random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100, random.nextDouble() * 100,
				random.nextDouble() * 100);
	}
	throw new RuntimeException("unrecognized type \"" + type + "\"");
}
 
Example 5
Source File: TMAbstractEdge.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private QuadCurve2D getFromCurve(int index) {
  QuadCurve2D curve = getCurvedLine(index);
  QuadCurve2D result = new QuadCurve2D.Double();
  curve.subdivide(result,null);
  
  return result;
}
 
Example 6
Source File: ShapeUtilities.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to replace an arbitrary shape by one of the standard Java2D constructs.
 * For example if the given {@code path} is a {@link Path2D} containing only a single
 * line or a quadratic curve, then this method replaces it by a {@link Line2D} or
 * {@link QuadCurve2D} object respectively.
 *
 * @param  path  the shape to replace by a simpler Java2D construct.
 *         This is generally an instance of {@link Path2D}, but not necessarily.
 * @return a simpler Java construct, or {@code path} if no better construct is proposed.
 */
public static Shape toPrimitive(final Shape path) {
    final PathIterator it = path.getPathIterator(null);
    if (!it.isDone()) {
        final double[] buffer = new double[6];
        if (it.currentSegment(buffer) == PathIterator.SEG_MOVETO) {
            it.next();
            if (!it.isDone()) {
                final double x1 = buffer[0];
                final double y1 = buffer[1];
                final int code = it.currentSegment(buffer);
                it.next();
                if (it.isDone()) {
                    if (isFloat(path)) {
                        switch (code) {
                            case PathIterator.SEG_LINETO:  return new       Line2D.Float((float) x1, (float) y1, (float) buffer[0], (float) buffer[1]);
                            case PathIterator.SEG_QUADTO:  return new  QuadCurve2D.Float((float) x1, (float) y1, (float) buffer[0], (float) buffer[1], (float) buffer[2], (float) buffer[3]);
                            case PathIterator.SEG_CUBICTO: return new CubicCurve2D.Float((float) x1, (float) y1, (float) buffer[0], (float) buffer[1], (float) buffer[2], (float) buffer[3], (float) buffer[4], (float) buffer[5]);
                        }
                    } else {
                        switch (code) {
                            case PathIterator.SEG_LINETO:  return new       Line2D.Double(x1,y1, buffer[0], buffer[1]);
                            case PathIterator.SEG_QUADTO:  return new  QuadCurve2D.Double(x1,y1, buffer[0], buffer[1], buffer[2], buffer[3]);
                            case PathIterator.SEG_CUBICTO: return new CubicCurve2D.Double(x1,y1, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
                        }
                    }
                }
            }
        }
    }
    return path;
}
 
Example 7
Source File: CaptchaUtils.java    From xmanager with Apache License 2.0 4 votes vote down vote up
private static void drawGraphic(BufferedImage image, String code){
	// 获取图形上下文
	Graphics2D g = image.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
	// 图形抗锯齿
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	// 字体抗锯齿
	g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	
	// 设定背景色,淡色
	g.setColor(getRandColor(210, 250));
	g.fillRect(0, 0, WIDTH, HEIGHT);
	
	// 画小字符背景
	Color color = null;
	for(int i = 0; i < 20; i++){
		color = getRandColor(120, 200);
		g.setColor(color);
		String rand = String.valueOf(charArray[RANDOM.nextInt(charArray.length)]);
		g.drawString(rand, RANDOM.nextInt(WIDTH), RANDOM.nextInt(HEIGHT));
		color = null;
	}
	// 取随机产生的认证码(4位数字)
	char[] buffer = code.toCharArray();
	for (int i = 0; i < buffer.length; i++){
		char _code = buffer[i];
		//旋转度数 最好小于45度
		int degree = RANDOM.nextInt(28);
		if (i % 2 == 0) {
			degree = degree * (-1);
		}
		//定义坐标
		int x = 22 * i, y = 21;
		//旋转区域
		g.rotate(Math.toRadians(degree), x, y);
		//设定字体颜色
		color = getRandColor(20, 130);
		g.setColor(color);
		//设定字体,每次随机
		g.setFont(RANDOM_FONT[RANDOM.nextInt(RANDOM_FONT.length)]);
		//将认证码显示到图象中
		g.drawString("" + _code, x + 8, y + 10);
		//旋转之后,必须旋转回来
		g.rotate(-Math.toRadians(degree), x, y);
	}
	//图片中间曲线,使用上面缓存的color
	g.setColor(color);
	//width是线宽,float型
	BasicStroke bs = new BasicStroke(3);
	g.setStroke(bs);
	//画出曲线
	QuadCurve2D.Double curve = new QuadCurve2D.Double(0d, RANDOM.nextInt(HEIGHT - 8) + 4, WIDTH / 2, HEIGHT / 2, WIDTH, RANDOM.nextInt(HEIGHT - 8) + 4);
	g.draw(curve);
	// 销毁图像
	g.dispose();
}
 
Example 8
Source File: baseDrawerItem.java    From brModelo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void DoPaint(Graphics2D g) {
    int l = left;
    int t = top;

    if (!outroPintor) {
        if (getTipo() == tipoDrawer.tpMedida) {
            l = dono.getL() + left;
            t = dono.getT() + top;
        } else {
            l = dono.getL();
            t = dono.getT();
        }
    }
    if (!recivePaint) {
        if (isGradiente()) {
            g.setPaint(PaintGradiente(g, l, t));
        } else {
            g.setColor(getCor());
        }
    }

    Shape dr = null;
    boolean ok = false;
    int[] pts;
    switch (tipo) {
        case tpElipse:
            pts = ArrayDePontos(getElipse());
            if (pts.length == 4) {
                dr = new Ellipse2D.Double(pts[0], pts[1], pts[2], pts[3]);
            }
            break;
        case tpRetangulo:
            pts = ArrayDePontos(getRetangulo());
            if (pts.length == 4) {
                dr = new Rectangle2D.Double(pts[0], pts[1], pts[2], pts[3]);
            }
            if (pts.length == 6) {
                dr = new RoundRectangle2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
            }
            break;
        case tpCurva:
            pts = ArrayDePontos(getCurva());
            if (pts.length == 8) {
                dr = new CubicCurve2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]);
            }
            if (pts.length == 6) {
                dr = new QuadCurve2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
            }
            break;
        case tpArco:
            //        public final static int OPEN = 0;
            //        public final static int CHORD = 1;
            //        public final static int PIE = 2;
            pts = ArrayDePontos(getArco());
            if (pts.length == 7) {
                dr = new Arc2D.Double(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5], pts[6]);
            }
            break;
        case tpImagem:
            DrawImagem(g);
            ok = true;
            break;
        case tpMedida:
            //# Foi retirado do dezenho porque está feio (DrawerEditor).)
            if (vertical == VERTICAL) {
                medidaH(g, l, t);
            } else {
                medidaV(g, l, t);
            }
            ok = true;
            break;
        case tpPath:
            DrawComplexPath(g, l, t);
            ok = true;
            break;
        default:
            g.drawLine(l, t, getWidth(), getHeight());
            ok = true;
            break;
    }
    if (dr == null || ok) {
        if (dr == null && !ok) {
            g.drawString("?", l + 5, t + 5);
        }
        return;
    }
    if (isFill()) {
        g.fill(dr);
    } else {
        g.draw(dr);
    }
}
 
Example 9
Source File: Curve.java    From Logisim with GNU General Public License v3.0 4 votes vote down vote up
private QuadCurve2D getCurve(HandleGesture gesture) {
	Handle[] p = getHandleArray(gesture);
	return new QuadCurve2D.Double(p[0].getX(), p[0].getY(), p[1].getX(), p[1].getY(), p[2].getX(), p[2].getY());
}
 
Example 10
Source File: Curve.java    From Logisim with GNU General Public License v3.0 4 votes vote down vote up
public QuadCurve2D getCurve2D() {
	return new QuadCurve2D.Double(p0.getX(), p0.getY(), p1.getX(), p1.getY(), p2.getX(), p2.getY());
}
 
Example 11
Source File: NaturalSpline.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Create the natural spline that interpolates the provided knots
 *
 * @param xx the abscissae of the provided points
 * @param yy the ordinates of the provided points
 * @return the resulting spline curve
 */
public static NaturalSpline interpolate (double[] xx,
                                         double[] yy)
{
    // Check parameters
    Objects.requireNonNull(xx, "NaturalSpline cannot interpolate null arrays");
    Objects.requireNonNull(yy, "NaturalSpline cannot interpolate null arrays");

    if (xx.length != yy.length) {
        throw new IllegalArgumentException(
                "NaturalSpline interpolation needs consistent coordinates");
    }

    // Number of segments
    final int n = xx.length - 1;

    if (n < 1) {
        throw new IllegalArgumentException(
                "NaturalSpline interpolation needs at least 2 points");
    }

    if (n == 1) {
        // Use a Line
        return new NaturalSpline(new Line2D.Double(xx[0], yy[0], xx[1], yy[1]));
    } else if (n == 2) {
        // Use a Quadratic (TODO: check this formula...)
        //            double t = (xx[1] - xx[0]) / (xx[2] - xx[0]);
        //            double u = 1 - t;
        //            double cpx = (xx[1] - (u * u * xx[0]) - (t * t * xx[2])) / 2 * t * u;
        //            double cpy = (yy[1] - (u * u * yy[0]) - (t * t * yy[2])) / 2 * t * u;
        return new NaturalSpline(
                new QuadCurve2D.Double(
                        xx[0],
                        yy[0],
                        (2 * xx[1]) - ((xx[0] + xx[2]) / 2),
                        (2 * yy[1]) - ((yy[0] + yy[2]) / 2),
                        xx[2],
                        yy[2]));
    } else {
        // Use a sequence of cubics
        double[] dx = getCubicDerivatives(xx);
        double[] dy = getCubicDerivatives(yy);
        Shape[] curves = new Shape[n];

        for (int i = 0; i < n; i++) {
            // Build each segment curve
            curves[i] = new CubicCurve2D.Double(
                    xx[i],
                    yy[i],
                    xx[i] + (dx[i] / 3),
                    yy[i] + (dy[i] / 3),
                    xx[i + 1] - (dx[i + 1] / 3),
                    yy[i + 1] - (dy[i + 1] / 3),
                    xx[i + 1],
                    yy[i + 1]);
        }

        return new NaturalSpline(curves);
    }
}
 
Example 12
Source File: TMAbstractEdge.java    From ontopia with Apache License 2.0 4 votes vote down vote up
private QuadCurve2D getToCurve(int index) {
  QuadCurve2D curve = getCurvedLine(index);
  QuadCurve2D result = new QuadCurve2D.Double();
  curve.subdivide(null,result);
  return result;
}
 
Example 13
Source File: TMAbstractEdge.java    From ontopia with Apache License 2.0 4 votes vote down vote up
private Point getMiddleOf(QuadCurve2D curve) {
  QuadCurve2D result = new QuadCurve2D.Double();
  curve.subdivide(result,null);
  return new Point((int)result.getP2().getX(), (int)result.getP2().getY());
}
 
Example 14
Source File: NaturalSpline.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Create the natural spline that interpolates the provided knots
 *
 * @param xx the abscissae of the provided points
 * @param yy the ordinates of the provided points
 * @return the resulting spline curve
 */
public static NaturalSpline interpolate (double[] xx,
                                         double[] yy)
{
    // Check parameters
    if ((xx == null) || (yy == null)) {
        throw new IllegalArgumentException(
                "NaturalSpline cannot interpolate null arrays");
    }

    if (xx.length != yy.length) {
        throw new IllegalArgumentException(
                "NaturalSpline interpolation needs consistent coordinates");
    }

    // Number of segments
    final int n = xx.length - 1;

    if (n < 1) {
        throw new IllegalArgumentException(
                "NaturalSpline interpolation needs at least 2 points");
    }

    if (n == 1) {
        // Use a Line
        return new NaturalSpline(
                new Line2D.Double(xx[0], yy[0], xx[1], yy[1]));
    } else if (n == 2) {
        // Use a Quadratic (TODO: check this formula...)
        //            double t = (xx[1] - xx[0]) / (xx[2] - xx[0]);
        //            double u = 1 - t;
        //            double cpx = (xx[1] - (u * u * xx[0]) - (t * t * xx[2])) / 2 * t * u;
        //            double cpy = (yy[1] - (u * u * yy[0]) - (t * t * yy[2])) / 2 * t * u;
        return new NaturalSpline(
                new QuadCurve2D.Double(
                xx[0],
                yy[0],
                (2 * xx[1]) - ((xx[0] + xx[2]) / 2),
                (2 * yy[1]) - ((yy[0] + yy[2]) / 2),
                xx[2],
                yy[2]));
    } else {
        // Use a sequence of cubics
        double[] dx = getCubicDerivatives(xx);
        double[] dy = getCubicDerivatives(yy);
        Shape[] curves = new Shape[n];

        for (int i = 0; i < n; i++) {
            // Build each segment curve
            curves[i] = new CubicCurve2D.Double(
                    xx[i],
                    yy[i],
                    xx[i] + (dx[i] / 3),
                    yy[i] + (dy[i] / 3),
                    xx[i + 1] - (dx[i + 1] / 3),
                    yy[i + 1] - (dy[i + 1] / 3),
                    xx[i + 1],
                    yy[i + 1]);
        }

        return new NaturalSpline(curves);
    }
}
 
Example 15
Source File: StrokeSettingsPanel.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
private static JPanel createStrokePreviewPanel(StrokeParam sp) {
        JComponent preview = new JComponent() {
            final Dimension size = new Dimension(120, 120);

            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
                g2.setColor(Color.WHITE);
                int width = getWidth();
                int height = getHeight();
                g2.fillRect(0, 0, width, height);
//                Line2D shape = new Line2D.Double(width * 0.1, height * 0.5, width * 0.9, height * 0.5);
                QuadCurve2D.Double shape = new QuadCurve2D.Double(
                        width * 0.1, height * 0.4,
                        width * 0.5, height * 0.8,
                        width * 0.9, height * 0.4
                );
                g2.setColor(Color.BLACK);
                g2.setStroke(sp.createStroke());
                g2.draw(shape);
            }

            @Override
            public Dimension getMinimumSize() {
                return size;
            }

            @Override
            public Dimension getPreferredSize() {
                return size;
            }
        };
        sp.setPreviewer(preview);

        var p = new JPanel(new BorderLayout());
        p.add(preview, CENTER);
        p.setBorder(createTitledBorder("Stroke Preview"));

        return p;
    }
 
Example 16
Source File: ShapeUtilities.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a quadratic curve passing by the 3 given points. There is an infinity of quadratic curves passing by
 * 3 points. We can express the curve we are looking for as a parabolic equation of the form {@code y=ax²+bx+c}
 * but where the <var>x</var> axis is not necessarily horizontal. The orientation of the <var>x</var> axis in
 * the above equation is determined by the {@code horizontal} parameter:
 *
 * <ul>
 *   <li>A value of {@code true} means that the <var>x</var> axis must be horizontal. The quadratic curve
 *       will then looks like an ordinary parabolic curve as we see in mathematic school book.</li>
 *   <li>A value of {@code false} means that the <var>x</var> axis must be parallel to the
 *       line segment joining the {@code P0} and {@code P2} ending points.</li>
 * </ul>
 *
 * Note that if {@code P0.y == P2.y}, then both {@code horizontal} values produce the same result.
 *
 * @param  x1  <var>x</var> value of the starting point.
 * @param  y1  <var>y</var> value of the starting point.
 * @param  px  <var>x</var> value of a passing point.
 * @param  py  <var>y</var> value of a passing point.
 * @param  x2  <var>x</var> value of the ending point.
 * @param  y2  <var>y</var> value of the ending point.
 * @param  horizontal  if {@code true}, the <var>x</var> axis is considered horizontal while computing the
 *         {@code y=ax²+bx+c} equation terms. If {@code false}, it is considered parallel to the line
 *         joining the {@code P0} and {@code P2} points.
 * @return a quadratic curve passing by the given points. The curve starts at {@code P0} and ends at {@code P2}.
 *         If two points are too close or if the three points are colinear, then this method returns {@code null}.
 *
 * @todo This method is used by Geotk (a sandbox for code that may migrate to SIS), but not yet by SIS.
 *       We temporarily keep this code here, but may delete or move it elsewhere in a future SIS version
 *       depending whether we port to SIS the sandbox code.
 */
public static QuadCurve2D.Double fitParabol(final double x1, final double y1,
                                            final double px, final double py,
                                            final double x2, final double y2,
                                            final boolean horizontal)
{
    final Point2D.Double p = parabolicControlPoint(x1, y1, px, py, x2, y2, horizontal);
    return (p != null) ? new QuadCurve2D.Double(x1, y1, p.x, p.y, x2, y2) : null;
}