import java.html" target="_blank">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;

/**

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

* 你可以单独运行,或者放在html里面

* * name="eyesApplet">

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

* @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 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 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 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 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);

}

}

java 鼠标动眼睛动_java实现眼睛跟着鼠标转动的跟踪眼代码相关推荐

  1. java鼠标经过代码_一段眼睛跟着鼠标转动的跟踪眼代码

    import java.applet.Applet; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; i ...

  2. 瞧一瞧看一看啦“一段眼睛跟着鼠标转动的跟踪眼代码”

    import java.applet.Applet; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; i ...

  3. java焦点事件如何使用_Java中如何释放鼠标事件的焦?

    1.使用MouseListener接口处理鼠标事件 鼠标事件有5种:按下鼠标键,释放鼠标键,点击鼠标键,鼠标进入和鼠标退出 mousePressed(MouseEvent e) 鼠标按下时调用 mou ...

  4. js简单实现鼠标拖拽功能:盒子可以跟着鼠标移动位置

    实现的原理: 根据上面的图可以算出移动后的盒子的left和top: 鼠标距离边界的值: ev.pageY - box.top ev.pageX - box.left box2.top = ev2.pa ...

  5. java 判断是否是日期_java判断是否为日期的方法(附代码)

    1.使用正则判断是否日期(推荐:java视频教程)public boolean isDate(String date) { /** * 判断日期格式和范围 */ String rexp = " ...

  6. java 错误声音播放器_java 音频播放器出不了声音,代码里哪有问题啊?

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java ...

  7. java主界面设置背景图片_java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改)...

    java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改) 关注:223  答案:4  mip版 解决时间 2021-01-26 22:09 提问者非莪莫属 2021-01 ...

  8. java导出pdf 含图片_java 生成PDF含图片和中文件实现代码

    1,所需包 iText.jar iTextAsian.ar(支持中包) 2,列子 package com.pdf; import java.awt.Color; import java.io.File ...

  9. java单击按钮切换图片_JAVA点击按钮改变背景图片 跪求代码·

    展开全部 // 不加包,图片跟类文件在62616964757a686964616fe4b893e5b19e31333262383634一个目录,命令行下编译执行就行了 // 如果建工程,图片放到工程根 ...

最新文章

  1. GDC服务器主机与证书不匹配,调用web服务soap时,错误https URL主机名与客户端信任库中服务器证书上的公用名(CN)不匹配...
  2. Oracle 备份还原数据库练习.
  3. Mysql学习(一)之简单介绍
  4. C/Cpp / 设计模式 / 模板模式
  5. fork和vfork,return和exit的理解
  6. opencv利用矩形框选中某一区域_【从零学习OpenCV】4Ubuntu系统中安装OpenCV 4
  7. 工作260:js判断一个数组是否包含一个指定的值
  8. 32位、64汇编区别
  9. 弹出界面eth0/eth1错误 激活连接失败 master connection not found or invalid 解决办法
  10. Yarn的资源调度与隔离
  11. mysql6默认什么字符集_mysql默认字符集问题
  12. oracle如何删除物理表空间,oracle 如何删除被误删物理文件的表空间
  13. js操作动态表格内元素
  14. 12306 官网硬卧下铺的选择(亲测可用)
  15. visual studio python使用教程_教程:在 Visual Studio 中开始使用 Flask Web 框架
  16. Arthas--深入排查java进程消耗CPU或内存过高问题
  17. c#实现批量坐标方位角计算
  18. 鼠标计算机无法识别,计算机无法识别usb鼠标
  19. 山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(七)-SQL注入字符型
  20. 软件测试方法和技术第一章——引论

热门文章

  1. Ubuntu18.04 实现串口通信
  2. 【SDPTWVRP】基于matlab头脑风暴算法求解带时间窗和同时取送货车辆路径问题【含Matlab源码 1990期】
  3. [Acc]4379. 两个闹钟 暴力
  4. 茁壮浏览器 android,傲游浏览器六一纯真献礼 过个别开生面的儿童节
  5. nginx.pid-nginx: [error] open() /var/run/nginx.pid failed (2: No such file or direc
  6. 对冲基金经理的告别信 (ZT)
  7. 2022-2028年全球与中国端点保护平台(EPP)行业市场深度调研及投资预测分析
  8. POE供电 网线 电源 网络情况图
  9. QPBOC快速借贷记流程(2)
  10. 一次代码评审,差点过不了试用期!