import java.awt.*; import java.awt.event.*; public class Fly extends Thread implements MouseListener { public int angle; public int x; public int y; public Network fly; public FlyApplet app; public double cos( double a , int radius ) { a = ( a * Math.PI ) / 180; return Math.cos( a ) * radius; } public double sin( double a , int radius ) { a = ( a * Math.PI ) / 180; return Math.sin( a ) * radius; } public Fly( FlyApplet a , Network n ) { app = a; x = 300; y = 100; angle = 170; fly = n; app.addMouseListener(this); } public Image drawFly() { int lwx; int lwy; int rwx; int rwy; Image a; Graphics g; a = app.createImage(20,20); g = a.getGraphics(); g.setColor(Color.white); g.fillRect(0,0,20,20); lwy = (int) (sin(angle , 8) + 8); lwx = (int) (-1 * cos(angle , 8) + 8); rwy = (int) (-1 * sin(angle , 8) + 8); rwx = (int) (cos(angle , 8) + 8); g.setColor( Color.black ); g.fillOval( 5 , 5 , 10 , 10 ); g.setColor( new Color( ((fly.LEFT.getRate() / 3) + 125) % 255 , 0, 175 ) ); g.fillOval( lwx , lwy , 4 , 4 ); g.setColor( new Color( ((fly.RIGHT.getRate() / 3) + 125) % 255 , 0, 175 ) ); g.fillOval( rwx , rwy , 4 , 4 ); return a; } public void update() { int lwing = fly.LEFT.getRate(); int rwing = fly.RIGHT.getRate(); if (lwing < 5) lwing = 5; if (rwing < 5) rwing = 5; if (rwing >= lwing) angle = (angle + ((rwing / lwing) /2)) % 360; else angle = (angle - ((lwing / rwing) / 2)) % 360 ; int tmp_x = x; int tmp_y = y; int factor = (lwing + rwing) / 20; tmp_y -= (int) (sin(angle + 90 , factor)); tmp_x += (int) (cos(angle + 90 , factor)); if (tmp_x < 0) tmp_x = 600; if (tmp_y < 0) tmp_y = 400; if (tmp_x > 600) tmp_x = 0; if (tmp_y > 400) tmp_y = 0; x = tmp_x; y = tmp_y; } public void stimulate() { int opp = Math.abs(x - app.stim_x) ; int adj = Math.abs(y - app.stim_y) ; double hyp = Math.sqrt( adj*adj + opp*opp ); double rad_ang = Math.asin(opp / hyp); int deg_ang = (int) (rad_ang * 180 / Math.PI); int ang = 0; if (app.stim_y < y && app.stim_x < x) ang = deg_ang - angle + 90; else if (app.stim_y > y && app.stim_x > x) ang = 270 - angle + deg_ang; else if (app.stim_y < y && app.stim_x > x) ang = 90 - deg_ang - angle; else if (app.stim_y > y && app.stim_x < x) ang = 90 - (angle - 180) - deg_ang; while (ang < 0) ang += 360; while (ang > 360) ang -= 360; if (ang <= 45 || ang > 345) fly.d.fire(); else if (ang <= 90) fly.c.fire(); else if (ang <= 135) fly.b.fire(); else if (ang <= 195) fly.a.fire(); } public void run() { Graphics g = app.getGraphics(); Image a = app.createImage(600,600); Graphics gt = a.getGraphics(); while (true) { stimulate(); update(); gt.setColor(Color.white); gt.fillRect(0,0,600,600); gt.drawImage( drawFly() , x , y, null ); gt.setColor(Color.black); gt.fillOval( app.stim_x , app.stim_y , 5 , 5 ); g.drawImage( a , 0 , 0 , null); app.stim_x += (int) (Math.random() * 9) - 4; app.stim_y += (int) (Math.random() * 9) - 4; try {Thread.sleep(100);} catch (Exception e) {} } } public void mouseClicked( MouseEvent e ) { app.stim_x = e.getX(); app.stim_y = e.getY(); } public void mousePressed(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseReleased(MouseEvent e){} }