impor net.java2000.tools.NoNull;
/**
* Sepotong kode mata pelacak yang membuat mata mengikuti pergerakan mouse. <br>
* Anda dapat menjalankannya sendiri atau memasukkannya ke dalam html<br>
* <kode applet="Mata" basis kode="basis kode"
* nama="mataApplet"><br>
* <param name="faceFile" value="doofus.jpg"/><br>
* <param nama="testMode" value="false"/> <br>
* <param nama="leftEyeX" value="75"/> <br>
* <param name="leftEyeY" value="77"/> <br>
* <param nama="rightEyeX" value="310"/> <br>
* <param nama="rightEyeY" value="75"/><br>
* <param nama="irisRadius" value="20"/> <br>
* <param nama="pupilRadius" value="8"/><br>
* <param nama="leftEyeRadius" value="5"/><br>
* <param name="rightEyeRadius" value="5"/> <br>
* <param nama="horizontalSkew" value="3.5"/><br>
* <param name="eyeIndependence" value="0.4"/> <br>
* <param nama="irisRed" value="128"/><br>
* <param name="irisGreen" value="64"/> <br>
* <param name="irisBlue" value="0"/> <br>
* <nama param="verticalOffset" value="100"/> <br>
*</applet>
*
* @penulis Zhao Xueqing, Java Century Network (java2000.net)
*
*/
kelas publik Eye memperluas Applet {
serial panjang akhir statis pribadiVersionUID = 4124530672062457469L;
String pribadi mErrorMessage;
Gambar pribadi mFace;
Warna pribadi mIrisColor, mPupilColor = Warna.hitam;
pribadi int mMouseX, mMouseY;
int pribadi mLeftEyeX, mLeftEyeY, mRightEyeX, mRightEyeY;
int pribadi mLeftIrisX, mLeftIrisY, mRightIrisX, mRightIrisY;
int pribadi mLeftPupilX, mLeftPupilY, mRightPupilX, mRightPupilY;
pribadi int mIrisRadius, mPupilRadius;
int pribadi mLeftEyeRadius, mRightEyeRadius, mLeftPupilTR, mRightPupilTR;
pribadi int mVerticalOffset;
// nilai bawaan
private int mFaceX = 0, mFaceY = 0; // gambar dimulai dari 0, 0
private int mIrisRed = 128, mIrisGreen = 64, mIrisBlue = 0;
pribadi ganda mHorizontalSkew = 3,5, mEyeIndependence = 0,5, mGapFactor = 1,5;
boolean pribadi mTestMode = false;
Dimensi pribadi mDimensi;
Gambar pribadi mImage;
mGraphics Grafik pribadi;
kekosongan publik init() {
mErrorMessage = nol;
mencoba {
// Beberapa parameter untuk diatur
// gambar wajah latar belakang
mFace = getImage(getCodeBase(), NoNull.toString(getParameter("faceFile"), "doofus.jpg"));
// x koordinat mata kiri
mLefEyeX = mLeftIrisX = mLeftPupilX = Integer.parseInt(NoNull.toString(
getParameter("leftEyeX"), "75"));
//Koordinat Y mata kiri
mLefEyeY = mLefIrisY = mLeftPupilY = Integer.parseInt(NoNull.toString(
getParameter("Mata KiriY"), "77"));
//x koordinat mata kanan
mRightEyeX = mRightIrisX = mRightPupilX = Integer.parseInt(NoNull.toString(
getParameter("rightEyeX"), "310"));
//Koordinat Y mata kanan
mRightEyeY = mRightIrisY = mRightPupilY = Integer.parseInt(NoNull.toString(
getParameter("Mata Kanan"), "75"));
//Jari-jari bola mata putih
mIrisRadius = Integer.parseInt(NoNull.toString(getParameter("irisRadius"), "20"));
// Jari-jari pupil mata
mPupilRadius = Integer.parseInt(NoNull.toString(getParameter("pupilRadius"), "8"));
//Radius pergerakan mata kiri
mLeftEyeRadius = Integer.parseInt(NoNull.toString(getParameter("leftEyeRadius"), "15"));
//Radius pergerakan mata kanan
mRightEyeRadius = Integer.parseInt(NoNull.toString(getParameter("rightEyeRadius"), "5"));
// parameter opsional
jika (getParameter("testMode") != nol)
mTestMode = Boolean.valueOf(NoNull.toString(getParameter("testMode"), "true"))
.booleanValue();
jika (getParameter("horizontalSkew") != null)
mHorizontalSkew = Ganda.valueOf(
NoNull.toString(getParameter("horizontalSkew"), "13.5")).doubleValue();
jika (getParameter("eyeIndependence") != null)
mEyeIndependence = Ganda.valueOf(
NoNull.toString(getParameter("eyeIndependence"), "0,4")).doubleValue();
jika (getParameter("irisMerah") != null)
mIrisRed = Integer.parseInt(NoNull.toString(getParameter("irisRed"), "128"));
jika (getParameter("irisHijau") != null)
mIrisGreen = Integer.parseInt(NoNull.toString(getParameter("irisGreen"), "64"));
jika (getParameter("irisBiru") != nol)
mIrisBlue = Integer.parseInt(NoNull.toString(getParameter("irisBlue"), "0"));
mIrisColor = Warna baru (mIrisRed, mIrisGreen, mIrisBlue);
jika (getParameter("verticalOffset") != nol)
mVerticalOffset = Integer.parseInt(NoNull.toString(getParameter("verticalOffset"),
"100"));
} tangkapan (Pengecualian e) {
mErrorMessage = "Parameter yang diperlukan buruk atau tidak ada.";
e.printStackTrace();
}
// Hitung radius pergerakan bola mata
mLeftPupilTR = mLefEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
mRightPupilTR = mRightEyeRadius + mIrisRadius - (int) (mGapFactor * mPupilRadius);
//Mendengarkan event mouse
MouseMotion aMouseMotion = MouseMotion baru();
this.addMouseMotionListener(aMouseMotion);
ini.setSize(400, 135);
}
public void paintFrame(Grafik g) {
jika (mErrorMessage != null) {
showError(g);
kembali;
}
// latar belakang wajah
g.drawImage(mFace, mFaceX, mFaceY, ini);
// Gambarlah bola luarnya
g.setColor(mIrisColor);
g.fillOval(mLeftIrisX - mIrisRadius, mLeftIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
g.fillOval(mRightIrisX - mIrisRadius, mRightIrisY - mIrisRadius, 2 * mIrisRadius,
2 * mIrisRadius);
// menggambar murid
g.setColor(mPupilColor);
g.fillOval(mLeftPupilX - mPupilRadius, mLeftPupilY - mPupilRadius, 2 * mPupilRadius,
2 * mPupilRadius);
g.fillOval(mRightPupilX - mPupilRadius, mRightPupilY - mPupilRadius, 2 * mPupilRadius,
2 * mPupilRadius);
jika (mTestMode) {
g.drawOval(mLeftEyeX - mLefEyeRadius, mLefEyeY - mLefEyeRadius, 2 * mLefEyeRadius,
2 * mRadius Mata Kiri);
g.drawOval(mRightEyeX - mRightEyeRadius, mRightEyeY - mRightEyeRadius,
2 * mRadius Mata Kanan, 2 * mRadius Mata Kanan);
}
}
public void mouseMoved() {
// koordinat iris kiri
int leftDX = mMouseX - mLeftEyeX;
int leftDY = mMouseY - mLeftEyeY;
jika (kiriDY == 0)
leftDY = 1; // mencegah pembagian dengan nol
double leftDXDY = (ganda) leftDX / leftDY;
double leftdy = Math.sqrt(Math.pow(mLeftEyeRadius, 2) / (Math.pow(leftDXDY, 2) + 1));
if (kiriDY < 0) {
kidal = -kiri;
}
double leftdx = leftDXDY * leftdy * mHorizontalSkew;
// koordinat iris mata kanan
int kananDX = mMouseX - mRightEyeX;
int rightDY = mMouseY - mRightEyeY;
jika (kananDY == 0)
rightDY = 1; // cegah pembagian dengan nol
ganda kananDXDY = (ganda) kananDX / kananDY;
double rightdy = Math.sqrt(Math.pow(mRightEyeRadius, 2) / (Math.pow(rightDXDY, 2) + 1));
if (kananDY < 0) {
benar = -benar;
}
ganda kanandx = kananDXDY * kanan * mHorizontalSkew;
// penyesuaian untuk iris
double avedx = (kanandx + kiridx) / 2;
double avedy = (kanan + kiri) / 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);
// posisi iris baru
mLeftIrisX = mLefEyeX + (int) kiridx;
mLeftIrisY = mLefEyeY + (int) kidal;
mRightIrisX = mRightEyeX + (int) kanandx;
mRightIrisY = mRightEyeY + (int) kanan;
// koordinat murid kiri
double leftpdy = Math.sqrt(Math.pow(mLeftPupilTR, 2) / (Math.pow(leftDXDY, 2) + 1));
if (kiriDY < 0) {
kiripdy = -kiripdy;
}
double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);
// koordinat murid yang tepat
double rightpdy = Math.sqrt(Math.pow(mRightPupilTR, 2) / (Math.pow(rightDXDY, 2) + 1));
if (kananDY < 0) {
kananpdy = -kananpdy;
}
ganda rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);
// penyesuaian untuk murid
double avepdx = (pdx kanan + pdx kiri) / 2;
double avepdy = (pdy kanan + pdy kiri) / 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);
// posisi murid baru
mLeftPupilX = mLefEyeX + (int) kiripdx;
mLeftPupilY = mLefEyeY + (int) kiripdy;
mRightPupilX = mRightEyeX + (int) kananpdx;
mRightPupilY = mRightEyeY + (int) kananpdy;
mengecat ulang();
}
pembaruan kekosongan publik(Grafik g) {
cat(g);
}
cat kekosongan publik(Grafik g) {
Dimensi d = getSize();
// membuat konteks grafik di luar layar
if ((mGraphics == null) || (d.width != mDimension.width)
||. (d.tinggi != mDimensi.tinggi)) {
mDimensi = d;
mImage = createImage(d.lebar, d.tinggi);
mGraphics = mImage.getGraphics();
}
// hapus gambar sebelumnya
mGraphics.setColor(getBackground());
mGraphics.fillRect(0, 0, d.lebar, d.tinggi);
mGraphics.setColor(Warna.hitam);
// melukis bingkai ke dalam gambar
bingkai cat(mGraphics);
// melukis gambar ke layar
g.drawImage(mImage, 0, 0, null);
}
kelas MouseMotion memperluas java.awt.event.MouseMotionAdapter {
public void mouseMoved(java.awt.event.MouseEvent acara) {
Objek objek = event.getSource();
if (objek == Mata.ini)
mouseMovedInApplet(acara);
}
}
void mouseMovedInApplet(java.awt.event.MouseEvent acara) {
// ambil kabel mouse
mMouseX = acara.getX();
mMouseY = acara.getY();
mouseDipindahkan();
}
public void mouseMovedInBrowser(int x, int y, int windowWidth) {
int appletW = getSize().lebar;
// sesuaikan mouse x dan y relatif terhadap posisi applet
mMouseX = x - (lebar jendela - appletW) / 2;
mMouseY = y - mVerticalOffset;
mouseDipindahkan();
}
private void showError(Grafik g) {
g.setFont(Font baru("TimesRoman", Font.BOLD, 12));
g.drawString(mErrorMessage, 10, 20);
}
}