BBS水木清华站∶精华区

发信人: andrewleu (Andy), 信区: Java 
标  题: An applet written by me 
发信站: BBS 水木清华站 (Fri May  1 09:12:47 1998) WWW-POST 
 
 
/* 
这是我写的一个小程序 
类似xwindows中的Xeyes 
希望你能喜欢 
*/ 
/* 
<html> 
<head> 
<title>The J Eyes </title> 
<!created by Liu Shu, 28-Apr-1998 --> 
</head> 
<applet code="Jeyes.class" width=500 height=300> 
</applet> 
<font SIZE="-1" FACE="Arial"><a HREF="andrewleu@hotmail.com">mailto:andrewleu@hotmail.com">andrewleu@hotmail.com 
</html> 
**/ 
 
import java.applet.*; 
import java.awt.*; 
import java.lang.Math; 
 
public class Jeyes extends Applet implements Runnable  

    Dimension d; 
    Thread motor ; 
    Image offScreen; 
    Graphics offScreenGraphics; 
    boolean loaded ; 
    int w    , h; 
    int x, y; 
    //mouse Position 
    boolean first=true; 
    int eyeX,eyeY; 
    //eyeball position 
    public void init(){ 
       d=this.size(); 
       w=d.width;h=d.height; 
       offScreen=createImage(w,h); 
       offScreenGraphics=offScreen.getGraphics(); 
     } 
public void    paint(Graphics g) 
    { 
      int leftCoX,leftCoY,rightCoX,rightCoY; 
      float arcLeft,arcRight; 
      if (first) 
       {  
        g.setColor(Color.black); 
        g.fillOval(20,20,w/2-40,h-40); 
        g.fillOval(w/2+20,20,w/2-40,h-40); 
        g.setColor(Color.white); 
        g.fillOval(35,35,w/2-70,h-70); 
        g.fillOval(w/2+35,35,w/2-70,h-70);// Eyes 
        g.setColor(Color.black); 
        g.fillOval(w/4, h/2,50,50); 
        g.fillOval(3*w/4,h/2,50,50);  //Eye balls 
        first=false; 
        } 
      else 
      {  
        offScreenGraphics.setColor(Color.black); 
        offScreenGraphics.fillOval(20,20,w/2-40,h-40); 
        offScreenGraphics.fillOval(w/2+20,20,w/2-40,h-40); 
        offScreenGraphics.setColor(Color.white); 
        offScreenGraphics.fillOval(35,35,w/2-70,h-70); 
        offScreenGraphics.fillOval(w/2+35,35,w/2-70,h-70);  //Eyes  
        offScreenGraphics.setColor(Color.black); 
        // next line to calculate the arc  
        leftCoX=x-w/4;leftCoY=y-h/2; 
        rightCoX=x-3*w/4;rightCoY=y-h/2; 
        arcLeft=(float) Math.atan((double)leftCoY/leftCoX); 
        if(leftCoX<0) arcLeft+=3.1415926;//since atan result is between -pi/2 --p/2 
        arcRight=(float)Math.atan((double)rightCoY/rightCoX); 
        if(rightCoX<0) arcRight+=3.1415926; 
        eyeBallPosition(leftCoX ,leftCoY,arcLeft); 
        offScreenGraphics.fillOval(w/4+eyeX-25,h/2+eyeY-25,50,50); 
 
        eyeBallPosition(rightCoX,rightCoY,arcRight); 
        offScreenGraphics.fillOval(3*w/4+eyeX-25,h/2+eyeY-25,50,50); 
        //offScreenGraphics.fillOval(w/4, h/2,50,50); 
        //offScreenGraphics.fillOval(3*w/4,h/2,50,50);     //Eye balls 
        g.drawImage(offScreen,0,0,null); 
      } 
 
    } 
public void eyeBallPosition(int CoX, int CoY, float Arc) 

if((CoX*CoX+CoY*CoY)<h/5*h/5) 
//the mouse in the eye pit 
  { 
       eyeX=CoX;eyeY=CoY; 
  } 
else 
   { 
       eyeX=(int)(Math.cos((double)Arc)*h/5); 
       eyeY=(int)(Math.sin((double)Arc)*h/5); 
    } 
}     
public void     update(Graphics g) 
    { 
       paint(g); 
    } 
    public void run() 
    { 
       while(true) 
       { 
           //mouseMove(new Event e, new int x,new int y); 
           repaint(); 
           try{ 
           motor.sleep(100); 
           } 
           catch(Exception e) 
           { 
             System.out.println("Exception\n"); 
           } 
       } 
    } 
    public void start() 
    { 
      motor=new Thread(this); 
      motor.setPriority(Thread.MIN_PRIORITY); 
      motor.start(); 
    } 
    public void stop() 
    { 
     } 
public boolean   mouseMove(Event e,int X,int Y) 
    { 
        x=X; 
        y=Y; 
        //System.out.println("x="+x+"  "+"y="+y); 
        return (true); 
    } 

        
        
 
 
-- 
爱在不言中...... 
 
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.99.61.172]  

BBS水木清华站∶精华区