import net.java2000.tools.NoNull;
/**
* Ein Tracking-Augencode, der dafür sorgt, dass die Augen der Bewegung der Maus folgen. <br>
* Sie können es alleine ausführen oder in HTML einfügen<br>
* <applet code="Eye" codebase="codebase"
* 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>
*
* @Autor Zhao Xueqing, Java Century Network (java2000.net)
*
*/
öffentliche Klasse Eye erweitert Applet {
private static final long serialVersionUID = 4124530672062457469L;
privater String mErrorMessage;
privates Bild mFace;
private Farbe 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 mirIrisRadius, mPupilRadius;
private int mLeftEyeRadius, mRightEyeRadius, mLeftPupilTR, mRightPupilTR;
private int mVerticalOffset;
// Standardwert
private int mFaceX = 0, mFaceY = 0; // Bild beginnt bei 0, 0
private int mIrisRed = 128, mIrisGreen = 64, mIrisBlue = 0;
privates Double mHorizontalSkew = 3,5, mEyeIndependence = 0,5, mGapFactor = 1,5;
privater boolescher Wert mTestMode = false;
private Dimension mDimension;
privates Bild mImage;
private Grafik mGraphics;
public void init() {
mErrorMessage = null;
versuchen {
// Einige Parameter zum Festlegen
// Hintergrundbild des Gesichts
mFace = getImage(getCodeBase(), NoNull.toString(getParameter("faceFile"), "doofus.jpg"));
// x-Koordinate des linken Auges
mLeftEyeX = mLeftIrisX = mLeftPupilX = Integer.parseInt(NoNull.toString(
getParameter("leftEyeX"), "75"));
//Y-Koordinate des linken Auges
mLeftEyeY = mLeftIrisY = mLeftPupilY = Integer.parseInt(NoNull.toString(
getParameter("leftEyeY"), "77"));
//x-Koordinate des rechten Auges
mRightEyeX = mRightIrisX = mRightPupilX = Integer.parseInt(NoNull.toString(
getParameter("rightEyeX"), "310"));
//Y-Koordinate des rechten Auges
mRightEyeY = mRightIrisY = mRightPupilY = Integer.parseInt(NoNull.toString(
getParameter("rightEyeY"), "75"));
//Der Radius des weißen Augapfels des Auges
mIrisRadius = Integer.parseInt(NoNull.toString(getParameter("irisRadius"), "20"));
//Der Pupillenradius des Auges
mPupilRadius = Integer.parseInt(NoNull.toString(getParameter("pupilRadius"), "8"));
//Bewegungsradius des linken Auges
mLeftEyeRadius = Integer.parseInt(NoNull.toString(getParameter("leftEyeRadius"), "15"));
//Bewegungsradius des rechten Auges
mRightEyeRadius = Integer.parseInt(NoNull.toString(getParameter("rightEyeRadius"), "5"));
// optionale 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 = neue Farbe(mIrisRed, mIrisGreen, mIrisBlue);
if (getParameter("verticalOffset") != null)
mVerticalOffset = Integer.parseInt(NoNull.toString(getParameter("verticalOffset"),
„100“));
} Catch (Ausnahme e) {
mErrorMessage = „Ungültiger oder fehlender erforderlicher Parameter.“;
e.printStackTrace();
}
// Berechne den Bewegungsradius des Augapfels
mLeftPupilTR = mLeftEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
mRightPupilTR = mRightEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
//Mausereignisse abhören
MouseMotion aMouseMotion = new MouseMotion();
this.addMouseMotionListener(aMouseMotion);
this.setSize(400, 135);
}
public void paintFrame(Graphics g) {
if (mErrorMessage != null) {
showError(g);
zurückkehren;
}
// Hintergrundgesicht
g.drawImage(mFace, mFaceX, mFaceY, this);
// Zeichne die äußere Kugel
g.setColor(mIrisColor);
g.fillOval(mLeftIrisX - mIrisRadius, mLeftIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
g.fillOval(mRightIrisX - mIrisRadius, mRightIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
// Schüler zeichnen
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() {
// Koordinaten für die linke Iris
int leftDX = mMouseX - mLeftEyeX;
int leftDY = mMouseY - mLeftEyeY;
if (leftDY == 0)
leftDY = 1; // Division durch Null verhindern
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;
// Koordinaten für die rechte Iris
int rightDX = mMouseX - mRightEyeX;
int rightDY = mMouseY - mRightEyeY;
if (rightDY == 0)
rightDY = 1; // Division durch Null verhindern
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;
// Anpassungen für die Iris
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);
// neue Irispositionen
mLeftIrisX = mLeftEyeX + (int) leftdx;
mLeftIrisY = mLeftEyeY + (int) leftdy;
mRightIrisX = mRightEyeX + (int) rightdx;
mRightIrisY = mRightEyeY + (int) rightdy;
// Koordinaten für die linke Pupille
double leftpdy = Math.sqrt(Math.pow(mLeftPupilTR, 2) / (Math.pow(leftDXDY, 2) + 1));
if (leftDY < 0) {
leftpdy = -leftpdy;
}
double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);
// Koordinaten für den richtigen Schüler
double rightpdy = Math.sqrt(Math.pow(mRightPupilTR, 2) / (Math.pow(rightDXDY, 2) + 1));
if (rightDY < 0) {
rightpdy = -rightpdy;
}
double rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);
// Anpassungen für die Schüler
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);
// neue Schülerstellen
mLeftPupilX = mLeftEyeX + (int) leftpdx;
mLeftPupilY = mLeftEyeY + (int) leftpdy;
mRightPupilX = mRightEyeX + (int) rightpdx;
mRightPupilY = mRightEyeY + (int) rightpdy;
neu streichen();
}
public void update(Graphics g) {
Farbe(g);
}
Farbe für öffentliche Hohlräume (Grafiken g) {
Dimension d = getSize();
// den Offscreen-Grafikkontext erstellen
if ((mGraphics == null) || (d.width != mDimension.width)
||. (d.height != mDimension.height)) {
mDimension = d;
mImage = createImage(d.width, d.height);
mGraphics = mImage.getGraphics();
}
// vorheriges Bild löschen
mGraphics.setColor(getBackground());
mGraphics.fillRect(0, 0, d.width, d.height);
mGraphics.setColor(Color.black);
// Den Rahmen in das Bild malen
paintFrame(mGraphics);
// Malen Sie das Bild auf den Bildschirm
g.drawImage(mImage, 0, 0, null);
}
Klasse MouseMotion erweitert java.awt.event.MouseMotionAdapter {
public void mouseMoved(java.awt.event.MouseEvent event) {
Objekt object = event.getSource();
if (object == Eye.this)
mouseMovedInApplet(event);
}
}
void mouseMovedInApplet(java.awt.event.MouseEvent event) {
// Holen Sie sich die Mauskoordinaten
mMouseX = event.getX();
mMouseY = event.getY();
mouseMoved();
}
public void mouseMovedInBrowser(int x, int y, int windowWidth) {
int appletW = getSize().width;
// Maus x und y relativ zur Applet-Position anpassen
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);
}
}