如何移动这个眼睛小程序或让它眨眼?

时间:2016-04-05 03:17:35

标签: java applet awt repaint

请帮助我如何使这个眼睛移动或使用重绘,线程和工具运行使其闪烁。我不知道在哪里放置正确的代码才能使其正常工作。请帮帮我们!谢谢! 这是代码:

import java.awt.*;
import java.applet.*;

public class Pucca extends Applet {

public Pucca(){
setSize(700, 700); }

//paint method
public void paint(Graphics g){

Color white = new Color(255,255,255);
g.setColor(white);
g.fillOval(600, 100, 125, 125); //left white fill eye

g.setColor(Color.BLA­CK);
g.drawOval(600, 100, 125, 125); // left big black line eye

g.setColor(white);
g.fillOval(700, 100, 125, 125); //right white fill eye

g.setColor(Color.BLA­CK);
g.drawOval(700, 100, 125, 125); //right big black line eye

Color blue = new Color(0, 160, 198);
g.setColor(blue);
g.fillOval(635, 130, 51, 51); // left blue fill eye

g.setColor(Color.BLA­CK);
g.drawOval(635, 130, 50, 50); // left black small line eye

g.setColor(blue);
g.fillOval(735, 130, 51, 51); // right blue fill eye

g.setColor(Color.BLA­CK);
g.drawOval(735, 130, 50, 50); // right black small line eye

g.setColor(Color.BLA­CK);
g.fillOval(650, 145, 20, 20); // left black iris 

g.setColor(Color.BLA­CK);
g.fillOval(750, 145, 20, 20); // right black iris

}
}

1 个答案:

答案 0 :(得分:1)

谈到动画,一切都变得多变。你也有很多重复的代码(严重的是,如果你能画一只眼睛,你可以画很多)。

您需要做的第一件事就是尽可能改变眼睛的所有值。

以下内容使眼睛大小和位置变量,虹膜和瞳孔成为眼睛大小的缩放值,这使得整个过程更容易制作动画。

接下来,您需要一个更新的循环,它可以更新您想要更改的值的状态。为了简单起见,我已经设置了它,以便瞳孔有一个可变的偏移,随着时间的推移而变化。

Eyes

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

public class Pucca extends Applet {

    public Pucca() {
        setSize(700, 700);
        Thread t = new Thread(new Runnable() {
            private int xDelta = -1;
            private int yDelta = 0;
            private int blinkCount = 0;

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException ex) {
                    }

                    xOffset += xDelta;
                    double irisSize = eyeSize.width * irisScale;
                    double range = ((eyeSize.width - irisSize) / 2);
                    if (xOffset <= -range) {
                        xOffset = -(int) range;
                        xDelta *= -1;
                    } else if (xOffset >= range) {
                        xOffset = (int) range;
                        xDelta *= -1;
                    }
                    blinkCount++;
                    if (blink && blinkCount > 10) {
                        blink = false;
                        blinkCount = 0;
                    } else if (blinkCount > 25) {
                        blink = true;
                        blinkCount = 0;
                    }
                    repaint();
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }

    private boolean blink = false;

    private int xOffset, yOffset = 0;
    private Dimension eyeSize = new Dimension(125, 125);
    private Point left = new Point(20, 20);
    private Point right = new Point(left.x + 100, left.y);
    private double irisScale = 0.4;
    private double pupilScale = 0.16;

//paint method
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        paintEye(g, new Rectangle(left, eyeSize));
        paintEye(g, new Rectangle(right, eyeSize));

    }

    protected void paintEye(Graphics g, Rectangle bounds) {

        Color white = new Color(255, 255, 255);

        if (blink) {
            g.setColor(Color.YELLOW);
        } else {
            g.setColor(white);
        }
        g.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); //left white fill eye

        g.setColor(Color.BLACK);
        g.drawOval(bounds.x, bounds.y, bounds.width, bounds.height); // left big black line eye

        if (!blink) {
            Color blue = new Color(0, 160, 198);

            paintEyePartAt(g, bounds, irisScale, blue);
            paintEyePartAt(g, bounds, pupilScale, Color.BLACK);
        }
    }

    private void paintEyePartAt(Graphics g, Rectangle bounds, double delta, Color color) {

        int width = (int) (bounds.width * delta);
        int height = (int) (bounds.height * delta);

        g.setColor(color);
        g.fillOval(
                xOffset + bounds.x + ((bounds.width - width) / 2),
                yOffset + bounds.y + ((bounds.height - height) / 2),
                width, height); // left blue fill eye
        g.setColor(Color.BLACK);
        g.drawOval(
                xOffset + bounds.x + ((bounds.width - width) / 2),
                yOffset + bounds.y + ((bounds.height - height) / 2),
                width,
                height); // left blue fill eye
    }
}

这使事情变得复杂,因为绘画可能由于多种原因而发生,其中许多原因是您无法控制或将被通知,因此您应该非常小心改变值的位置和时间。 / p>

您还应该查看Java Plugin support deprecatedMoving to a Plugin-Free Web以及Why CS teachers should stop teaching Java applets

Applets只是一种死技术,鉴于使用它们所固有的复杂性,你应该把注意力集中在基于窗口的程序上。

就个人而言,我首先要看看Painting in AWT and SwingPerforming Custom Painting