/*
* Created on 2004-11-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author aimin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
class myQuadCurvePanel extends Panel{
BorderLayout borderLayout = new BorderLayout();
Panel pdown = new Panel();
Checkbox check1,check2;
Label t1,t2,t3;
Label L1,L2,L3;
Point[] x = new Point[3];
int i,j=0,k,flag=0;
String ts1,ts2,ts3;
//定义myQuadCurvesPanel构造函数
public myQuadCurvePanel(){
x[0] = new Point(10,150);
x[1] = new Point(100,50);
x[2] = new Point(200,150);
this.setLayout(borderLayout);
t1 =new Label();
t2 =new Label();
t3 =new Label();
L1 =new Label();
L2 =new Label();
L3 =new Label();
L1.setText("Point1:");
L2.setText("Point2:");
L3.setText("Point3:");
ts1 = x[0].x+","+x[0].y+" ";
ts2 = x[1].x+","+x[1].y+" ";
ts3 = x[2].x+","+x[2].y+" ";
t1.setText(ts1);
t2.setText(ts2);
t3.setText(ts3);
pdown.setBackground(Color.PINK);
this.add(pdown,BorderLayout.SOUTH);
pdown.add(L1);
pdown.add(t1);
pdown.add(L2);
pdown.add(t2);
pdown.add(L3);
pdown.add(t3);
//监听鼠标下压事件,当鼠标按下时,调用方法panel_mousepressed(),该方法将
//判断鼠标按下的位置是否为控制点的位置
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
panel_mousePressed(e);
}
});
//监听鼠标拖动事件,当鼠标拖动时,调用方法panel_mouseDragged,该方法将修改控制点的位置
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
panel_mouseDragged(e);
}
});
}
//画出三个控制点的连线
public void paint(Graphics g){
for(j=0;j<3;j++){
g.fillOval(x[j].x-3,x[j].y-3,6,6);
if(j>=1){
g.drawLine(x[j-1].x,x[j-1].y,x[j].x,x[j].y);
}
}
draw_curve2D(g,x);
}
//定义方法panel_mousePressed,判断鼠标按下的位置是否在以控制点为中心的20个像素的长和宽
//的正方形区域内,如果是,设置flag = 1;
public void panel_mousePressed(MouseEvent e){
Graphics g = getGraphics();
int[] ax = new int[4];
int[] ay = new int[4];
int x0 = e.getX();
int y0 = e.getY();
Polygon a;
for(i = 0;i < 3;i++){
ax[0] = x[i].x-15;ax[1] = x[i].x+15;ax[2]= x[i].x+15;ax[3] = x[i].x-15;
ay[0] = x[i].y-15;ay[1] = x[i].y+15;ay[2]= x[i].y+15;ax[3] = x[i].y-15;
a= new Polygon(ax,ay,4);
if(a.contains(x0,y0)){
k= i;
flag = 1;
}
}
}
//定义panel_mouseDragged方法,当flag = 1时,修改控制点的坐标为鼠标移动的位置坐标,并且调用repaint()方法,重新绘制曲线
public void panel_mouseDragged(MouseEvent e){
Graphics g = getGraphics();
if(flag == 1){
int x1 = e.getX();
int y1 = e.getY();
x[k].x = x1;
x[k].y = y1;
repaint();
}
}
//定义Draw_Curve2D()方法,定义图形的上下文,用3个控制点定义二次曲线curve,然后再调用QuadCurve2D.Float的draw方法绘制曲线,
//最后用QuadCurve2D.Float的getX1(),getY1(),getX2(),getY2(),getCtrlX(),getCtrlY()方法返回控制点的坐标,然后将值赋给并显示在屏幕的下方
void draw_curve2D(Graphics g,Point[] x){
Graphics2D g2D = (Graphics2D)g;
QuadCurve2D.Float curve = new QuadCurve2D.Float(x[0].x,x[0].y,x[1].x,x[1].y,x[2].x,x[2].y);
//Graphics2D g2D = (Graphics2D)g;
g2D.draw(curve);
ts1 = curve.getX1()+","+curve.getY1()+" ";
ts2 = curve.getCtrlX()+","+curve.getCtrlY()+" ";
ts3 = curve.getX2()+","+curve.getY2()+" ";
t1.setText(ts1);
t2.setText(ts2);
t3.setText(ts3);
}
}
public class myQuadCurve extends Frame{
public myQuadCurve(){
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){
System.exit(0);}});
myQuadCurvePanel p = new myQuadCurvePanel();
this.add(p);
}
public static void main(String[] args){
myQuadCurve f = new myQuadCurve();
f.setSize(400,300);
f.show();
}
}