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




代码如下:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import net.java2000.tools.NoNull;

/**
* 一段眼睛跟着鼠标转动的跟踪眼代码。<br>
* 你可以单独运行,或者放在html里面<br>
* <applet code="Eye" codebase="codebase" width="400" height="135"
* name="eyesApplet"><br>
* <param name="faceFile" value="doofus.jpg"/><br>
* <param name="testMode" value="false"/> <br>
* <param name="leftEyeX" value="75"/> <br>
* <param name="leftEyeY" value="77"/> <br>
* <param name="rightEyeX" value="310"/> <br>
* <param name="rightEyeY" value="75"/><br>
* <param name="irisRadius" value="20"/> <br>
* <param name="pupilRadius" value="8"/><br>
* <param name="leftEyeRadius" value="5"/><br>
* <param name="rightEyeRadius" value="5"/> <br>
* <param name="horizontalSkew" value="3.5"/><br>
* <param name="eyeIndependence" value="0.4"/> <br>
* <param name="irisRed" value="128"/><br>
* <param name="irisGreen" value="64"/> <br>
* <param name="irisBlue" value="0"/> <br>
* <param name="verticalOffset" value="100"/> <br>
* </applet>
*
* @author 赵学庆,Java世纪网(java2000.net)
*
*/
public class Eye extends Applet {
private static final long serialVersionUID = 4124530672062457469L;
private String mErrorMessage;
private Image mFace;
private Color mIrisColor, mPupilColor = Color.black;
private int mMouseX, mMouseY;
private int mLeftEyeX, mLeftEyeY, mRightEyeX, mRightEyeY;
private int mLeftIrisX, mLeftIrisY, mRightIrisX, mRightIrisY;
private int mLeftPupilX, mLeftPupilY, mRightPupilX, mRightPupilY;
private int mIrisRadius, mPupilRadius;
private int mLeftEyeRadius, mRightEyeRadius, mLeftPupilTR, mRightPupilTR;
private int mVerticalOffset;

// 默认值
private int mFaceX = 0, mFaceY = 0; // image start at 0, 0
private int mIrisRed = 128, mIrisGreen = 64, mIrisBlue = 0;
private double mHorizontalSkew = 3.5, mEyeIndependence = 0.5, mGapFactor = 1.5;
private boolean mTestMode = false;

private Dimension mDimension;
private Image mImage;
private Graphics mGraphics;

public void init() {
mErrorMessage = null;

try {
// 设置的一些参数
// 背景的面部图片
mFace = getImage(getCodeBase(), NoNull.toString(getParameter("faceFile"), "doofus.jpg"));

// 左侧眼睛的x坐标
mLeftEyeX = mLeftIrisX = mLeftPupilX = Integer.parseInt(NoNull.toString(
getParameter("leftEyeX"), "75"));

// 左侧眼睛的y坐标
mLeftEyeY = mLeftIrisY = mLeftPupilY = Integer.parseInt(NoNull.toString(
getParameter("leftEyeY"), "77"));

// 右侧眼睛的x坐标
mRightEyeX = mRightIrisX = mRightPupilX = Integer.parseInt(NoNull.toString(
getParameter("rightEyeX"), "310"));

// 右侧眼睛的y坐标
mRightEyeY = mRightIrisY = mRightPupilY = Integer.parseInt(NoNull.toString(
getParameter("rightEyeY"), "75"));

// 眼睛的白眼球半径
mIrisRadius = Integer.parseInt(NoNull.toString(getParameter("irisRadius"), "20"));

// 眼睛的瞳孔半径
mPupilRadius = Integer.parseInt(NoNull.toString(getParameter("pupilRadius"), "8"));

// 左眼睛的移动半径
mLeftEyeRadius = Integer.parseInt(NoNull.toString(getParameter("leftEyeRadius"), "15"));

// 右眼睛的移动半径
mRightEyeRadius = Integer.parseInt(NoNull.toString(getParameter("rightEyeRadius"), "5"));

// 可选参数
if (getParameter("testMode") != null)
mTestMode = Boolean.valueOf(NoNull.toString(getParameter("testMode"), "true"))
.booleanValue();

if (getParameter("horizontalSkew") != null)
mHorizontalSkew = Double.valueOf(
NoNull.toString(getParameter("horizontalSkew"), "13.5")).doubleValue();
if (getParameter("eyeIndependence") != null)
mEyeIndependence = Double.valueOf(
NoNull.toString(getParameter("eyeIndependence"), "0.4")).doubleValue();
if (getParameter("irisRed") != null)
mIrisRed = Integer.parseInt(NoNull.toString(getParameter("irisRed"), "128"));
if (getParameter("irisGreen") != null)
mIrisGreen = Integer.parseInt(NoNull.toString(getParameter("irisGreen"), "64"));
if (getParameter("irisBlue") != null)
mIrisBlue = Integer.parseInt(NoNull.toString(getParameter("irisBlue"), "0"));
mIrisColor = new Color(mIrisRed, mIrisGreen, mIrisBlue);
if (getParameter("verticalOffset") != null)
mVerticalOffset = Integer.parseInt(NoNull.toString(getParameter("verticalOffset"),
"100"));
} catch (Exception e) {
mErrorMessage = "Bad or missing required parameter.";
e.printStackTrace();
}

// 计算眼球的移动半径
mLeftPupilTR = mLeftEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
mRightPupilTR = mRightEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);

// 侦听鼠标事件
MouseMotion aMouseMotion = new MouseMotion();
this.addMouseMotionListener(aMouseMotion);
this.setSize(400, 135);
}

public void paintFrame(Graphics g) {
if (mErrorMessage != null) {
showError(g);
return;
}

// 背景面部
g.drawImage(mFace, mFaceX, mFaceY, this);

// 画外部的球体
g.setColor(mIrisColor);
g.fillOval(mLeftIrisX - mIrisRadius, mLeftIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
g.fillOval(mRightIrisX - mIrisRadius, mRightIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
// 画瞳孔
g.setColor(mPupilColor);
g.fillOval(mLeftPupilX - mPupilRadius, mLeftPupilY - mPupilRadius, 2 * mPupilRadius,
2 * mPupilRadius);
g.fillOval(mRightPupilX - mPupilRadius, mRightPupilY - mPupilRadius, 2 * mPupilRadius,
2 * mPupilRadius);

if (mTestMode) {
g.drawOval(mLeftEyeX - mLeftEyeRadius, mLeftEyeY - mLeftEyeRadius, 2 * mLeftEyeRadius,
2 * mLeftEyeRadius);
g.drawOval(mRightEyeX - mRightEyeRadius, mRightEyeY - mRightEyeRadius,
2 * mRightEyeRadius, 2 * mRightEyeRadius);
}
}

public void mouseMoved() {
// coordinates for the left iris
int leftDX = mMouseX - mLeftEyeX;
int leftDY = mMouseY - mLeftEyeY;
if (leftDY == 0)
leftDY = 1; // prevent divide by zero
double leftDXDY = (double) leftDX / leftDY;
double leftdy = Math.sqrt(Math.pow(mLeftEyeRadius, 2) / (Math.pow(leftDXDY, 2) + 1));
if (leftDY < 0) {
leftdy = -leftdy;
}
double leftdx = leftDXDY * leftdy * mHorizontalSkew;

// coordinates for the right iris
int rightDX = mMouseX - mRightEyeX;
int rightDY = mMouseY - mRightEyeY;
if (rightDY == 0)
rightDY = 1; // prevent divide by zero
double rightDXDY = (double) rightDX / rightDY;
double rightdy = Math.sqrt(Math.pow(mRightEyeRadius, 2) / (Math.pow(rightDXDY, 2) + 1));
if (rightDY < 0) {
rightdy = -rightdy;
}
double rightdx = rightDXDY * rightdy * mHorizontalSkew;

// adjustments for the irises
double avedx = (rightdx + leftdx) / 2;
double avedy = (rightdy + leftdy) / 2;
leftdx = leftdx + (avedx - leftdx) * (1 - mEyeIndependence);
rightdx = rightdx + (avedx - rightdx) * (1 - mEyeIndependence);
leftdy = leftdy + (avedy - leftdy) * (1 - mEyeIndependence);
rightdy = rightdy + (avedy - rightdy) * (1 - mEyeIndependence);
// new iris positions
mLeftIrisX = mLeftEyeX + (int) leftdx;
mLeftIrisY = mLeftEyeY + (int) leftdy;
mRightIrisX = mRightEyeX + (int) rightdx;
mRightIrisY = mRightEyeY + (int) rightdy;

// coordinates for the left pupil
double leftpdy = Math.sqrt(Math.pow(mLeftPupilTR, 2) / (Math.pow(leftDXDY, 2) + 1));
if (leftDY < 0) {
leftpdy = -leftpdy;
}
double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);

// coordinates for the right pupil
double rightpdy = Math.sqrt(Math.pow(mRightPupilTR, 2) / (Math.pow(rightDXDY, 2) + 1));
if (rightDY < 0) {
rightpdy = -rightpdy;
}
double rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);

// adjustments for the pupils
double avepdx = (rightpdx + leftpdx) / 2;
double avepdy = (rightpdy + leftpdy) / 2;
leftpdx = leftpdx + (avepdx - leftpdx) * (1 - mEyeIndependence);
rightpdx = rightpdx + (avepdx - rightpdx) * (1 - mEyeIndependence);
leftpdy = leftpdy + (avepdy - leftpdy) * (1 - mEyeIndependence);
rightpdy = rightpdy + (avepdy - rightpdy) * (1 - mEyeIndependence);

// new pupil positions
mLeftPupilX = mLeftEyeX + (int) leftpdx;
mLeftPupilY = mLeftEyeY + (int) leftpdy;
mRightPupilX = mRightEyeX + (int) rightpdx;
mRightPupilY = mRightEyeY + (int) rightpdy;

repaint();
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
Dimension d = getSize();

// create the offscreen graphics context
if ((mGraphics == null) || (d.width != mDimension.width)
|| (d.height != mDimension.height)) {
mDimension = d;
mImage = createImage(d.width, d.height);
mGraphics = mImage.getGraphics();
}

// erase the previous image
mGraphics.setColor(getBackground());
mGraphics.fillRect(0, 0, d.width, d.height);
mGraphics.setColor(Color.black);

// paint the frame into the image
paintFrame(mGraphics);

// paint the image onto the screen
g.drawImage(mImage, 0, 0, null);
}

class MouseMotion extends java.awt.event.MouseMotionAdapter {
public void mouseMoved(java.awt.event.MouseEvent event) {
Object object = event.getSource();
if (object == Eye.this)
mouseMovedInApplet(event);
}
}

void mouseMovedInApplet(java.awt.event.MouseEvent event) {
// get the mouse coords
mMouseX = event.getX();
mMouseY = event.getY();
mouseMoved();
}

public void mouseMovedInBrowser(int x, int y, int windowWidth) {
int appletW = getSize().width;

// adjust mouse x and y relative to applet position
mMouseX = x - (windowWidth - appletW) / 2;
mMouseY = y - mVerticalOffset;
mouseMoved();
}

private void showError(Graphics g) {
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
g.drawString(mErrorMessage, 10, 20);
}
}

(0)

相关推荐

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

    复制代码 代码如下: import java.applet.Applet; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import net.java2000.tools.NoNull; /** * 一段眼睛跟着鼠标转动的跟踪眼代码.<br> * 你可以单独运行,或者放在html里面<br&g

  • JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动效果示例

    本文实例讲述了JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动.分享给大家供大家参考,具体如下: 1. 盒子跟着鼠标移动 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no,

  • Opencv实现眼睛控制鼠标的实践

    如何用眼睛来控制鼠标?一种基于单一前向视角的机器学习眼睛姿态估计方法.在此项目中,每次单击鼠标时,我们都会编写代码来裁剪你们的眼睛图像.使用这些数据,我们可以反向训练模型,从你们您的眼睛预测鼠标的位置.在开始项目之前,我们需要引入第三方库. # For monitoring web camera and performing image minipulations import cv2 # For performing array operations import numpy as np #

  • JS实现的表格行鼠标点击高亮效果代码

    本文实例讲述了JS实现的表格行鼠标点击高亮效果代码.分享给大家供大家参考,具体如下: 偶尔在看天天基金网净值公告时,从源代码里剥下的一段js代码,感觉比较实用,分享一下(仅IE有效) <script type="text/javascript"> var highlightcolor='#E0F2FE'; var clickcolor='#ffedd2'; function MouseOver(){ var source=event.srcElement; if (sour

  • PHP使用debug_backtrace方法跟踪调试代码调用详解

    本文实例讲述了PHP使用debug_backtrace方法跟踪调试代码调用.分享给大家供大家参考,具体如下: 在开发过程中,例如要修改别人开发的代码或调试出问题的代码,需要对代码流程一步步去跟踪,找到出问题的地方进行修改.如果有一个方法可以获取到某段代码是被哪个方法调用,并能一直回溯到最开始调用的地方(包括调用的文件,行数,参数等),这样就能很方便的定位到出问题的地方. 使用php的debug_backtrace方法可以对代码调用进行跟踪,方便调试代码.它可以生成一个关联数组,数组中会收集当前应

  • jQuery实现鼠标跟随提示层效果代码(可显示文本,Div,Table,Html等)

    本文实例讲述了jQuery实现鼠标跟随提示层效果代码.分享给大家供大家参考,具体如下: 运行效果截图如下: Web网站有不少需要用到tip提示层的地方,结合jquery的jquery.cluetip.js ,可以实现本地字符或ajax异步调用显示提示层.如以上购评分明细提示.(优势:宽度需要调用页加载时定义,高度auto , 且三角指定图标能根据显示层的大小位置来调用它的位置,达到醒目直观的效果) jquery.cluetip.css /* global */ #cluetip-close im

  • 利用Python实现Windows下的鼠标键盘模拟的实例代码

    本文介绍了利用Python实现Windows下的鼠标键盘模拟的实例代码,分享给大家 本来用按键精灵是可以实现我的需求,而且更简单,但既然学python ,就看一下呗. 依赖: PyUserInput pip install PyUserInput PyUserInput 依赖 pyhook,所以还得安装 pyhook.按需下载,下载地址. 我是 win10 64 位 python 2.7,用的是第二个,下载之后用解压软件打开,把 pyHook放到C:\Python27\Lib\site-pack

  • C++制作鼠标连点器实例代码

    众所周知······ 鼠标连点器是个东西······ 那么你会编吗?······ 尤其是用C++的那种······ 不会也没有关系,今天,同我一起,看看就会. #include<stdio.h> #include<windows.h> int main() { while(1) { if(GetAsyncKeyState(VK_SPACE))//按下空格,开始执行 { while(1) { mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_L

  • javascript之鼠标拖动位置互换效果代码

    鼠标拖动,位置互换效果,主要用于div,提高用户体验等方面 div.box{ position:relative; padding:5px; background-color:#000; width:300px; height:300px; overflow:hidden; } div.box div.item{ float:left; width:90px; height:90px; background-color:#ccc; margin:5px; position:relative; c

  • 多浏览器兼容的获取元素和鼠标的位置的js代码

    复制代码 代码如下: //获取元素的位置 function getLeft(obj) { if (obj == null) return null; var mendingObj = obj; var mendingLeft = mendingObj.offsetLeft; while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != &q

随机推荐