2013-03-24 64 views
2

我目前有一个JScrollPane,它包含基本上是项目列表的内容。此JScrollPane将显示在信息屏幕上。在Java中创建动画JScrollPane的简单方法?

我正在寻找的方法是让它自动滚动到列表的最底部,然后回到顶部。我认识到使用JScrollPane可能无法实现,所以任何替代方案的建议也会很棒!

+0

安全将是与移动JScrollBar中,也许必须改变scroll_increment这份工作,也不知道什么项目列表,那里JComponents没有问题,实现滚动,但与JPanel的,则为JComponents滚动不能自然... – mKorbel 2013-03-25 07:45:01

回答

0

您是否使用计时器向间隔发送滚动指令到JScrollPane?只是想到的第一件事......

1

通常我会使用TimingFramework或者您可以使用类似TridentUnviversal Tween Engine作为任何动画的基础。主要原因是,除了为你做大部分工作之外,他们还提供可变速度,这将使动画看起来更自然。

但是,您可以使用javax.swing.Timer来实现基本概念。

该示例将允许您滚动到图像的底部并再次返回。

动画需要5秒钟(由runningTiming变量提供),使其变得可变(图像越大,运动越快,图像越小,速度越慢)。

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(); 
      } 
     } 
    } 
} 
相关问题