TabbedPaneDemo.java  
1   /*
2    * @(#)TabbedPaneDemo.java  1.10 04/07/26
3    * 
4    * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
5    * 
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    * 
9    * -Redistribution of source code must retain the above copyright notice, this
10   *  list of conditions and the following disclaimer.
11   * 
12   * -Redistribution in binary form must reproduce the above copyright notice, 
13   *  this list of conditions and the following disclaimer in the documentation
14   *  and/or other materials provided with the distribution.
15   * 
16   * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
17   * be used to endorse or promote products derived from this software without 
18   * specific prior written permission.
19   * 
20   * This software is provided "AS IS," without a warranty of any kind. ALL 
21   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
27   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
28   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
29   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
30   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31   * 
32   * You acknowledge that this software is not designed, licensed or intended
33   * for use in the design, construction, operation or maintenance of any
34   * nuclear facility.
35   */
36  
37  /*
38   * @(#)TabbedPaneDemo.java  1.10 04/07/26
39   */
40  
41  
42  import javax.swing.*;
43  import javax.swing.event.*;
44  import javax.swing.text.*;
45  import javax.swing.border.*;
46  import javax.swing.colorchooser.*;
47  import javax.swing.filechooser.*;
48  import javax.accessibility.*;
49  
50  import java.awt.*;
51  import java.awt.event.*;
52  import java.beans.*;
53  import java.util.*;
54  import java.io.*;
55  import java.applet.*;
56  import java.net.*;
57  
58  /**
59   * JTabbedPane Demo
60   *
61   * @version 1.10 07/26/04
62   * @author Jeff Dinkins
63   */
64  public class TabbedPaneDemo extends DemoModule implements ActionListener {
65      HeadSpin spin;
66  
67      JTabbedPane tabbedpane;
68  
69      ButtonGroup group;
70  
71      JRadioButton top;
72      JRadioButton bottom;
73      JRadioButton left;
74      JRadioButton right;
75  
76      /**
77       * main method allows us to run as a standalone demo.
78       */
79      public static void main(String[] args) {
80      TabbedPaneDemo demo = new TabbedPaneDemo(null);
81      demo.mainImpl();
82      }
83  
84      /**
85       * TabbedPaneDemo Constructor
86       */
87      public TabbedPaneDemo(SwingSet2 swingset) {
88      // Set the title for this demo, and an icon used to represent this
89      // demo inside the SwingSet2 app.
90      super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
91  
92      // create tab position controls
93      JPanel tabControls = new JPanel();
94      tabControls.add(new JLabel(getString("TabbedPaneDemo.label")));
95      top    = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.top")));
96      left   = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.left")));
97      bottom = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.bottom")));
98      right  = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.right")));
99      getDemoPanel().add(tabControls, BorderLayout.NORTH);
100 
101     group = new ButtonGroup();
102     group.add(top);
103     group.add(bottom);
104     group.add(left);
105     group.add(right);
106 
107     top.setSelected(true);
108 
109     top.addActionListener(this);
110     bottom.addActionListener(this);
111     left.addActionListener(this);
112     right.addActionListener(this);
113 
114     // create tab 
115     tabbedpane = new JTabbedPane();
116     getDemoPanel().add(tabbedpane, BorderLayout.CENTER);
117 
118     String name = getString("TabbedPaneDemo.laine");
119     JLabel pix = new JLabel(createImageIcon("tabbedpane/laine.jpg", name));
120     tabbedpane.add(name, pix);
121 
122     name = getString("TabbedPaneDemo.ewan");
123     pix = new JLabel(createImageIcon("tabbedpane/ewan.jpg", name));
124     tabbedpane.add(name, pix);
125 
126     name = getString("TabbedPaneDemo.hania");
127     pix = new JLabel(createImageIcon("tabbedpane/hania.jpg", name));
128     tabbedpane.add(name, pix);
129 
130     name = getString("TabbedPaneDemo.bounce");
131     spin = new HeadSpin();
132     tabbedpane.add(name, spin);
133     
134     tabbedpane.getModel().addChangeListener(
135        new ChangeListener() {
136           public void stateChanged(ChangeEvent e) {
137           SingleSelectionModel model = (SingleSelectionModel) e.getSource();
138           if(model.getSelectedIndex() == tabbedpane.getTabCount()-1) {
139               spin.go();
140           }
141           }
142        }
143     );
144     }
145 
146     public void actionPerformed(ActionEvent e) {
147     if(e.getSource() == top) {
148         tabbedpane.setTabPlacement(JTabbedPane.TOP);
149     } else if(e.getSource() == left) {
150         tabbedpane.setTabPlacement(JTabbedPane.LEFT);
151     } else if(e.getSource() == bottom) {
152         tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
153     } else if(e.getSource() == right) {
154         tabbedpane.setTabPlacement(JTabbedPane.RIGHT);
155     }
156     }
157 
158     class HeadSpin extends JComponent implements ActionListener {
159     javax.swing.Timer animator;
160     
161     ImageIcon icon[] = new ImageIcon[6];
162 
163     int tmpScale;
164 
165     final static int numImages = 6;
166 
167     double x[] = new double[numImages];
168     double y[] = new double[numImages];
169 
170     int xh[] = new int[numImages];
171     int yh[] = new int[numImages];
172 
173     double scale[] = new double[numImages];
174 
175     public HeadSpin() {
176         setBackground(Color.black);
177         icon[0] = createImageIcon("tabbedpane/ewan.gif", getString("TabbedPaneDemo.ewan"));
178         icon[1] = createImageIcon("tabbedpane/stephen.gif", getString("TabbedPaneDemo.stephen"));
179         icon[2] = createImageIcon("tabbedpane/david.gif", getString("TabbedPaneDemo.david"));
180         icon[3] = createImageIcon("tabbedpane/matthew.gif", getString("TabbedPaneDemo.matthew"));
181         icon[4] = createImageIcon("tabbedpane/blake.gif", getString("TabbedPaneDemo.blake"));
182         icon[5] = createImageIcon("tabbedpane/brooke.gif", getString("TabbedPaneDemo.brooke"));
183 
184         /*
185         for(int i = 0; i < 6; i++) {
186         x[i] = (double) rand.nextInt(500);
187         y[i] = (double) rand.nextInt(500);
188         }
189         */
190     }
191     
192     public void go() {
193         animator = new javax.swing.Timer(22 + 22 + 22, this);
194         animator.start();
195     }
196 
197     public void paint(Graphics g) {
198         g.setColor(getBackground());
199         g.fillRect(0, 0, getWidth(), getHeight());
200 
201         for(int i = 0; i < numImages; i++) {
202         if(x[i] > 3*i) {
203             nudge(i);
204             squish(g, icon[i], xh[i], yh[i], scale[i]);
205         } else {
206             x[i] += .05;
207             y[i] += .05;
208         }
209         }
210     }
211 
212     Random rand = new Random();
213 
214     public void nudge(int i) {
215         x[i] += (double) rand.nextInt(1000) / 8756;
216         y[i] += (double) rand.nextInt(1000) / 5432;
217         int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
218         scale[i] = (double) tmpScale / 10;
219         int nudgeX = (int) (((double) getWidth()/2) * .8);
220         int nudgeY = (int) (((double) getHeight()/2) * .60);
221         xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
222         yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
223     }
224 
225     public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {
226         if(isVisible()) {
227         g.drawImage(icon.getImage(), x, y,
228                 (int) (icon.getIconWidth()*scale),
229                 (int) (icon.getIconHeight()*scale),
230                 this);
231         } 
232     }
233     
234     public void actionPerformed(ActionEvent e) {
235         if(isVisible()) {
236         repaint();
237         } else {
238         animator.stop();
239         }
240     }
241     }
242 }
243 
244