2013-03-24 35 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(); 
      } 
     } 
    } 
}