1
35
36
39
40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.border.*;
45 import javax.swing.colorchooser.*;
46 import javax.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import java.awt.*;
50 import java.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57
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
78 public static void main(String[] args) {
79 TabbedPaneDemo demo = new TabbedPaneDemo(null);
80 demo.mainImpl();
81 }
82
83
86 public TabbedPaneDemo(SwingSet2 swingset) {
87 super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
90
91 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 tabbedpane = new JTabbedPane();
115 getDemoPanel().add(tabbedpane, BorderLayout.CENTER);
116
117 String 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.getModel().addChangeListener(
134 new ChangeListener() {
135 public void stateChanged(ChangeEvent e) {
136 SingleSelectionModel model = (SingleSelectionModel) e.getSource();
137 if(model.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 javax.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
189 }
190
191 public void go() {
192 animator = new javax.swing.Timer(22 + 22 + 22, this);
193 animator.start();
194 }
195
196 public void paint(Graphics g) {
197 g.setColor(getBackground());
198 g.fillRect(0, 0, getWidth(), getHeight());
199
200 for(int i = 0; i < numImages; i++) {
201 if(x[i] > 3*i) {
202 nudge(i);
203 squish(g, icon[i], xh[i], yh[i], scale[i]);
204 } else {
205 x[i] += .05;
206 y[i] += .05;
207 }
208 }
209 }
210
211 Random rand = new Random();
212
213 public void nudge(int i) {
214 x[i] += (double) rand.nextInt(1000) / 8756;
215 y[i] += (double) rand.nextInt(1000) / 5432;
216 int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
217 scale[i] = (double) tmpScale / 10;
218 int nudgeX = (int) (((double) getWidth()/2) * .8);
219 int nudgeY = (int) (((double) getHeight()/2) * .60);
220 xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
221 yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
222 }
223
224 public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {
225 if(isVisible()) {
226 g.drawImage(icon.getImage(), x, y,
227 (int) (icon.getIconWidth()*scale),
228 (int) (icon.getIconHeight()*scale),
229 this);
230 }
231 }
232
233 public void actionPerformed(ActionEvent e) {
234 if(isVisible()) {
235 repaint();
236 } else {
237 animator.stop();
238 }
239 }
240 }
241 }
242
243