初次提交
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>BezierExC</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,12 @@
|
||||
#Fri Jun 24 15:22:54 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
package com.bn.bezier;
|
||||
|
||||
//贝塞尔曲线上点的类
|
||||
|
||||
public class BNPosition
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public BNPosition(float x,float y)
|
||||
{
|
||||
this.x=(int) x;
|
||||
this.y=(int) y;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.bn.bezier;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SpinnerNumberModel;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class BezierExMain extends JFrame
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
new BezierExMain();
|
||||
}
|
||||
|
||||
MyPanel mp;
|
||||
JLabel jlfd=new JLabel("分段");
|
||||
JSpinner jspfd;
|
||||
JLabel jlbj=new JLabel("步进");
|
||||
JSpinner jspbj;
|
||||
JButton jbClear=new JButton("清空");
|
||||
JButton jbFlush=new JButton("删除尾");
|
||||
JButton jbLeft=new JButton("<-左移");
|
||||
JButton jbRight=new JButton("右移->");
|
||||
JButton jbUp=new JButton("上移");
|
||||
JButton jbDown=new JButton("下移");
|
||||
JTextArea jta=new JTextArea();
|
||||
JScrollPane jsp=new JScrollPane(jta);
|
||||
JCheckBox jcb =new JCheckBox("显示控制点");
|
||||
public BezierExMain()
|
||||
{
|
||||
this.setTitle("Bezier曲线工具");
|
||||
this.setLayout(null);
|
||||
|
||||
mp=new MyPanel(this);
|
||||
mp.setBounds(10,10,400,Constant.HEIGHT);
|
||||
this.add(mp);
|
||||
|
||||
jcb.setBounds(415,10,90,20);
|
||||
this.add(jcb);
|
||||
jcb.setSelected(true);
|
||||
jcb.addChangeListener
|
||||
(
|
||||
new ChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent arg0)
|
||||
{
|
||||
Constant.XSKZD=jcb.isSelected();
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jlfd.setBounds(420,40,50,20);
|
||||
this.add(jlfd);
|
||||
|
||||
Integer value = new Integer(20);
|
||||
Integer min = new Integer(0);
|
||||
Integer max = new Integer(100);
|
||||
Integer step = new Integer(1);
|
||||
SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, step);
|
||||
jspfd=new JSpinner(model);
|
||||
jspfd.setBounds(450,40,50,20);
|
||||
this.add(jspfd);
|
||||
jspfd.addChangeListener
|
||||
(
|
||||
new ChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent arg0)
|
||||
{
|
||||
Constant.FD=(Integer)jspfd.getValue();
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbClear.setBounds(420,70,80,20);
|
||||
this.add(jbClear);
|
||||
jbClear.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
BezierUtil.al.clear();
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbFlush.setBounds(420,100,80,20);
|
||||
this.add(jbFlush);
|
||||
jbFlush.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
BezierUtil.al.remove(BezierUtil.al.size()-1);
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jlbj.setBounds(420,130,50,20);
|
||||
this.add(jlbj);
|
||||
value = new Integer(1);
|
||||
min = new Integer(1);
|
||||
max = new Integer(20);
|
||||
step = new Integer(1);
|
||||
model = new SpinnerNumberModel(value, min, max, step);
|
||||
jspbj=new JSpinner(model);
|
||||
jspbj.setBounds(450,130,50,20);
|
||||
this.add(jspbj);
|
||||
jspbj.addChangeListener
|
||||
(
|
||||
new ChangeListener()
|
||||
{
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent arg0)
|
||||
{
|
||||
Constant.ZYBJ=(Integer)jspbj.getValue();
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbLeft.setBounds(420,160,80,20);
|
||||
this.add(jbLeft);
|
||||
jbLeft.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
for(BNPosition bnp:BezierUtil.al)
|
||||
{
|
||||
bnp.x-=Constant.ZYBJ;
|
||||
}
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbRight.setBounds(420,190,80,20);
|
||||
this.add(jbRight);
|
||||
jbRight.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
for(BNPosition bnp:BezierUtil.al)
|
||||
{
|
||||
bnp.x+=Constant.ZYBJ;
|
||||
}
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbUp.setBounds(420,220,80,20);
|
||||
this.add(jbUp);
|
||||
jbUp.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
for(BNPosition bnp:BezierUtil.al)
|
||||
{
|
||||
bnp.y+=Constant.ZYBJ;
|
||||
}
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jbDown.setBounds(420,250,80,20);
|
||||
this.add(jbDown);
|
||||
jbDown.addActionListener
|
||||
(
|
||||
new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
for(BNPosition bnp:BezierUtil.al)
|
||||
{
|
||||
bnp.y-=Constant.ZYBJ;
|
||||
}
|
||||
mp.repaint();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
jsp.setBounds(510,10,210,360);
|
||||
this.add(jsp);
|
||||
|
||||
this.setBounds(10,10,730,400);
|
||||
this.setVisible(true);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
mp.requestFocus();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.bn.bezier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BezierUtil
|
||||
{
|
||||
static ArrayList<BNPosition> al=new ArrayList<BNPosition>(); //贝塞尔曲线基于的点的列表
|
||||
|
||||
public static ArrayList<BNPosition> getBezierData(float span)//求贝塞尔曲线上点的类
|
||||
{
|
||||
ArrayList<BNPosition> result=new ArrayList<BNPosition>(); //存放贝塞尔曲线上点的结果列表
|
||||
|
||||
int n=al.size()-1;
|
||||
|
||||
if(n<1) //基于的点的数少于1,无贝塞尔曲线
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
int steps=(int) (1.0f/span); //总得步进数
|
||||
long[] jiechengNA=new long[n+1]; //声明一个长度为n+1的阶乘数组
|
||||
|
||||
for(int i=0;i<=n;i++){ //求0到n的阶乘
|
||||
jiechengNA[i]=jiecheng(i);
|
||||
}
|
||||
|
||||
for(int i=0;i<=steps;i++)
|
||||
{
|
||||
float t=i*span;
|
||||
if(t>1) //t的值在0-1之间
|
||||
{
|
||||
t=1;
|
||||
}
|
||||
float xf=0;
|
||||
float yf=0;
|
||||
|
||||
float[] tka=new float[n+1];
|
||||
float[] otka=new float[n+1];
|
||||
for(int j=0;j<=n;j++)
|
||||
{
|
||||
tka[j]=(float) Math.pow(t, j); //计算t的j次幂
|
||||
otka[j]=(float) Math.pow(1-t, j); //计算1-t的j次幂
|
||||
}
|
||||
|
||||
for(int k=0;k<=n;k++)
|
||||
{
|
||||
float xs=(jiechengNA[n]/(jiechengNA[k]*jiechengNA[n-k]))*tka[k]*otka[n-k];
|
||||
xf=xf+al.get(k).x*xs;
|
||||
yf=yf+al.get(k).y*xs;
|
||||
}
|
||||
result.add(new BNPosition(xf,yf));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//求阶乘的方法
|
||||
public static long jiecheng(int n){
|
||||
long result=1; //声明一个long型的变量
|
||||
if(n==0) //0的阶乘为1
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
for(int i=2;i<=n;i++){ //求大于等于2的数的阶乘
|
||||
result=result*i;
|
||||
}
|
||||
|
||||
return result; //返回结果
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.bn.bezier;
|
||||
|
||||
public class Constant
|
||||
{
|
||||
public static int HEIGHT=350;
|
||||
public static int FD=20;//分段
|
||||
public static int ZYBJ=1;//左移步进
|
||||
public static boolean XSKZD=true;//是否显示控制点
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.bn.bezier;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class MyPanel extends JPanel implements MouseListener,MouseMotionListener
|
||||
{
|
||||
BezierExMain father;
|
||||
|
||||
public MyPanel(BezierExMain father)
|
||||
{
|
||||
this.father=father;
|
||||
this.addMouseListener(this);
|
||||
this.addMouseMotionListener(this);
|
||||
}
|
||||
|
||||
public void paint(Graphics g)
|
||||
{
|
||||
g.setColor(new Color(0,0,0)); //黑色背景
|
||||
|
||||
|
||||
g.fillRect(0, 0, 1000, 1000);
|
||||
|
||||
father.jta.setText("以下为控制点列表:\n");
|
||||
father.jta.append(" x\t y\n");
|
||||
for(int i=0;i<BezierUtil.al.size()-1;i++)
|
||||
{
|
||||
BNPosition start=BezierUtil.al.get(i);
|
||||
BNPosition end=BezierUtil.al.get(i+1);
|
||||
if(Constant.XSKZD)
|
||||
{
|
||||
g.setColor(new Color(255,0,255));
|
||||
|
||||
|
||||
g.drawLine(start.x, Constant.HEIGHT-start.y, end.x, Constant.HEIGHT-end.y);
|
||||
|
||||
g.setColor(new Color(255,255,255));
|
||||
|
||||
|
||||
g.drawRect(start.x-3, Constant.HEIGHT-start.y-3, 7, 7);
|
||||
}
|
||||
father.jta.append(start.x+"\t"+start.y+"\n");
|
||||
|
||||
}
|
||||
if(BezierUtil.al.size()>0)
|
||||
{
|
||||
BNPosition yilou=BezierUtil.al.get(BezierUtil.al.size()-1);
|
||||
father.jta.append(yilou.x+"\t"+yilou.y+"\n");
|
||||
if(Constant.XSKZD)
|
||||
{
|
||||
g.setColor(new Color(255,255,255));
|
||||
|
||||
g.drawRect(yilou.x-3, Constant.HEIGHT-yilou.y-3, 7, 7);
|
||||
}
|
||||
}
|
||||
father.jta.append("=========================\n");
|
||||
father.jta.append("以下为Bezier曲线中点列表(分段"+Constant.FD+"):\n");
|
||||
father.jta.append(" x\t y\n");
|
||||
g.setColor(new Color(0,255,0));
|
||||
|
||||
|
||||
ArrayList<BNPosition> list=BezierUtil.getBezierData(1.0f/Constant.FD);
|
||||
for(int i=0;i<list.size()-1;i++)
|
||||
{
|
||||
BNPosition start=list.get(i);
|
||||
BNPosition end=list.get(i+1);
|
||||
g.drawLine(start.x, Constant.HEIGHT-start.y, end.x, Constant.HEIGHT-end.y);
|
||||
father.jta.append(start.x+"\t"+start.y+"\n");
|
||||
}
|
||||
if(list.size()>0)
|
||||
{
|
||||
BNPosition yilou=list.get(list.size()-1);
|
||||
father.jta.append(yilou.x+"\t"+yilou.y+"\n");
|
||||
}
|
||||
father.jta.append("=========================\n");
|
||||
father.jta.append("以下为自动生成的代码:\n");
|
||||
father.jta.append("//加入数据点\n");
|
||||
for(BNPosition pos:BezierUtil.al)
|
||||
{
|
||||
father.jta.append("BezierUtil.al.add(new BNPosition("+pos.x+", "+pos.y+"));\n");
|
||||
}
|
||||
father.jta.setCaretPosition(0);
|
||||
}
|
||||
|
||||
int state=0;//0-初始态 1-抓住
|
||||
int currIndex=-1;
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0){}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0)
|
||||
{
|
||||
int x=arg0.getX();
|
||||
int y=arg0.getY();
|
||||
//判断有没有按下控制点
|
||||
for(int i=0;i<BezierUtil.al.size();i++)
|
||||
{
|
||||
BNPosition bnp=BezierUtil.al.get(i);
|
||||
if(Math.abs(x-bnp.x)<=3&&Math.abs(y-(Constant.HEIGHT-bnp.y))<=3)
|
||||
{
|
||||
currIndex=i;
|
||||
state=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0)
|
||||
{
|
||||
if(state==1)
|
||||
{
|
||||
state=0;
|
||||
currIndex=-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
BezierUtil.al.add(new BNPosition(arg0.getX(),Constant.HEIGHT-arg0.getY()));
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent arg0)
|
||||
{
|
||||
if(state==1)
|
||||
{
|
||||
BezierUtil.al.get(currIndex).x=arg0.getX();
|
||||
BezierUtil.al.get(currIndex).y=Constant.HEIGHT-arg0.getY();
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent arg0)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user