贝塞尔曲线java实现

import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

class BezierPanel extends JComponent
{
    private static int SIZE = 8;
    private int current;
    private Point2D[] points;
    

    public BezierPanel()
    {
        Point2D p1= new Point2D.Double(64,190);
        Point2D p2= new Point2D.Double(64,150);
        Point2D p3= new Point2D.Double(64,104);
        Point2D p4= new Point2D.Double(64, 64);
        points = new Point2D[]{p1,p2,p3,p4};
        
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent event)
            {
                Point2D p =event.getPoint();
                for(int i = 0; i < points.length; i++)
                {
                    double x = points[i].getX() - SIZE/2;
                    double y = points[i].getY() - SIZE/2;
                    Rectangle2D r = new Rectangle2D.Double(x, y, SIZE, SIZE);
                    if(r.contains(p))
                    {
                        current = i;
                        break;
                    }
                }
            }
            public void mouseReleased(MouseEvent event)
            {
                current = -1;
            }
        });
        
        addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent event)
            {
                if (current == -1 ) {
                    return;
                }
                if(current != -1)
                    points[current] = event.getPoint();
                
                repaint();
            }
        });
        current = -1;
    }
    public Point2D cubicBezier(double t, Point2D[] p)
    {
        Point2D[] temp = new Point2D[p.length];
        for(int k=0; k < p.length; k++)
            temp[k]=p[k];
        for(int i=0; i< 3; i++)
        {
            for(int j = 0; j < 4-i-1 ; j++)
            {
                double x = (1-t)*temp[j].getX() + t*temp[j+1].getX();
                double y = (1-t)*temp[j].getY()+ t*temp[j+1].getY();
                temp[j] = new Point2D.Double(x,y);
            }
        }
        return temp[0];
    }
    
    public void drawBezier(Graphics g, Point2D[] p)
    {
        for(double t = 0; t < 1; t+=0.002)
        {
            Point2D p1= cubicBezier(t,p);
            Point2D p2 = cubicBezier(t+0.001,p);
            g.drawLine((int)Math.round(p1.getX()),(int)Math.round(p1.getY()),(int)Math.round(p2.getX()),(int)Math.round(p2.getY()));
        }
    }
    public void paintComponent(Graphics g)
    {
        if(points == null) return;
        Graphics2D g2 = (Graphics2D) g;
        for(int i = 0; i < points.length; i++)
        {
            double x = points[i].getX() - SIZE/2;
            double y = points[i].getY() - SIZE/2;
            g2.drawString(String.valueOf(i+1), (float)(x+SIZE), (float)(y+SIZE));
            g2.fill(new Rectangle2D.Double(x, y, SIZE, SIZE));
        }
        drawBezier(g,points);
    }
}

public class Test04 extends JFrame
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setTitle("BezierTest");
                frame.setSize(264,360);
                BezierPanel bezier = new BezierPanel();
                bezier.setBorder(new LineBorder(Color.black));
                bezier.setPreferredSize(new Dimension(254, 254));
                frame.add(bezier);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
    
}

输入九个点坐标

import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Test04 extends JFrame{
	static int SIZE = 8;
    static Point2D[] points = new Point2D[9];
	static class BezierPanel extends JComponent{
	    public BezierPanel(){
	    	Scanner in = new Scanner(System.in);
	    	System.out.println("依次输入9个点横纵坐标:");
	    	for(int i=0;i<9;i++) {
	    		points[i] = new Point2D.Double(in.nextDouble(),in.nextDouble());
	    	}
	        in.close(); 
	    }
	    public Point2D cubicBezier(double t, Point2D[] p){
	    	//通过循环 返回当t为某个值时,八次Bezier曲线上这个点
	        Point2D[] temp = new Point2D[p.length];
	        for(int k=0; k < p.length; k++)
	            temp[k]=p[k];
	        for(int i=0; i< 8; i++){
	            for(int j = 0; j < 9-i-1 ; j++){
	                double x = (1-t)*temp[j].getX() + t*temp[j+1].getX();
	                double y = (1-t)*temp[j].getY()+ t*temp[j+1].getY();
	                temp[j] = new Point2D.Double(x,y);
	            }
	        }
	        return temp[0];
	    }
	    
	    public void drawBezier(Graphics g, Point2D[] p){
	    	//通过画点作出八次Bezier曲线
	        for(double t = 0; t < 1; t+=0.0001){//设置t即步长为0.0001区间[0,1]
	            Point2D p1= cubicBezier(t,p);//找点
	            g.drawLine((int)Math.round(p1.getX()),(int)Math.round(p1.getY()),
	            		(int)Math.round(p1.getX()),(int)Math.round(p1.getY()));//画点
	        }
	    }
	    public void paintComponent(Graphics g){
	        for(int i=0;i<points.length-1;i++) {
	        	g.drawLine((int)points[i].getX(),(int)points[i].getY(),
	        			(int)points[i+1].getX(),(int)points[i+1].getY());
	        }
	        if(points == null) return;
	        Graphics2D g2 = (Graphics2D) g;
	        for(int i = 0; i < points.length; i++){
	            double x = points[i].getX() - SIZE/2;
	            double y = points[i].getY() - SIZE/2;
	            g2.drawString(String.valueOf(i+1), (float)(x+SIZE), (float)(y+SIZE));
	            g2.fill(new Rectangle2D.Double(x, y, SIZE, SIZE));
	        }
	        drawBezier(g,points);
	    }
	}
	
    public static void main(String[] args){ 
    	JFrame frame = new JFrame();
    	frame.setTitle("Bezier");
    	frame.setSize(600,400);
    	BezierPanel bezier = new BezierPanel();
    	bezier.setPreferredSize(new Dimension(600, 400));
    	frame.add(bezier);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    }   
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值