/* Copyright F. P. Marin ( Mon Oct 11 07 20:07:07 1999 ) E-mail: felix@bloch.ciens.ucv.ve Permission to use, copy, modify and distribute this software and its documentation for NON-COMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. F. P. Marin makes no representations or warranties about the suitability of the software, either express or implied, including but no limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. F. P. Marin shall not be liable for any damage suffered by license as a result of using, modifying or distributing this software or its derivatives. */ import java.awt.*; import java.util.Random; public class PerroLiebre extends java.applet.Applet implements Runnable { static final Color COLORFONDO=Color.black.brighter(); static final Color COLORCIRCULO=Color.cyan.brighter(); static final Color COLORLIEBRE=Color.yellow.brighter(); static final Color COLORPERRO=Color.red.brighter(); static final double FACTOR=0.5,RAPIDEZLIEBRE=1.0; static final double FACTORDECONVERSION=180.0/Math.PI; static final double DT=0.5/RAPIDEZLIEBRE; static final double RAPIDEZPORDEFECTODELPERRO=1.01*RAPIDEZLIEBRE; static final int RADIOLIEBRE=3; Circulo c; PanelInferior pI=new PanelInferior(this); ControlPerro controlPerro=new ControlPerro(this); double t=0.0; Graphics theg; Image theimg; int h,h2,w,w2; Liebre liebre; Perro perro; Random r=new Random(); Thread plThread=null; public void init() { setLayout(new BorderLayout()); add("North",controlPerro); add("South",pI); h=size().height; w=size().width; h2=(int)(h/2.0 + 0.5); w2=(int)(w/2.0 + 0.5); c=new Circulo(this); theimg=createImage(w,h); theg=theimg.getGraphics(); theg.setColor(COLORFONDO); theg.fillRect(0,0,w,h); inicialize((double)c.xc,(double)c.yc,RAPIDEZPORDEFECTODELPERRO); } public void start() { if ( plThread==null ) { plThread=new Thread(this); plThread.start(); } } public void run() { double fase; // while ( true ) { repaint(); try { Thread.sleep(41); } catch(InterruptedException ie) {} if ( !perro.evolucion() ) break; t+=DT; fase=liebre.wl*t; liebre.x=c.xc + c.r*Math.cos(fase); liebre.y=c.yc + c.r*Math.sin(fase); fase+=perro.theta; perro.x=liebre.x + perro.rho*Math.cos(fase); perro.y=liebre.y + perro.rho*Math.sin(fase); } repaint(); try { Thread.sleep(41); } catch(InterruptedException ie) {} } public void update(Graphics g) { c.dibuje(theg); // el circulo liebre.dibuje(theg); // la liebre perro.dibuje(theg); // el perro theg.setColor(COLORCIRCULO); theg.drawLine(0,h2,w,h2); theg.drawLine(w2,0,w2,h); paint(g); } public void paint(Graphics g) { g.drawImage(theimg,0,0,this); } public void stop() { if ( plThread!=null ) { plThread.stop(); plThread=null; } } public static String con4Dec(double val) { return Math.floor(val*10000.0 + 0.5)/10000.0 + ""; } public void inicialize(double x,double y,double vp) { theg.setColor(COLORFONDO); theg.fillRect(0,0,w,h); t=0.0; liebre=new Liebre(c,this); perro=new Perro(x,y,vp,liebre,this); } public static double magnitud(double x,double y) { x=Math.abs(x); y=Math.abs(y); // if ( x0.0 ) { y/=x; x*=Math.sqrt(1.0 + y*y); } return x; } public boolean mouseMove(Event evnt,int xMouse,int yMouse) { pI.m.setPunto(xMouse,yMouse); pI.m.setRho0(xMouse,yMouse); pI.m.setTheta0(xMouse,yMouse); return true; } public boolean mouseUp(Event evnt,int xMouse,int yMouse) { stop(); // if ( perro!=null ) inicialize((double)xMouse,(double)yMouse, perro.vp); else inicialize((double)xMouse,(double)yMouse, RAPIDEZPORDEFECTODELPERRO); // start(); return true; } } /****************************EMPIEZA ANIMALES*****************************/ class Animal { Color color; double x,y; int intX,intY; PerroLiebre madre; public Animal(double x,double y,PerroLiebre madre) { this.madre=madre; this.x=x; this.y=y; intX=(int)(x + 0.5); intY=(int)(y + 0.5); } } class Liebre extends Animal { Circulo c; double xl0,yl0,wl; int d,penX,penY,r; public Liebre(Circulo c,PerroLiebre madre) { super((double)(c.xc + c.r),(double)c.yc,madre); xl0=x; yl0=y; penX=intX; penY=intY; this.c=c; wl=madre.RAPIDEZLIEBRE/c.r; color=madre.COLORLIEBRE; r=madre.RADIOLIEBRE; d=2*r; } public final void dibuje(Graphics g) { g.setColor(madre.COLORFONDO); g.fillOval(penX - r,penY - r,d,d); g.setColor(color); intX=(int)(x + 0.5); intY=(int)(y + 0.5); g.fillOval(intX - r,intY - r,d,d); penX=intX; penY=intY; } } class Perro extends Animal { double rho,theta,vp; Liebre laLiebre; public Perro(double vp,Liebre laLiebre,PerroLiebre madre) { this(madre.w/2.0,madre.h/2.0,vp,laLiebre,madre); } public Perro(double x,double y,double vp,Liebre laLiebre, PerroLiebre madre) { super(x,y,madre); this.laLiebre=laLiebre; color=madre.COLORPERRO; this.vp=vp; madre.controlPerro.actualizeVp(vp); // // Calculo de rho y theta rho=x - laLiebre.x; double temp=y - laLiebre.y; theta=Math.atan2(temp,rho); rho=madre.magnitud(rho,temp); } public final void dibuje(Graphics g) { g.setColor(color); intX=(int)(x - 0.5); intY=(int)(y - 0.5); g.fillOval(intX,intY,2,2); madre.controlPerro.ajusteRho(rho); madre.controlPerro.ajusteTheta(theta); } public final boolean evolucion() { double deltaT=0.0,dRho,dt,dTheta,rhoAnterior; // while ( deltaTwidth ) height=width; x=(int)((madre.w - width)/2.0 + 0.5); y=(int)((madre.h - height)/2.0 + 0.5); xc=x + (int)(width/2.0 + 0.5); yc=y + (int)(height/2.0 + 0.5); r=(int)(height/2.0 + 0.5); color=madre.COLORCIRCULO; } public final void dibuje(Graphics g) { g.setColor(color); g.drawArc(x,y,width,height,0,360); } } class ColoresDelPerro extends Panel { Color color[]={Color.blue.brighter(), Color.darkGray.brighter(), Color.gray.brighter(), Color.green.brighter(), Color.magenta.brighter(), Color.orange.brighter(), Color.red.brighter(), Color.white.brighter()}; // Button bColor[]= new Button[color.length]; PanelInferior madre; public ColoresDelPerro(PanelInferior madre) { super(); this.madre=madre; // setLayout(new GridLayout(1,bColor.length)); // int i=0; while ( i