import java.awt.*; import java.awt.event.*; import java.applet.*; /** *

タイトル:

*

説明:

*

著作権: Copyright (c) 2004

*

会社名:

* @author 未入力 * @version 1.0 */ public class Applet1 extends Applet { private boolean isStandalone = false; int[] spx=new int[800]; int[] spy=new int[800]; int px[]={30,60,150,200,250}; int py[]={200,80,50,220,50}; int nump=0; int tmax; int match=-1; int [] nv1 = {0,1,2,3,4,5,6,7}; int [] nv2 = {0,0,0,1,2,3,3,3}; int [] nv = {1,2,3,4,5,6,7,8}; boolean funcview=false,curvview=false; boolean profile=false; private Button button1 = new Button(); private Button button2 = new Button(); private Checkbox checkbox1 = new Checkbox(); private Checkbox checkbox2 = new Checkbox(); private CheckboxGroup checkboxGroup1 = new CheckboxGroup(); //引数値の取得 public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //アプレットのビルド public Applet1() { } //アプレットの初期化 public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //コンポーネントの初期化 private void jbInit() throws Exception { button1.setLabel("基底関数"); button1.setBounds(new Rectangle(16, 254, 71, 27)); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); this.setLayout(null); button2.setLabel("曲線"); button2.setBounds(new Rectangle(108, 255, 57, 26)); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { this_mousePressed(e); } }); this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { this_mouseDragged(e); } }); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { this_keyReleased(e); } }); checkbox1.setCheckboxGroup(checkboxGroup1); checkbox1.setLabel("[1,2,3,4,5,6,7]"); checkbox1.setBounds(new Rectangle(198, 245, 96, 18)); checkbox2.setCheckboxGroup(checkboxGroup1); checkbox2.setLabel("[0,0,0,1,2,3,3,3]"); checkbox2.setState(true); checkbox2.setBounds(new Rectangle(198, 268, 104, 21)); this.add(button1, null); this.add(button2, null); this.add(checkbox1, null); this.add(checkbox2, null); } //アプレットの情報取得 public String getAppletInfo() { return "アプレット情報"; } //引数情報の取得 public String[][] getParameterInfo() { return null; } public void paint(Graphics g){ if(profile){//制御点多角形を表示 g.setColor(new Color(150,150,250)); for(int i=0;i<5;i++){ if(i>0)g.drawLine(px[i-1],py[i-1],px[i],py[i]); g.drawRect(px[i]-3,py[i]-3,5,5); } } g.setColor(Color.BLACK); for(int i=1;i20)&&(i<50))||tmax==3)||funcview){ g.drawLine(spx[i-1],spy[i-1],spx[i],spy[i]); } } } double baseN(int i,int k,double t,int nv[]){ double w1=0.0,w2=0.0; if(k==1){ if(t >=nv[i] && t=3.0 では基底関数は0になる double r=baseN(j,3,t,nv); //if(j==3) System.out.println("baseFun:"+t+" "+r); //spx[k]=i*10+50; spx[k]=i*300/(tmax*10)+20; spy[k++]=200-(int)(r*150); //System.out.println("btn1:"+j+" k:"+k); } //nump=31*5; nump=(tmax*10+1)*5; profile=false; repaint(); } } void button2_actionPerformed(ActionEvent e) { if(checkbox1.getState()){nv=nv1;tmax=7;} else {nv=nv2;tmax=3;}; //曲線の計算 BspCalc(); profile=true; funcview=false; repaint(); } void BspCalc(){ //int [] nv = {0,0,0,1,2,3,3,3};tmax=3 //int [] nv = {0,1,2,3,4,5,6,7}; tmax=7; double t,r; int j; for( j=0;j<=tmax*10;j++){ t=(double)j/10.0; if (j==tmax*10) t -= 0.001;//t>=tmax では基底関数は0になる double x=0.0,y=0.0; for(int i=0;i<=4;i++){ r=baseN(i,3,t,nv); x += px[i]*r; y += py[i]*r; //if(j<20)System.out.println("drawFun:"+i+" "+x+" "+y+" r:"+r+" t:"+t); } spx[j]=(int)x; spy[j]=(int)y; //System.out.println("drawpos:"+j+" "+spx[j]+" "+spy[j]); } //nump=(tmax*10+1)*5; nump=j--; } double dist(double p1[], double p2[]) { //2点間の距離を返す return Math.sqrt((p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1])); } public void this_mousePressed(MouseEvent e){ //マウスのボタンが押された match=-1; double mpoint[]=new double[2]; double ppoint[]=new double[2]; //近い点を探す mpoint[0]=e.getX();mpoint[1]=e.getY(); for(int i=0;i= 0) { px[match]=e.getX(); py[match]=e.getY(); } BspCalc(); repaint();//再表示 profile=true; } void this_keyReleased(KeyEvent e) { match=-1; } }//class