如何添加额外的控制点到贝塞尔曲线?

在这里是与总共4个控制点创建Bezier曲线的代码:如何添加额外的控制点到贝塞尔曲线

  • 蓝颜色是起始和结束的控制点。
  • 青色是第二和第三个控制点。

我想一个控制点从这些3个控制点的添加到现有的曲线,所以现在我有3个青色的彩色控制点,这样我可以调整曲线(如拖动曲线),而不是2个控制点。这需要做什么?

我附上了下面的代码的输出作为附件。

import java.awt.Color; 

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.geom.GeneralPath;

public class Piyush extends Frame implements MouseListener, MouseMotionListener {

// private int[] xs = { 75, 150, 300, 375 };

//

// private int[] ys = { 250, 100, 350, 250 };

private int[] xs = { 75, 200, 300, 375 };

private int[] ys = { 250, 100, 100, 250 };

private int dragIndex = NOT_DRAGGING;

private final static int NEIGHBORHOOD = 15;

private final static int NOT_DRAGGING = -1;

public static void main(String[] args) {

new Piyush();

}

public Piyush() {

setSize(500, 450);

addMouseListener(this);

addMouseMotionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

}

public void paint(Graphics g) {

for (int i = 0; i < 4; i++) {

if (i == 0 || i == 3){

g.setColor(Color.blue);

g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12);}

else{

g.setColor(Color.cyan);

g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12);

}

}

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.black);

GeneralPath path = new GeneralPath();

path.moveTo(xs[0], ys[0]);

path.curveTo(xs[1], ys[1], xs[2], ys[2], xs[3], ys[3]);

g2d.draw(path);

}

public void mousePressed(MouseEvent e) {

dragIndex = NOT_DRAGGING;

int minDistance = Integer.MAX_VALUE;

int indexOfClosestPoint = -1;

for (int i = 0; i < 4; i++) {

int deltaX = xs[i] - e.getX();

int deltaY = ys[i] - e.getY();

int distance = (int) (Math.sqrt(deltaX * deltaX + deltaY * deltaY));

if (distance < minDistance) {

minDistance = distance;

indexOfClosestPoint = i;

}

}

if (minDistance > NEIGHBORHOOD)

return;

dragIndex = indexOfClosestPoint;

}

public void mouseReleased(MouseEvent e) {

if (dragIndex == NOT_DRAGGING)

return;

xs[dragIndex] = e.getX();

ys[dragIndex] = e.getY();

dragIndex = NOT_DRAGGING;

repaint();

}

public void mouseDragged(MouseEvent e) {

if (dragIndex == NOT_DRAGGING)

return;

xs[dragIndex] = e.getX();

ys[dragIndex] = e.getY();

repaint();

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseMoved(MouseEvent e) {

}

}

回答:

还问这里

http://www.dreamincode.net/forums/topic/408270-need-help-in-adding-a-control-point-for-bezier-curve/page__pid__2347742__st__0&#entry2347742

代码库来自这里

http://www.java2s.com/Tutorials/Java/Graphics/Shape/Drag_the_control_point_for_Bezier_curve_in_Java.htm

以上是 如何添加额外的控制点到贝塞尔曲线? 的全部内容, 来源链接: utcz.com/qa/261384.html

回到顶部