KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > TabbedPaneDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)TabbedPaneDemo.java 1.8 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57 /**
58  * JTabbedPane Demo
59  *
60  * @version 1.8 01/23/03
61  * @author Jeff Dinkins
62  */

63 public class TabbedPaneDemo extends DemoModule implements ActionListener {
64     HeadSpin spin;
65
66     JTabbedPane tabbedpane;
67
68     ButtonGroup group;
69
70     JRadioButton top;
71     JRadioButton bottom;
72     JRadioButton left;
73     JRadioButton right;
74
75     /**
76      * main method allows us to run as a standalone demo.
77      */

78     public static void main(String JavaDoc[] args) {
79     TabbedPaneDemo demo = new TabbedPaneDemo(null);
80     demo.mainImpl();
81     }
82
83     /**
84      * TabbedPaneDemo Constructor
85      */

86     public TabbedPaneDemo(SwingSet2 swingset) {
87     // Set the title for this demo, and an icon used to represent this
88
// demo inside the SwingSet2 app.
89
super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
90
91     // create tab position controls
92
JPanel tabControls = new JPanel();
93     tabControls.add(new JLabel(getString("TabbedPaneDemo.label")));
94     top = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.top")));
95     left = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.left")));
96     bottom = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.bottom")));
97     right = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.right")));
98     getDemoPanel().add(tabControls, BorderLayout.NORTH);
99
100     group = new ButtonGroup();
101     group.add(top);
102     group.add(bottom);
103     group.add(left);
104     group.add(right);
105
106     top.setSelected(true);
107
108     top.addActionListener(this);
109     bottom.addActionListener(this);
110     left.addActionListener(this);
111     right.addActionListener(this);
112
113     // create tab
114
tabbedpane = new JTabbedPane();
115     getDemoPanel().add(tabbedpane, BorderLayout.CENTER);
116
117     String JavaDoc name = getString("TabbedPaneDemo.laine");
118     JLabel pix = new JLabel(createImageIcon("tabbedpane/laine.jpg", name));
119     tabbedpane.add(name, pix);
120
121     name = getString("TabbedPaneDemo.ewan");
122     pix = new JLabel(createImageIcon("tabbedpane/ewan.jpg", name));
123     tabbedpane.add(name, pix);
124
125     name = getString("TabbedPaneDemo.hania");
126     pix = new JLabel(createImageIcon("tabbedpane/hania.jpg", name));
127     tabbedpane.add(name, pix);
128
129     name = getString("TabbedPaneDemo.bounce");
130     spin = new HeadSpin();
131     tabbedpane.add(name, spin);
132     
133     tabbedpane.addChangeListener(
134        new ChangeListener() {
135           public void stateChanged(ChangeEvent e) {
136           //SingleSelectionModel model = (SingleSelectionModel) e.getSource();
137
if(tabbedpane.getSelectedIndex() == tabbedpane.getTabCount()-1) {
138               spin.go();
139           }
140           }
141        }
142     );
143     }
144
145     public void actionPerformed(ActionEvent e) {
146     if(e.getSource() == top) {
147         tabbedpane.setTabPlacement(JTabbedPane.TOP);
148     } else if(e.getSource() == left) {
149         tabbedpane.setTabPlacement(JTabbedPane.LEFT);
150     } else if(e.getSource() == bottom) {
151         tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
152     } else if(e.getSource() == right) {
153         tabbedpane.setTabPlacement(JTabbedPane.RIGHT);
154     }
155     }
156
157     class HeadSpin extends JComponent implements ActionListener {
158     swingwtx.swing.Timer animator;
159     
160     ImageIcon icon[] = new ImageIcon[6];
161
162     int tmpScale;
163
164     final static int numImages = 6;
165
166     double x[] = new double[numImages];
167     double y[] = new double[numImages];
168
169     int xh[] = new int[numImages];
170     int yh[] = new int[numImages];
171
172     double scale[] = new double[numImages];
173
174     public HeadSpin() {
175         setBackground(Color.black);
176         icon[0] = createImageIcon("tabbedpane/ewan.gif", getString("TabbedPaneDemo.ewan"));
177         icon[1] = createImageIcon("tabbedpane/stephen.gif", getString("TabbedPaneDemo.stephen"));
178         icon[2] = createImageIcon("tabbedpane/david.gif", getString("TabbedPaneDemo.david"));
179         icon[3] = createImageIcon("tabbedpane/matthew.gif", getString("TabbedPaneDemo.matthew"));
180         icon[4] = createImageIcon("tabbedpane/blake.gif", getString("TabbedPaneDemo.blake"));
181         icon[5] = createImageIcon("tabbedpane/brooke.gif", getString("TabbedPaneDemo.brooke"));
182
183             setDoubleBuffered(true);
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 swingwtx.swing.Timer(22 + 22 + 22, this);
194         animator.start();
195     }
196
197     public void paint(Graphics g) {
198         g.setColor(Color.black);
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
Popular Tags