返回顶部

收藏

动态切换标签

更多
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. 糖果 发表 2015-08-03 21:33:22 逆向基础(十三) JAVA (四)
  2. 博主 发表 2013-03-27 17:01:00 Java基础知识之Annotation
  3. Terry Zhang 发表 2014-04-20 15:36:04 [正则] 预定义字符类型
  4. Terry Zhang 发表 2014-05-03 06:47:05 [集合框架] Deque 接口
  5. root 发表 2015-04-26 02:40:02 Java 基础:认识&理解关键字 native 实战篇
  6. 糖果 发表 2015-08-03 20:25:16 逆向基础(十三) JAVA (三)
  7. Terry Zhang 发表 2014-05-09 15:21:58 Java 中的访问修饰符
  8. ticmy 发表 2012-11-08 12:42:26 对象实例化的顺序
  9. 博主 发表 2017-04-04 08:28:15 Java注解的基础与高级应用
  10. ImportNew读者投稿 发表 2013-03-14 09:48:51 Java到底是传引用还是传值?
  11. Terry Zhang 发表 2014-04-22 03:30:42 [正则] 数量词
  12. Terry Zhang 发表 2014-05-06 04:18:19 [集合框架] Map 接口

发表评论