⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 眼睛.txt

📁 用JAVA人眼图形
💻 TXT
字号:
/*
这是我写的一个小程序
类似xwindows中的Xeyes
希望你能喜欢
500*300
*/
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);
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -