import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class Triangle extends GameCanvas implements Runnable{ double xp1=getWidth()-2; double yp1=getWidth()-2; double xp2=2; double yp2=getWidth()-2; double sin60=Math.sin (3.14/3.); int nivel_de_recursividad=5;//getWidth()>getHeight()?getWidth()/30: getHeight()/30; boolean run; Thread t=new Thread(this); public Triangle() { super(true); setFullScreenMode(true); } public void start(){ run=true; t.start(); } public void run(){ Graphics g=getGraphics(); while (run) { paintHolst(g); try {t.sleep(10);} catch(Exception exc){} } } public void paintHolst(Graphics g){ g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); g.setColor(0); paintRecursivo(g,nivel_de_recursividad,xp1,yp1,xp2,yp2); flushGraphics(); } private void paintRecursivo(Graphics g, int i, double xp12, double yp12, double xp22, double yp22 ) { double dx=(xp22-xp12) /2; double dy=(yp22- yp12)/2; double xp32=xp12+dx-2*dy*sin60; double yp32=yp12+dy+2*dx*sin60; double dx1=(xp22+xp12)/2; double dy1=(yp22+yp12)/2; double dx2=(xp32+xp22)/2; double dy2=(yp32+yp22)/2; double dx3=(xp12+xp32)/2; double dy3= (yp12+yp32)/2; if (i<=0) { g.fillTriangle((int)xp12,(int)yp12,(int)xp22,(int)yp22,(int)xp32,(int)yp32); g.drawLine((int)xp12,(int)yp12,(int)xp22,(int)yp22); g.drawLine((int)xp22,(int)yp22,(int)xp32,(int)yp32); g.drawLine((int)xp32,(int)yp32,(int)xp12,(int)yp12); } else { paintRecursivo(g,i-1,xp12,yp12,dx1,dy1); paintRecursivo(g,i-1,dx1,dy1,xp22,yp22); paintRecursivo(g,i-1,dx3,dy3,dx2,dy2); } } }