创建Java中的动画JScrollPane的简单方法是什么? [英] Simple way of creating an animated JScrollPane in Java?

查看:166
本文介绍了创建Java中的动画JScrollPane的简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有保存的是基本项目清单JScrollPane中。此JScrollPane是一个信息屏幕上显示的

I currently have a JScrollPane that holds what is basically a list of items. This JScrollPane is to be displayed on an information screen.

我正在寻找的是使其自动滚动到列表的底部,在给定的时间间隔,然后回到顶部的某种方式。我承认这可能不是使用JScrollPane中实现的,所以对于任何替代品也建议将是巨大的!

What I'm looking for is some way of making it automatically scroll to the bottom of the list at a given interval, then back to the top. I recognise this is probably not achievable using a JScrollPane, so any suggestions for alternatives would also be great!

推荐答案

通常我会使用 TimingFramework 或者你可以使用像三叉戟或的 Unviversal吐温引擎的作为任何动画基地。最主要的原因是,除了做最适合你的工作,他们也提供变速,这将使动画看起来更加自然。

Normally I would use the TimingFramework or you could use something like Trident or the Unviversal Tween Engine as a bases for any animation. The main reason is, apart from doing most of the work for you, they also provide variable speed, which will make the animation look more natural.

但是你可以使用实现了基本概念的 javax.swing.Timer中的

But you can achieve the basic concept using a javax.swing.Timer.

这个例子将允许您滚动到图像的底部,然后再返回。

This example will allow you to scroll to the bottom of an image and back again.

动画将需要5秒(由 runningTiming 变量提供),使其成为变量(较大的图像,更快的运动,较小的图像越慢)。

The animation will take 5 seconds (as supplied by the runningTiming variable), allowing it to be variable (the larger the image, the faster the movement, the smaller the image, the slower).

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AutoScroller {

    public static void main(String[] args) {
        new AutoScroller();
    }
    private long startTime = -1;
    private int range = 0;
    private int runningTime = 5000;
    private int direction = 1;

    public AutoScroller() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JScrollPane scrollPane = new JScrollPane(new TestPane());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(scrollPane);
//                frame.pack();
                frame.setSize(scrollPane.getPreferredSize().width, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (startTime < 0) {
                            startTime = System.currentTimeMillis();
                            range = scrollPane.getViewport().getView().getPreferredSize().height - scrollPane.getHeight();
                        }
                        long duration = System.currentTimeMillis() - startTime;
                        float progress = 1f;
                        if (duration >= runningTime) {
                            startTime = -1;
                            direction *= -1;
                            // Make the progress equal the maximum range for the new direction
                            // This will prevent it from "bouncing"
                            if (direction < 0) {
                                progress = 1f;
                            } else {
                                progress = 0f;
                            }
                        } else {
                            progress = (float) duration / (float) runningTime;
                            if (direction < 0) {
                                progress = 1f - progress;
                            }
                        }

                        int yPos = (int) (range * progress);

                        scrollPane.getViewport().setViewPosition(new Point(0, yPos));

                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();

            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage image;

        public TestPane() {
            try {
                image = ImageIO.read(new File("Path/to/your/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? new Dimension(200, 200) : new Dimension(image.getWidth(), image.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - image.getWidth()) / 2;
                int y = (getHeight() - image.getHeight()) / 2;
                g2d.drawImage(image, x, y, this);
                g2d.dispose();
            }
        }
    }
}

这篇关于创建Java中的动画JScrollPane的简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆