智能使用Class CubicCurve2D
应该可以实现你想要的:CubicCurve2D类实现了Shape
接口。此类表示(x, y)
坐标空间中的三次参数曲线段。CubicCurve2D.Float
和CubicCurve2D.Double
子类指定三次曲线float
和double
精度。
此类setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
允许设置两个控制点。如果我们在曲线开始之前(ctrlx1, ctrly1)
和曲线结束之后设置控制点(ctrlx2, ctrly2)
。为了保持曲线的角度为90
度数的倍数,我们可以类似地计算控制点,如下所示(计算为90 deegree
):
ctrlx1 = x1; // curve start x
ctrly1 = y2 - delta; // curve start y
ctrlx2 = x1 + delta; // curve end x
ctrly2 = y2; // curve end y
在下面的例子中,我假设了delta = 10
;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
CubicCurve2D c = new CubicCurve2D.Double();
int x1 = 150, y1 = 150; //p1
int x2 = 350, y2 = 300;//p3
int ctrlx1, ctrly1, ctrlx2, ctrly2;
int delta = 10;
ctrlx1 = x1; // curve start x
ctrly1 = y2 - delta; // curve start y
ctrlx2 = x1 + delta; // curve end x
ctrly2 = y2;
g2d.drawRect(x1-50, y1-100, 100, 100);
c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
g2d.drawRect(x2, y2-50, 100, 100);
g2d.draw(c);
}