Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

swing - Simple way of creating an animated JScrollPane in Java?

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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();
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...