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!
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();
}
}
}
}
Have you looked into using a timer to send scroll instructions to the JScrollPane at intervals? Just the first thing that comes to mind...
来源:https://stackoverflow.com/questions/15604399/simple-way-of-creating-an-animated-jscrollpane-in-java