이것저것 다 해봤는데 잘 안되서 ㅠㅠ 초보에게 힘을 주세요.
희1미햬
질문요약: 빨간색코드 부분에 보면 재귀 되면서 도스창에 제대로 값이 출력이 되는데요.이때 그냥 전역변수로 출력된 좌표값을 넘겨주고 paint함수를 호출하도록 했습니다.(본인생각;;)제 생각엔 값이 도출될때마다 페인트 함수(파란색코드)가 불려야 할것 같은데..
파란코드에서 주석처리한부분 출력해보면 한번씩밖에 출력이 안된요 ㅠㅠ 제 생각대로라면 처음엔 1번 두번째는 4번 세번째는 16번.......씩 증가해야 하는데...)
이상하게 한번씩 밖에 안불리네요 ㅠㅠ 제귀라서 버튼누룰수록 호출되는게 정상이거든요 ㅠㅠ
도와 주십시오..ㅠㅠ
-----------------------------------------------------------------------------------------------
import java.awt.*;import java.awt.event.*;import java.awt.Graphics;
class Koch_Curve_sub extends Frame implements ActionListener { double x; double y; int n; double dir; double Len; int count; double [] arr = new double[2]; private Button bt = new Button(확인); private BorderLayout b1 = new BorderLayout(); public Koch_Curve_sub(String title){ super(title); x= 80; y = 300; arr[0]= 560; arr[1]= 300; dir = 0; Len = 480*3; count=0; this.init(); this.start(); super.setSize(640,480); super.setLocation(0, 0); super.setResizable(false); super.setVisible(true); } public void init(){ this.setLayout(b1); this.add(South,bt); } public void start(){ bt.addActionListener(this); } public void koch(double dir, double Len, int n) { //재귀호출되는 함수 if( n 0) { koch(dir,Len,n-1); // draw
x=x+Len/3*Math.cos(dir*3.14/180); // draw /
y=y+Len/3*Math.sin(dir*3.14/180); dir = dir-60; koch(dir,Len,n-1);
x=x+Len/3*Math.cos(dir*3.14/180); // draw y=y+Len/3*Math.sin(dir*3.14/180); dir = dir+120; koch(dir,Len,n-1);
x=x+Len/3*Math.cos(dir*3.14/180); // draw y=y+Len/3*Math.sin(dir*3.14/180); dir = dir-60; koch(dir,Len,n-1); }
else { arr[0]=x+Len/3*Math.cos(dir); arr[1]=y+Len/3*Math.sin(dir); //g.drawLine(0, 0, 200, 800); System.out.println(x+ : +y+ : +arr[0]+ : +arr[1]); //repaint(); invalidate(); this.repaint(); }
}
public void update(Graphics g){ g.clearRect(0, 0, 640, 480); paint(g); }
public void paint(Graphics g){ //System.out.println(x+ : +y+ : +arr[0]+ : +arr[1]);
g.drawLine((int)x, (int)y, (int)arr[0], (int)arr[1]); }
public void actionPerformed(ActionEvent e){ if(e.getSource() == bt){ System.out.println(n:+n); koch( 0, Len, n); this.n+=1; this.x=80; this.y=300; this.Len = this.Len/3; } }}public class Koch_Curve { public static void main(String [] ar){ Koch_Curve_sub ex = new Koch_Curve_sub(제목); }
}