2023年12月3日发(作者:)
一段眼睛跟着鼠标转动的跟踪眼代码原文:
运行效果
1. import ;2. import ;3. import ion;4. import ;5. import cs;6. import ;7.
8. import ;9.
10. /**11. * 一段眼睛跟着鼠标转动的跟踪眼代码。
12. * 你可以单独运行,或者放在html里面
13. *
15. *
16. *
17. *
18. *
19. *
20. *
21. *
22. *
23. *
24. *
25. *
26. *
27. *
28. *
29. *
30. *
31. * 32. *
33. * @author 赵学庆,Java世纪网()34. *
35. */36. public class Eye extends Applet {37. private static final long serialVersionUID = 4124539L;38. private String mErrorMessage;39. private Image mFace;40. private Color mIrisColor, mPupilColor = ;41. private int mMouseX, mMouseY;42. private int mLeftEyeX, mLeftEyeY, mRightEyeX, mRightEyeY;43. private int mLeftIrisX, mLeftIrisY, mRightIrisX, mRightIrisY;44. private int mLeftPupilX, mLeftPupilY, mRightPupilX, mRightPupilY;45. private int mIrisRadius, mPupilRadius;46. private int mLeftEyeRadius, mRightEyeRadius, mLeftPupilTR, mRightPupilTR;47. private int mVerticalOffset;48.
49. // 默认值50. private int mFaceX = 0, mFaceY = 0; // image start at 0, 051. private int mIrisRed = 128, mIrisGreen = 64, mIrisBlue = 0;52. private double mHorizontalSkew = 3.5, mEyeIndependence = 0.5, mGapFactor = 1.5;53. private boolean mTestMode = false;54.
55. private Dimension mDimension;56. private Image mImage;57. private Graphics mGraphics;58.
59. public void init() {60. mErrorMessage = null;61.
62. try {63. // 设置的一些参数64. // 背景的面部图片65. mFace = getImage(getCodeBase(), ng(getParameter("faceFile"), ""));66.
67. // 左侧眼睛的x坐标68. mLeftEyeX = mLeftIrisX = mLeftPupilX = nt(ng(69. getParameter("leftEyeX"), "75"));70.
71. // 左侧眼睛的y坐标72. mLeftEyeY = mLeftIrisY = mLeftPupilY = nt(ng(73. getParameter("leftEyeY"), "77"));74.
75. // 右侧眼睛的x坐标76. mRightEyeX = mRightIrisX = mRightPupilX = nt(ng(77. getParameter("rightEyeX"), "310"));78.
79. // 右侧眼睛的y坐标80. mRightEyeY = mRightIrisY = mRightPupilY = nt(ng(81. getParameter("rightEyeY"), "75"));82.
83. // 眼睛的白眼球半径84. mIrisRadius = nt(ng(getParameter("irisRadius"), "20"));85.
86. // 眼睛的瞳孔半径87. mPupilRadius = nt(ng(getParameter("pupilRadius"), "8"));88.
89. // 左眼睛的移动半径90. mLeftEyeRadius = nt(ng(getParameter("leftEyeRadius"), "15"));91.
92. // 右眼睛的移动半径93. mRightEyeRadius = nt(ng(getParameter("rightEyeRadius"), "5"));94.
95. // 可选参数96. if (getParameter("testMode") != null)97. mTestMode = f(ng(getParameter("testMode"), "true"))98. .booleanValue();99.
100. if (getParameter("horizontalSkew") != null)101. mHorizontalSkew = f(102. ng(getParameter("horizontalSkew"), "13.5")).doubleValue();103. if (getParameter("eyeIndependence") != null)104. mEyeIndependence = f(105. ng(getParameter("eyeIndependence"), "0.4")).doubleValue();106. if (getParameter("irisRed") != null)107. mIrisRed = nt(ng(getParameter("irisRed"), "128"));108. if (getParameter("irisGreen") != null)109. mIrisGreen = nt(ng(getParameter("irisGreen"), "64"));110. if (getParameter("irisBlue") != null)111. mIrisBlue = nt(ng(getParameter("irisBlue"), "0"));112. mIrisColor = new Color(mIrisRed, mIrisGreen, mIrisBlue);113. if (getParameter("verticalOffset") != null)114. mVerticalOffset = nt(ng(getParameter("verticalOffset"),115. "100"));116. } catch (Exception e) {117. mErrorMessage = "Bad or missing required parameter.";118. tackTrace();119. }120.
121. // 计算眼球的移动半径122. mLeftPupilTR = mLeftEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);123. mRightPupilTR = mRightEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);124.
125. // 侦听鼠标事件126. MouseMotion aMouseMotion = new MouseMotion();127. seMotionListener(aMouseMotion);128. e(400, 135);129. }130.
131. public void paintFrame(Graphics g) {132. if (mErrorMessage != null) {133. showError(g);134. return;135. }136.
137. // 背景面部138. age(mFace, mFaceX, mFaceY, this);139.
140. // 画外部的球体141. or(mIrisColor);142. al(mLeftIrisX - mIrisRadius, mLeftIrisY - mIrisRadius, 2 * mIrisRadius,143. 2 * mIrisRadius);144. al(mRightIrisX - mIrisRadius, mRightIrisY - mIrisRadius, 2 * mIrisRadius,145. 2 * mIrisRadius);146. // 画瞳孔147. or(mPupilColor);148. al(mLeftPupilX - mPupilRadius, mLeftPupilY - mPupilRadius, 2 * mPupilRadius,149. 2 * mPupilRadius);150. al(mRightPupilX - mPupilRadius, mRightPupilY - mPupilRadius, 2 * mPupilRadius,151. 2 * mPupilRadius);152.
153. if (mTestMode) {154. al(mLeftEyeX - mLeftEyeRadius, mLeftEyeY - mLeftEyeRadius, 2 * mLeftEyeRadius,155. 2 * mLeftEyeRadius);156. al(mRightEyeX - mRightEyeRadius, mRightEyeY - mRightEyeRadius,157. 2 * mRightEyeRadius, 2 * mRightEyeRadius);158. }159. }160.
161. public void mouseMoved() {162. // coordinates for the left iris163. int leftDX = mMouseX - mLeftEyeX;164. int leftDY = mMouseY - mLeftEyeY;165. if (leftDY == 0)166. leftDY = 1; // prevent divide by zero167. double leftDXDY = (double) leftDX / leftDY;168. double leftdy = ((mLeftEyeRadius, 2) / ((leftDXDY, 2) + 1));169. if (leftDY < 0) {170. leftdy = -leftdy;171. }172. double leftdx = leftDXDY * leftdy * mHorizontalSkew;173.
174. // coordinates for the right iris175. int rightDX = mMouseX - mRightEyeX;176. int rightDY = mMouseY - mRightEyeY;177. if (rightDY == 0)178. rightDY = 1; // prevent divide by zero179. double rightDXDY = (double) rightDX / rightDY;180. double rightdy = ((mRightEyeRadius, 2) / ((rightDXDY, 2) + 1));181. if (rightDY < 0) {182. rightdy = -rightdy;183. }184. double rightdx = rightDXDY * rightdy * mHorizontalSkew;185.
186. // adjustments for the irises187. double avedx = (rightdx + leftdx) / 2;188. double avedy = (rightdy + leftdy) / 2;189. leftdx = leftdx + (avedx - leftdx) * (1 - mEyeIndependence);190. rightdx = rightdx + (avedx - rightdx) * (1 - mEyeIndependence);191. leftdy = leftdy + (avedy - leftdy) * (1 - mEyeIndependence);192. rightdy = rightdy + (avedy - rightdy) * (1 - mEyeIndependence);193. // new iris positions194. mLeftIrisX = mLeftEyeX + (int) leftdx;195. mLeftIrisY = mLeftEyeY + (int) leftdy;196. mRightIrisX = mRightEyeX + (int) rightdx;197. mRightIrisY = mRightEyeY + (int) rightdy;198.
199. // coordinates for the left pupil200. double leftpdy = ((mLeftPupilTR, 2) / ((leftDXDY, 2) + 1));201. if (leftDY < 0) {202. leftpdy = -leftpdy;203. }204. double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);205. 206. // coordinates for the right pupil207. double rightpdy = ((mRightPupilTR, 2) / ((rightDXDY, 2) + 1));208. if (rightDY < 0) {209. rightpdy = -rightpdy;210. }211. double rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);212.
213. // adjustments for the pupils214. double avepdx = (rightpdx + leftpdx) / 2;215. double avepdy = (rightpdy + leftpdy) / 2;216. leftpdx = leftpdx + (avepdx - leftpdx) * (1 - mEyeIndependence);217. rightpdx = rightpdx + (avepdx - rightpdx) * (1 - mEyeIndependence);218. leftpdy = leftpdy + (avepdy - leftpdy) * (1 - mEyeIndependence);219. rightpdy = rightpdy + (avepdy - rightpdy) * (1 - mEyeIndependence);220.
221. // new pupil positions222. mLeftPupilX = mLeftEyeX + (int) leftpdx;223. mLeftPupilY = mLeftEyeY + (int) leftpdy;224. mRightPupilX = mRightEyeX + (int) rightpdx;225. mRightPupilY = mRightEyeY + (int) rightpdy;226.
227. repaint();228. }229.
230. public void update(Graphics g) {231. paint(g);232. }233.
234. public void paint(Graphics g) {235. Dimension d = getSize();236.
237. // create the offscreen graphics context238. if ((mGraphics == null) || ( != )239. || ( != )) {240. mDimension = d;241. mImage = createImage(, );242. mGraphics = phics();243. }244.
245. // erase the previous image246. or(getBackground());247. ct(0, 0, , );248. or();249.
250. // paint the frame into the image251. paintFrame(mGraphics);252.
253. // paint the image onto the screen254. age(mImage, 0, 0, null);255. }256.
257. class MouseMotion extends otionAdapter {258. public void mouseMoved(vent event) {259. Object object = rce();260. if (object == )261. mouseMovedInApplet(event);262. }263. }264.
265. void mouseMovedInApplet(vent event) {266. // get the mouse coords267. mMouseX = ();268. mMouseY = ();269. mouseMoved();270. }271.
272. public void mouseMovedInBrowser(int x, int y, int windowWidth) {273. int appletW = getSize().width;274.
275. // adjust mouse x and y relative to applet position276. mMouseX = x - (windowWidth - appletW) / 2;277. mMouseY = y - mVerticalOffset;278. mouseMoved();279. }280.
281. private void showError(Graphics g) {282. t(new Font("TimesRoman", , 12));283. ring(mErrorMessage, 10, 20);284. }285. }286.
发布评论