// This program is a working example of a java program that can draw some // simple graphics. You can use it as a template but at least change the // class name and the drawstuff method name to something else. import java.awt.*; import java.awt.Graphics; import javax.swing.*; public class tri17 extends JFrame { private int width; // dimensions of window in pixels private int height; private Graphics display; // drawing object // constructor public tri17(int x, int y)// x,y indicate width, height { width=x; height=y; this.setBounds(0,0,width,height); setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); display = this.getGraphics(); // drawing object display.setColor(Color.white); display.fillRect(0,0,width,height); // draw solid white rectangle try { Thread.sleep(500); } catch (Exception e) {} // synch with system // trianglel(); // call user function that draws display.setColor(Color.blue); int x1= width/2, y1 = 40; int x2= 20, y2=height-20; int x3 = width-20, y3= height-20; triangle(x1,y1,x2,y2,x3,y3); } // whith recursion protected void triangle(int x1,int y1,int x2,int y2,int x3, int y3) { display.drawLine(x1,y1,x2,y2); display.drawLine(x2,y2,x3,y3); display.drawLine(x1,y1,x3,y3); try { Thread.sleep(10); } catch (Exception e) {} if (Math.abs(x2-x3)>=4) // continue? { // compute midpoints: int lx = (x1+x2)/2, ly = (y1+y2)/2; int rx = (x1+x3)/2, ry = (y1+y3)/2; int bx = (x2+x3)/2, by = (y2+y3)/2; // make 3 recursive calls triangle(x1,y1,lx,ly,rx,ry); triangle(lx,ly,x2,y2,bx,by); triangle(rx,ry,bx,by,x3,y3); }// if continue }// why is this a safe use of recursion? // "without" recursion protected void trianglel() { display.setColor(Color.blue); int x1= width/2, y1 = 40; int x2= 20, y2=height-20; int x3 = width-20, y3= height-20; // draw three lines of triangle LinkedList Stack = new LinkedList(); // see LinkedList.java Stack.push(new tinfo(x1,y1,x2,y2,x3,y3)); while (Stack.size()>0) { // pop stack: tinfo tf = Stack.pop(); x1= tf.x1; y1=tf.y1; // load into convenient vars x2= tf.x2; y2=tf.y2; x3= tf.x3; y3=tf.y3; display.drawLine(x1,y1,x2,y2); display.drawLine(x2,y2,x3,y3); display.drawLine(x1,y1,x3,y3); // try { Thread.sleep(10); } catch (Exception e) {} if (Math.abs(x2-x3)>=4) // continue? { // compute midpoints: int lx = (x1+x2)/2, ly = (y1+y2)/2; int rx = (x1+x3)/2, ry = (y1+y3)/2; int bx = (x2+x3)/2, by = (y2+y3)/2; // push three frames on stack Stack.push(new tinfo(x1,y1,lx,ly,rx,ry)); Stack.push(new tinfo(lx,ly,x2,y2,bx,by)); Stack.push(new tinfo(rx,ry,bx,by,x3,y3)); }// if continue }//main recursion loop }// user-defined drawing function // the following method is called when the window needs to be repainted: public void paint(Graphics g) { } // override autopaint method // main must create an instance of tri17 class: public static void main(String[] args) { tri17 t = new tri17(800,700); // create instance } class tinfo // internal class for one triangle { int x1, x2, x3, y1, y2, y3; public tinfo(int a, int b, int c, int d, int e, int f) { x1=a; y1 = b; x2=c; y2=d; x3 =e; y3=f; } }//tinfo for stack frames }//tri17