package de_Casteljau;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class BezierFrame extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BezierFrame bf=new BezierFrame();
bf.setSize(500, 500);
bf.setResizable(false);
bf.setVisible(true);
}
BezierPanel bp=null;
BtnPanel btnp=null;
public BezierFrame()
{
this.setLayout(new BorderLayout());
bp=new BezierPanel();
add(bp,BorderLayout.CENTER);
btnp=new BtnPanel();
add(btnp,BorderLayout.SOUTH);
}
}
class BtnPanel extends JPanel {
public JButton btn_bezier=null;
public JButton btn_bspline=null;
//public JButton btn_erase=null;
public JLabel status=null;
//public BezierPanel bp=null;
JButton btn_hermite=new JButton("Hermite");
public BtnPanel()
{
btn_bezier=new JButton("Bezier");
btn_bspline=new JButton("B-Spline");
status=new JLabel("Bezier");
//btn_erase=new JButton("Erase");
//bp=new BezierPanel();
this.setLayout(new FlowLayout());
add(btn_bezier);
add(btn_bspline);
add(btn_hermite);
add(status);
/*btn_erase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//bp.eraseCurve();
}
});*/
btn_bezier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//System.out.println("1");
BezierPanel.select=1;
status.setText("Bezier");
}
});
btn_bspline.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//System.out.println("2");
BezierPanel.select=2;
status.setText("Bspline");
}
});
btn_hermite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
BezierPanel.select=3;
status.setText("Hermite");
}
});
}
}
class BezierPanel extends JPanel {
int[] x;
int[] y;
int i,j;
int flag=0;
int k;
boolean erase=false;
public static int select=1;
int flagb=0;
int ib,jb;
int translate=0;
int pressPoint=-1;
Vector vx=new Vector();
Vector vy=new Vector();
int n,size,h,z;
int kb=3;
int[] t;
int[] xh;
int[] yh;
int jh=0;
int kh=0;
public BezierPanel()
{
x=new int[4];
y=new int[4];
xh=new int[4];
yh=new int[4];
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
Graphics g=getGraphics();
if((e.getModifiersEx()&InputEvent.BUTTON3_DOWN_MASK)!=0)
{
System.out.println("r");
//g.clearRect(0, 0, 500, 500);
//j=0;
//for(i=0;i<4;i++)
//{
//x[i]=0;
//y[i]=0;
//}
}
else
{
if(BezierPanel.select==1)
{
if(j<4&(erase==false))
{
x[j]=e.getX();
y[j]=e.getY();
g.drawString(x[j]+","+y[j], x[j], y[j]);
if(j>=1)
{
g.drawLine(x[j-1], y[j-1], x[j], y[j]);
}
j++;
}
if(j==4&(erase==false))
{
drawBezier(g,x,y);
/*j=0;
for(i=0;i<4;i++)
{
x[i]=0;
y[i]=0;
}*/
///////
}
}
else
{
if(BezierPanel.select==3)
{
if(jh<4&(erase==false))
{
xh[jh]=e.getX();
yh[jh]=e.getY();
if (jh%2==0)
{
g.drawString(xh[jh]+","+yh[jh], xh[jh], yh[jh]);
}
else if (jh%2==1)
{
g.drawLine(xh[jh-1], yh[jh-1], xh[jh], yh[jh]);
}
}
jh++;
if(jh==4&(erase==false))
{
DrawHermite(xh[0],yh[0],xh[2],yh[2],(xh[1]-xh[0]),(yh[1]-yh[0]),(xh[3]-xh[2]),(yh[3]-yh[2]),g);
jh=0;
}
}
}
}
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
if((e.getModifiersEx()&InputEvent.BUTTON3_DOWN_MASK)!=0)
{
//System.out.println("r");
erase=true;
Graphics g=getGraphics();
g.clearRect(0, 0, 500, 500);
j=0;
k=0;
flag=0;
for(i=0;i<4;i++)
{
x[i]=0;
y[i]=0;
}
vx.removeAllElements();
vy.removeAllElements();
flagb=0;
pressPoint=-1;
}
else
{
erase=false;
if(BezierPanel.select==1)
{
panel_mousePressed(e);
}
if(BezierPanel.select==2)
{
panel_mousePressedB(e);
}
if(BezierPanel.select==3)
{
panel_mousePressedH(e);
}
}
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e)
{
if((e.getModifiersEx()&InputEvent.BUTTON3_DOWN_MASK)!=0)
{
}
else
{
if(BezierPanel.select==1)
{
panel_mouseDragged(e);
}
if(BezierPanel.select==2)
{
panel_mouseDraggedB(e);
}
if(BezierPanel.select==3)
{
panel_mouseDraggedH(e);
}
}
}
});
}
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<4;i++)
{
ax[0]=x[i]-10;
ax[1]=x[i]+10;
ax[2]=x[i]+10;
ax[3]=x[i]-10;
ay[0]=y[i]-10;
ay[1]=y[i]-10;
ay[2]=y[i]+10;
ay[3]=y[i]+10;
a=new Polygon(ax,ay,4);
if(a.contains(x0, y0))
{
k=i;
flag=1;
}
}
}
public void panel_mousePressedB(MouseEvent e)
{
Graphics g=getGraphics();
flagb=0;
int[] ax=new int[4];
int[] ay=new int[4];
int x0=e.getX();
int y0=e.getY();
Polygon a;
for(ib=0;ib<vx.size();ib++)
{
ax[0]=((Integer)vx.get(ib)).intValue()-10;
ax[1]=((Integer)vx.get(ib)).intValue()+10;
ax[2]=((Integer)vx.get(ib)).intValue()+10;
ax[3]=((Integer)vx.get(ib)).intValue()-10;
ay[0]=((Integer)vy.get(ib)).intValue()-10;
ay[1]=((Integer)vy.get(ib)).intValue()-10;
ay[2]=((Integer)vy.get(ib)).intValue()+10;
ay[3]=((Integer)vy.get(ib)).intValue()+10;
a=new Polygon(ax,ay,4);
if(a.contains(x0,y0))
{
flagb=1;
pressPoint=ib;
}
}
if(flagb==0)
{
Integer IntX0=new Integer(x0);
Integer IntY0=new Integer(y0);
vx.addElement(IntX0);
vy.addElement(IntY0);
size=vx.size();
g.setColor(Color.BLUE);
g.fillOval(IntX0.intValue()-3, IntY0.intValue()-3, 6, 6);
if(size>1)
{
g.drawLine(
((Integer)vx.get(size-2)).intValue(),
((Integer)vy.get(size-2)).intValue(),
((Integer)vx.get(size-1)).intValue(),
((Integer)vy.get(size-1)).intValue()
);
}
}
size=vx.size();
if(size>3)
{
repaint();
}
}
public void panel_mousePressedH(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<4;i++)
{
ax[0]=xh[i]-10;
ax[1]=xh[i]+10;
ax[2]=xh[i]+10;
ax[3]=xh[i]-10;
ay[0]=yh[i]-10;
ay[1]=yh[i]-10;
ay[2]=yh[i]+10;
ay[3]=yh[i]+10;
a=new Polygon(ax,ay,4);
if(a.contains(x0, y0))
{
kh=i;
}
}
}
public void panel_mouseDragged(MouseEvent e)
{
Graphics g=getGraphics();
if(flag==1)
{
int x1=e.getX();
int y1=e.getY();
x[k]=x1;
y[k]=y1;
repaint();
g.clearRect(0, 0, 500, 500);
g.drawLine(x[0], y[0], x[1], y[1]);
g.drawLine(x[1], y[1], x[2], y[2]);
g.drawLine(x[2], y[2], x[3], y[3]);
g.drawString(x[0]+","+y[0], x[0], y[0]);
g.drawString(x[1]+","+y[1], x[1], y[1]);
g.drawString(x[2]+","+y[2], x[2], y[2]);
g.drawString(x[3]+",