1
36
37
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
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
79 public static void main(String[] args) {
80 TabbedPaneDemo demo = new TabbedPaneDemo(null);
81 demo.mainImpl();
82 }
83
84
87 public TabbedPaneDemo(SwingSet2 swingset) {
88 super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
91
92 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 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
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