如何使用NoApplet制作球生长的动画?

问题描述 投票:0回答:1

我正在尝试用java制作一个简单的动画,其中球的尺寸会增大,然后有一个子类,当我运行它时,它会使球的尺寸增大,直到到达屏幕的末尾,此时它会缩小并重复该过程。这一切都是用 Java 实现的 NoApplet。我是框架初学者,这是我认为应该如何工作的代码。

import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import javax.swing.*;
import noapplet.NoApplet;

public class Balloon extends NoApplet{
//Field declarations? Not really sure what declarations would I use. 
    protected int delay = 100;
    protected Dimension dim;
    protected int x, y;
    protected Timer timer;
    protected int offset = 10;
    public Balloon() {
    }
    public void init(){
//initialization and have parameter 
        dim = getSize();
        x = dim.width;
        y = dim.height;

    }
    protected void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
//start making the screen black and then make the animation 


    }
    public static void main (String[]args){
        new Balloon().run();

我有类似的代码,说明这样的事情应该如何工作,但我不知道如何“翻译”

package noapplet.assigment;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import javax.swing.*;
import noapplet.NoApplet;
import noapplet.example.Omok;

public class ScrollingBanner extends NoApplet {

    protected String text;
    protected Font font =  new java.awt.Font("Sans-serif", Font.BOLD, 24);
    protected Dimension dim;
    protected int x, y;
    protected int delay = 100; // millis
    protected int offset = 10;
    protected Timer timer; // animation timer

    public void init() {
        // get parameters "delay" and "text"
        String att = getParameter("delay");
        if (att != null) {
            delay = Integer.parseInt(att);
        }
        att = getParameter("text");
        text = Objects.requireNonNullElse(att, "Lets Go");

        // set the initial position of the text
        dim = getSize();
        x = dim.width;
        y = font.getSize();

        // initialize the animation timer
        timer = new Timer(delay, e -> repaint());
    }
    protected void paintComponent(Graphics g) {
        // get the font metrics to determine the length of the text
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics();
        int length = fm.stringWidth(text);

        // adjust the position of the text from the previous frame
        x -= offset;
        // if the text is completely off to the left end
        // move the position back to the right end
        //if (x < -length) { x = -dim.width; }
        if (x < -length) { x = dim.width; }

        // set the pen color and draw the background
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, dim.width, dim.height);

        // set the pen color and then draw the text
        g.setColor(Color.GREEN);
        g.drawString(text, x, y);
    }
    public void start() {
        timer.start();
    }
    public void stop() {
        timer.stop();
    }
    public static void main (String [] args){
        new ScrollingBanner().run();
    }
}


相关图片:

java frameworks applet
1个回答
0
投票

因此,基于这个示例,我可以简单地进行修改以支持圆形(而不是自定义形状)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.time.Duration;
import java.time.Instant;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class TestPane extends JPanel {

        private Path2D path;

        private Instant anchorPoint;
        private Duration playDuration = Duration.ofSeconds(2);
        private double scale = 1;

        private double lowerRange = 0.75;
        private double upperRange = 1.25;

        public TestPane() {
            super();
            setPreferredSize(new Dimension(800, 600));
            path = new Path2D.Double(new Ellipse2D.Double(-100, -100, 200, 200));

            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (anchorPoint == null) {
                        anchorPoint = Instant.now();
                    }
                    Duration playTime = Duration.between(anchorPoint, Instant.now());
                    double progress = (double) playTime.toMillis() / playDuration.toMillis();
                    if (progress >= 1) {
                        anchorPoint = null;
                        progress = 1;
                    }

                    if (progress > 0.5) {
                        progress = 1.0 - progress;
                    }

                    scale = ((upperRange - lowerRange) * progress) + lowerRange;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            AffineTransform gat = new AffineTransform();

            gat.scale(1.0, -1.0);
            gat.translate(getWidth() / 2.0, -getHeight() / 2.0);
            g2d.transform(gat);

            Shape shape = path.createTransformedShape(AffineTransform.getScaleInstance(scale, scale));

            g2d.setPaint(Color.PINK);
            g2d.fill(shape);
            g2d.setStroke(new BasicStroke(5.0f));
            g2d.setPaint(Color.BLACK);
            g2d.draw(shape);
            g2d.dispose();
        }
    }
}

Cavet - 这将从大开始并缩小,在重复之前,要扭转这种情况,只需更改

scale
计算以反转
progress
,例如...

scale = ((upperRange - lowerRange) * (1d - progress)) + lowerRange;

Cavet - 如果不知道

NoApplet
是什么,它会吓到我

© www.soinside.com 2019 - 2024. All rights reserved.