一段眼睛跟着鼠标转动的跟踪眼代码

原文: http://www.java2000.net/p10992

运行效果

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import net.java2000.tools.NoNull;
  8. /**
  9.  * 一段眼睛跟着鼠标转动的跟踪眼代码。<br>
  10.  * 你可以单独运行,或者放在html里面<br>
  11.  * <applet code="Eye" codebase="codebase" width="400" height="135"
  12.  * name="eyesApplet"><br>
  13.  * <param name="faceFile" value="doofus.jpg"/><br>
  14.  * <param name="testMode" value="false"/> <br>
  15.  * <param name="leftEyeX" value="75"/> <br>
  16.  * <param name="leftEyeY" value="77"/> <br>
  17.  * <param name="rightEyeX" value="310"/> <br>
  18.  * <param name="rightEyeY" value="75"/><br>
  19.  * <param name="irisRadius" value="20"/> <br>
  20.  * <param name="pupilRadius" value="8"/><br>
  21.  * <param name="leftEyeRadius" value="5"/><br>
  22.  * <param name="rightEyeRadius" value="5"/> <br>
  23.  * <param name="horizontalSkew" value="3.5"/><br>
  24.  * <param name="eyeIndependence" value="0.4"/> <br>
  25.  * <param name="irisRed" value="128"/><br>
  26.  * <param name="irisGreen" value="64"/> <br>
  27.  * <param name="irisBlue" value="0"/> <br>
  28.  * <param name="verticalOffset" value="100"/> <br>
  29.  * </applet>
  30.  * 
  31.  * @author 赵学庆,Java世纪网(java2000.net)
  32.  * 
  33.  */
  34. public class Eye extends Applet {
  35.   private static final long serialVersionUID = 4124530672062457469L;
  36.   private String mErrorMessage;
  37.   private Image mFace;
  38.   private Color mIrisColor, mPupilColor = Color.black;
  39.   private int mMouseX, mMouseY;
  40.   private int mLeftEyeX, mLeftEyeY, mRightEyeX, mRightEyeY;
  41.   private int mLeftIrisX, mLeftIrisY, mRightIrisX, mRightIrisY;
  42.   private int mLeftPupilX, mLeftPupilY, mRightPupilX, mRightPupilY;
  43.   private int mIrisRadius, mPupilRadius;
  44.   private int mLeftEyeRadius, mRightEyeRadius, mLeftPupilTR, mRightPupilTR;
  45.   private int mVerticalOffset;
  46.   // 默认值
  47.   private int mFaceX = 0, mFaceY = 0// image start at 0, 0
  48.   private int mIrisRed = 128, mIrisGreen = 64, mIrisBlue = 0;
  49.   private double mHorizontalSkew = 3.5, mEyeIndependence = 0.5, mGapFactor = 1.5;
  50.   private boolean mTestMode = false;
  51.   private Dimension mDimension;
  52.   private Image mImage;
  53.   private Graphics mGraphics;
  54.   public void init() {
  55.     mErrorMessage = null;
  56.     try {
  57.       // 设置的一些参数
  58.       // 背景的面部图片
  59.       mFace = getImage(getCodeBase(), NoNull.toString(getParameter("faceFile"), "doofus.jpg"));
  60.       // 左侧眼睛的x坐标
  61.       mLeftEyeX = mLeftIrisX = mLeftPupilX = Integer.parseInt(NoNull.toString(
  62.           getParameter("leftEyeX"), "75"));
  63.       // 左侧眼睛的y坐标
  64.       mLeftEyeY = mLeftIrisY = mLeftPupilY = Integer.parseInt(NoNull.toString(
  65.           getParameter("leftEyeY"), "77"));
  66.       // 右侧眼睛的x坐标
  67.       mRightEyeX = mRightIrisX = mRightPupilX = Integer.parseInt(NoNull.toString(
  68.           getParameter("rightEyeX"), "310"));
  69.       // 右侧眼睛的y坐标
  70.       mRightEyeY = mRightIrisY = mRightPupilY = Integer.parseInt(NoNull.toString(
  71.           getParameter("rightEyeY"), "75"));
  72.       // 眼睛的白眼球半径
  73.       mIrisRadius = Integer.parseInt(NoNull.toString(getParameter("irisRadius"), "20"));
  74.       // 眼睛的瞳孔半径
  75.       mPupilRadius = Integer.parseInt(NoNull.toString(getParameter("pupilRadius"), "8"));
  76.       // 左眼睛的移动半径
  77.       mLeftEyeRadius = Integer.parseInt(NoNull.toString(getParameter("leftEyeRadius"), "15"));
  78.       // 右眼睛的移动半径
  79.       mRightEyeRadius = Integer.parseInt(NoNull.toString(getParameter("rightEyeRadius"), "5"));
  80.       // 可选参数
  81.       if (getParameter("testMode") != null)
  82.         mTestMode = Boolean.valueOf(NoNull.toString(getParameter("testMode"), "true"))
  83.             .booleanValue();
  84.       if (getParameter("horizontalSkew") != null)
  85.         mHorizontalSkew = Double.valueOf(
  86.             NoNull.toString(getParameter("horizontalSkew"), "13.5")).doubleValue();
  87.       if (getParameter("eyeIndependence") != null)
  88.         mEyeIndependence = Double.valueOf(
  89.             NoNull.toString(getParameter("eyeIndependence"), "0.4")).doubleValue();
  90.       if (getParameter("irisRed") != null)
  91.         mIrisRed = Integer.parseInt(NoNull.toString(getParameter("irisRed"), "128"));
  92.       if (getParameter("irisGreen") != null)
  93.         mIrisGreen = Integer.parseInt(NoNull.toString(getParameter("irisGreen"), "64"));
  94.       if (getParameter("irisBlue") != null)
  95.         mIrisBlue = Integer.parseInt(NoNull.toString(getParameter("irisBlue"), "0"));
  96.       mIrisColor = new Color(mIrisRed, mIrisGreen, mIrisBlue);
  97.       if (getParameter("verticalOffset") != null)
  98.         mVerticalOffset = Integer.parseInt(NoNull.toString(getParameter("verticalOffset"),
  99.             "100"));
  100.     } catch (Exception e) {
  101.       mErrorMessage = "Bad or missing required parameter.";
  102.       e.printStackTrace();
  103.     }
  104.     // 计算眼球的移动半径
  105.     mLeftPupilTR = mLeftEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
  106.     mRightPupilTR = mRightEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
  107.     // 侦听鼠标事件
  108.     MouseMotion aMouseMotion = new MouseMotion();
  109.     this.addMouseMotionListener(aMouseMotion);
  110.     this.setSize(400135);
  111.   }
  112.   public void paintFrame(Graphics g) {
  113.     if (mErrorMessage != null) {
  114.       showError(g);
  115.       return;
  116.     }
  117.     // 背景面部
  118.     g.drawImage(mFace, mFaceX, mFaceY, this);
  119.     // 画外部的球体
  120.     g.setColor(mIrisColor);
  121.     g.fillOval(mLeftIrisX - mIrisRadius, mLeftIrisY - mIrisRadius, 2 * mIrisRadius,
  122.         2 * mIrisRadius);
  123.     g.fillOval(mRightIrisX - mIrisRadius, mRightIrisY - mIrisRadius, 2 * mIrisRadius,
  124.         2 * mIrisRadius);
  125.     // 画瞳孔
  126.     g.setColor(mPupilColor);
  127.     g.fillOval(mLeftPupilX - mPupilRadius, mLeftPupilY - mPupilRadius, 2 * mPupilRadius,
  128.         2 * mPupilRadius);
  129.     g.fillOval(mRightPupilX - mPupilRadius, mRightPupilY - mPupilRadius, 2 * mPupilRadius,
  130.         2 * mPupilRadius);
  131.     if (mTestMode) {
  132.       g.drawOval(mLeftEyeX - mLeftEyeRadius, mLeftEyeY - mLeftEyeRadius, 2 * mLeftEyeRadius,
  133.           2 * mLeftEyeRadius);
  134.       g.drawOval(mRightEyeX - mRightEyeRadius, mRightEyeY - mRightEyeRadius,
  135.           2 * mRightEyeRadius, 2 * mRightEyeRadius);
  136.     }
  137.   }
  138.   public void mouseMoved() {
  139.     // coordinates for the left iris
  140.     int leftDX = mMouseX - mLeftEyeX;
  141.     int leftDY = mMouseY - mLeftEyeY;
  142.     if (leftDY == 0)
  143.       leftDY = 1// prevent divide by zero
  144.     double leftDXDY = (double) leftDX / leftDY;
  145.     double leftdy = Math.sqrt(Math.pow(mLeftEyeRadius, 2) / (Math.pow(leftDXDY, 2) + 1));
  146.     if (leftDY < 0) {
  147.       leftdy = -leftdy;
  148.     }
  149.     double leftdx = leftDXDY * leftdy * mHorizontalSkew;
  150.     // coordinates for the right iris
  151.     int rightDX = mMouseX - mRightEyeX;
  152.     int rightDY = mMouseY - mRightEyeY;
  153.     if (rightDY == 0)
  154.       rightDY = 1// prevent divide by zero
  155.     double rightDXDY = (double) rightDX / rightDY;
  156.     double rightdy = Math.sqrt(Math.pow(mRightEyeRadius, 2) / (Math.pow(rightDXDY, 2) + 1));
  157.     if (rightDY < 0) {
  158.       rightdy = -rightdy;
  159.     }
  160.     double rightdx = rightDXDY * rightdy * mHorizontalSkew;
  161.     // adjustments for the irises
  162.     double avedx = (rightdx + leftdx) / 2;
  163.     double avedy = (rightdy + leftdy) / 2;
  164.     leftdx = leftdx + (avedx - leftdx) * (1 - mEyeIndependence);
  165.     rightdx = rightdx + (avedx - rightdx) * (1 - mEyeIndependence);
  166.     leftdy = leftdy + (avedy - leftdy) * (1 - mEyeIndependence);
  167.     rightdy = rightdy + (avedy - rightdy) * (1 - mEyeIndependence);
  168.     // new iris positions
  169.     mLeftIrisX = mLeftEyeX + (int) leftdx;
  170.     mLeftIrisY = mLeftEyeY + (int) leftdy;
  171.     mRightIrisX = mRightEyeX + (int) rightdx;
  172.     mRightIrisY = mRightEyeY + (int) rightdy;
  173.     // coordinates for the left pupil
  174.     double leftpdy = Math.sqrt(Math.pow(mLeftPupilTR, 2) / (Math.pow(leftDXDY, 2) + 1));
  175.     if (leftDY < 0) {
  176.       leftpdy = -leftpdy;
  177.     }
  178.     double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);
  179.     // coordinates for the right pupil
  180.     double rightpdy = Math.sqrt(Math.pow(mRightPupilTR, 2) / (Math.pow(rightDXDY, 2) + 1));
  181.     if (rightDY < 0) {
  182.       rightpdy = -rightpdy;
  183.     }
  184.     double rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);
  185.     // adjustments for the pupils
  186.     double avepdx = (rightpdx + leftpdx) / 2;
  187.     double avepdy = (rightpdy + leftpdy) / 2;
  188.     leftpdx = leftpdx + (avepdx - leftpdx) * (1 - mEyeIndependence);
  189.     rightpdx = rightpdx + (avepdx - rightpdx) * (1 - mEyeIndependence);
  190.     leftpdy = leftpdy + (avepdy - leftpdy) * (1 - mEyeIndependence);
  191.     rightpdy = rightpdy + (avepdy - rightpdy) * (1 - mEyeIndependence);
  192.     // new pupil positions
  193.     mLeftPupilX = mLeftEyeX + (int) leftpdx;
  194.     mLeftPupilY = mLeftEyeY + (int) leftpdy;
  195.     mRightPupilX = mRightEyeX + (int) rightpdx;
  196.     mRightPupilY = mRightEyeY + (int) rightpdy;
  197.     repaint();
  198.   }
  199.   public void update(Graphics g) {
  200.     paint(g);
  201.   }
  202.   public void paint(Graphics g) {
  203.     Dimension d = getSize();
  204.     // create the offscreen graphics context
  205.     if ((mGraphics == null) || (d.width != mDimension.width)
  206.         || (d.height != mDimension.height)) {
  207.       mDimension = d;
  208.       mImage = createImage(d.width, d.height);
  209.       mGraphics = mImage.getGraphics();
  210.     }
  211.     // erase the previous image
  212.     mGraphics.setColor(getBackground());
  213.     mGraphics.fillRect(00, d.width, d.height);
  214.     mGraphics.setColor(Color.black);
  215.     // paint the frame into the image
  216.     paintFrame(mGraphics);
  217.     // paint the image onto the screen
  218.     g.drawImage(mImage, 00null);
  219.   }
  220.   class MouseMotion extends java.awt.event.MouseMotionAdapter {
  221.     public void mouseMoved(java.awt.event.MouseEvent event) {
  222.       Object object = event.getSource();
  223.       if (object == Eye.this)
  224.         mouseMovedInApplet(event);
  225.     }
  226.   }
  227.   void mouseMovedInApplet(java.awt.event.MouseEvent event) {
  228.     // get the mouse coords
  229.     mMouseX = event.getX();
  230.     mMouseY = event.getY();
  231.     mouseMoved();
  232.   }
  233.   public void mouseMovedInBrowser(int x, int y, int windowWidth) {
  234.     int appletW = getSize().width;
  235.     // adjust mouse x and y relative to applet position
  236.     mMouseX = x - (windowWidth - appletW) / 2;
  237.     mMouseY = y - mVerticalOffset;
  238.     mouseMoved();
  239.   }
  240.   private void showError(Graphics g) {
  241.     g.setFont(new Font("TimesRoman", Font.BOLD, 12));
  242.     g.drawString(mErrorMessage, 1020);
  243.   }
  244. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值