CopyCode The Code is as follows: 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;
/**
* An eye tracking code with the mouse rotating. <Br>
* You can run it separately or put it in 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 Zhao Xueqing, Java century network (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;
// Default Value
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 {
// Set some parameters
// Face image of the background
Mface = getimage (getcodebase (), nonull. tostring (getparameter ("facefile"), "doofus.jpg "));
// X coordinate of the left eye
Mlefteyex = mleftirisx = mleftpupilx = integer. parseint (nonull. tostring (
Getparameter ("lefteyex"), "75 "));
// Y coordinate of the left eye
Mlefteyey = mleftirisy = mleftpupily = integer. parseint (nonull. tostring (
Getparameter ("lefteyey"), "77 "));
// X coordinate of the right eye
Mrighteyex = mrightirisx = mrightpupilx = integer. parseint (nonull. tostring (
Getparameter ("righteyex"), "310 "));
// Y coordinate of the right eye
Mrighteyey = mrightirisy = mrightpupily = integer. parseint (nonull. tostring (
Getparameter ("righteyey"), "75 "));
// The White-Eye radius of the eye
Mirisradius = integer. parseint (nonull. tostring (getparameter ("irisradius"), "20 "));
// Pupil radius of the eye
Mpupilradius = integer. parseint (nonull. tostring (getparameter ("pupilradius"), "8 "));
// Radius of Left Eye Movement
Mlefteyeradius = integer. parseint (nonull. tostring (getparameter ("lefteyeradius"), "15 "));
// Radius of movement of the right eye
Mrighteyeradius = integer. parseint (nonull. tostring (getparameter ("righteyeradius"), "5 "));
// Optional parameter
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 ();
}
// Calculate the eye movement radius
Mleftpupiltr = mlefteyeradius + mirisradius-(INT) (mgapfactor * mpupilradius );
Mrightpupiltr = mrighteyeradius + mirisradius-(INT) (mgapfactor * mpupilradius );
// Listen for mouse events
Mousemotion amousemotion = new mousemotion ();
This. addmousemotionlistener (amousemotion );
This. setsize (400,135 );
}
Public void paintframe (Graphics g ){
If (merrormessage! = NULL ){
Showerror (g );
Return;
}
// Background face
G. drawimage (mface, mfacex, mfacey, this );
// Draw an external sphere
G. setcolor (miriscolor );
G. filloval (mleftirisx-mirisradius, mleftirisy-mirisradius, 2 * mirisradius,
2 * mirisradius );
G. filloval (mrightirisx-mirisradius, mrightirisy-mirisradius, 2 * mirisradius,
2 * mirisradius );
// Pupil
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 1_wwidth ){
Int appletw = getsize (). width;
// Adjust mouse X and Y relative to applet position
Mmousex = x-(required wwidth-appletw)/2;
Mmousey = Y-mverticaloffset;
Mousemoved ();
}
Private void showerror (Graphics g ){
G. setfont (new font ("timesroman", Font. Bold, 12 ));
G. drawstring (merrormessage, 10, 20 );
}
}