返回顶部

收藏

动态切换标签

更多
public class TransitionTabbedPane extends JTabbedPane 
        implements ChangeListener, Runnable {
        protected int animation_length = 20;
public TransitionTabbedPane() {
            super();
            this.addChangeListener(this);

        }
        public int getAnimationLength() {
            return this.animation_length;
        }

        public void setAnimationLength(int length) {
            this.animation_length = length;
        }

    // threading code
    public void stateChanged(ChangeEvent evt) {
        new Thread(this).start();
    }

    protected int step;
    protected BufferedImage buf = null;
    protected int previous_tab = -1;

    public void run() {
        step = 0;

    // save the previous tab
    if(previous_tab != -1) {
        Component comp = this.getComponentAt(previous_tab);
        buf = new BufferedImage(comp.getWidth(),

            comp.getHeight(),
            BufferedImage.TYPE_4BYTE_ABGR);
        comp.paint(buf.getGraphics());
    }

    for(int i=0; i<animation_length; i++) {
    step = i;
    repaint();
    try {

        Thread.currentThread().sleep(100);
    } catch (Exception ex) {
        p("ex: " + ex);
    }
}

    step = -1;
    previous_tab = this.getSelectedIndex();
    repaint();

    public void paintChildren(Graphics g) {
        super.paintChildren(g);

        if(step != -1) {
            Rectangle size = this.getComponentAt(0).getBounds();
            Graphics2D g2 = (Graphics2D)g;
            paintTransition(g2, step, size, buf);
        }
    }

    public void paintTransition(Graphics2D g2, int step,
        Rectangle size, Image prev) {
    }

}
    public class InOutPane extends TransitionTabbedPane {
        public void paintTransition(Graphics2D g2, int state,
            Rectangle size, Image prev) {

        int length = getAnimationLength();
        int half = length/2;
        double scale = size.getHeight()/length;
        int offset = 0;
        // calculate the fade out part
        if(state >= 0 && state < half) { 
// draw the saved version of the old tab component 
        if(prev != null) {
            g2.drawImage(prev,(int)size.getX(),(int)size.getY(),null);
        } 
        offset = (int)((10-state)*scale);
    }
    // calculate the fade in part
    if(state >= half && state < length) {
        g2.setColor(Color.white);
        offset = (int)((state-10)*scale);

    }

    // do the drawing
    g2.setColor(Color.white);
    Rectangle area = new Rectangle((int)(size.getX()+offset),

        (int)(size.getY()+offset),
        (int)(size.getWidth()-offset*2),
        (int)(size.getHeight()-offset*2));

    g2.fill(area); 
    } 
}

    public class TabFadeTest {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Fade Tabs");
   JTabbedPane tab = new InOutPane();
            tab.addTab("t1",new JButton("Test Button 1"));
            tab.addTab("t2",new JButton("Test Button 2"));

            frame.getContentPane().add(tab);
            frame.pack();
            frame.show();

            }
        }

    public class VenetianPane extends TransitionTabbedPane { 
        public void paintTransition(Graphics2D g2, int step, 
                Rectangle size, Image prev) {
            int length = getAnimationLength();
            int half = length/2;

            // create a blind
            Rectangle blind = new Rectangle();

            // calculate the fade out part
            if(step >= 0 && step < half) {
                // draw the saved version of the old tab component
                if(prev != null) {

                g2.drawImage(prev,(int)size.getX(),(int)size.getY(),null); 
                } 
                // calculate the growing blind 
                blind = new Rectangle(
                (int)size.getX(),
                (int)size.getY(),
                step,
                (int)size.getHeight());

            }
            // calculate the fade in part
            if(step >= half && step < length) {
                // calculate the shrinking blind
                blind = new Rectangle(

                (int)size.getX(),
                (int)size.getY(),
                length-step,
                (int)size.getHeight());

                blind.translate(step-half,0);
            }

            // draw the blinds
            for(int i=0; i<size.getWidth()/half; i++) {
                g2.setColor(Color.white);
                g2.fill(blind);
                blind.translate(half,0);

                }
            } 
        }

}
//该片段来自于http://outofmemory.cn

标签:java,基础

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Terry Zhang 发表 2014-04-27 08:18:40 [集合框架] Java 集合框架简介
  2. Terry Zhang 发表 2014-05-02 08:58:00 [集合框架] Set 接口
  3. 博主 发表 2013-08-26 02:35:24 JAVA操作Hbase基础例子
  4. 郑 旭东 发表 2014-01-16 16:46:51 JAVA多线程和并发基础面试问答
  5. Terry Zhang 发表 2014-04-17 15:27:06 [正则] 字符类型
  6. Terry Zhang 发表 2014-04-27 10:43:29 [集合框架] 接口
  7. 逍遥冲 发表 2013-12-05 05:53:28 [引]Java NIO —- IO复用
  8. Terry Zhang 发表 2014-04-18 05:08:52 [NIO.2] 编写一个文件拷贝应用
  9. Terry Zhang 发表 2014-04-24 08:14:18 [正则] 边界匹配器
  10. Terry Zhang 发表 2014-05-03 06:33:32 [集合框架] List 接口
  11. Terry Zhang 发表 2014-05-13 06:05:01 [集合框架] SortedSet 接口
  12. 逍遥冲 发表 2013-12-30 14:13:10 [引]编译器自举和移植

发表评论