繁体   English   中英

如何移动该小程序或使其闪烁?

[英]How to move this eye applet or to make it blink?

请帮助我如何使用重新绘制,线程和可运行的工具使眼睛动起来或使其眨眼。 我不知道在哪里放置正确的代码以使其正常工作。 请帮助我! 谢谢! 这是代码:

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

}
}

说到动画,一切都会变。 您还会有很多重复的代码(严重的是,如果您可以画一只眼睛,则可以画很多)。

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

跟随使眼睛大小和位置可变,虹膜和瞳孔成为眼睛大小的缩放值,这使整个过程更易于动画制作。

接下来,您需要一个更新的循环,该循环可以更新您要更改的值的状态。 为简单起见,我对其进行了设置,以使瞳孔具有可变的偏移量,该偏移量会随着时间而变化。

眼睛

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
    }
}

这使事情变得复杂,因为绘画可能会由于多种原因发生,其中许多原因您无法控制或将要通知,因此您在更改值的位置和时间时应格外小心。

您还应该查看不赞成使用的Java插件支持转向无插件Web的知识,以及CS老师为什么应该停止教授Java applet的原因

Applet只是一项过时的技术,鉴于使用它们所固有的复杂性,您应该将注意力集中在基于窗口的程序上。

我个人首先要看一下AWT中的绘画以及Swing执行自定义绘画

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM