Sunday 3 February 2013


Moving Eyes:

import java.awt.*;
import java.applet.*;
/*
<applet code="MovingFace" width=300 height=50>
</applet>
 */

public class MovingFace extends Applet implements Runnable {

    Thread t = null;
    int state, i = 0;
    int X = 0, Y = 0;
    int UserX = 102, UserY = 102;
    boolean stopFlag;
// Set colors and initialize thread.
    public void init() {
        setBackground(Color.gray);
        setForeground(Color.red);
    }
// Start thread
    public void start() {
        X = Integer.parseInt(getParameter("Xcord"));
        Y = Integer.parseInt(getParameter("Ycord"));
        t = new Thread(this, "eyeThread");
        stopFlag = false;
        t.start();
    }
// Entry point for the thread that runs the banner.
    public void run() {
        for (;;) {
            try {
                for (i = 0; i < 20; i += 2) {
                    repaint();
                    Thread.sleep(200);
                }
                for (; i > 0; i -= 2) {
                    repaint();
                    Thread.sleep(200);
                }

                if (stopFlag) {
                    break;
                }
            } catch (InterruptedException e) {
            }
        }
    }
// Pause the banner.
    public void stop() {
        stopFlag = false;
        t = null;
    }
// Display the banner.
    public void paint(Graphics g) {

        g.setColor(Color.orange);
        g.fillOval(X + 0, Y + 0, 100, 100); //face

        g.setColor(Color.black);
        g.fillOval(X + 15 + i, Y + 30, 10, 10); //moving eyes
        g.fillOval(X + 55 + i, Y + 30, 10, 10); //moving eyes

        g.drawOval(X + 15, Y + 20, 30, 20); //eye left
        g.drawOval(X + 55, Y + 20, 30, 20); //eye right

        g.setColor(Color.blue);
        int xpoints[] = {X + 40, X + 60, X + 50}; // for nose
        int ypoints[] = {Y + 65, Y + 65, Y + 40};
        int num = 3;
        g.fillPolygon(xpoints, ypoints, num); // end nose

        g.setColor(Color.red);
        g.fillOval(X + 32, Y + 75, 35, 8); //mouth

        int xpoints1[] = {1, UserX - 1, UserX - 1, 1};
        int ypoints1[] = {1, 1, UserY - 1, UserY - 1};
        int num1 = 4;
        g.drawPolygon(xpoints1, ypoints1, num1);

        int xpoints2[] = {2, UserX - 2, UserX - 2, 2};
        int ypoints2[] = {2, 2, UserY - 2, UserY - 2};
        int num2 = 4;
        g.drawPolygon(xpoints2, ypoints2, num2);
    }
}

No comments:

Post a Comment