36744
|
1 |
/*
|
|
2 |
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package com.sun.swingset3.demos.tabbedpane;
|
|
24 |
|
|
25 |
import java.awt.*;
|
|
26 |
import java.awt.event.ActionEvent;
|
|
27 |
import java.awt.event.ActionListener;
|
|
28 |
import java.util.Random;
|
|
29 |
import javax.swing.*;
|
|
30 |
import javax.swing.event.ChangeEvent;
|
|
31 |
|
|
32 |
import com.sun.swingset3.DemoProperties;
|
|
33 |
import com.sun.swingset3.demos.ResourceManager;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* JTabbedPane Demo
|
|
37 |
*
|
|
38 |
* @version 1.11 11/17/05
|
|
39 |
* @author Jeff Dinkins
|
|
40 |
*/
|
|
41 |
@DemoProperties(
|
|
42 |
value = "JTabbedPane Demo",
|
|
43 |
category = "Containers",
|
|
44 |
description = "Demonstrates JTabbedPane, a container which allows tabbed navigation of components",
|
|
45 |
sourceFiles = {
|
|
46 |
"com/sun/swingset3/demos/tabbedpane/TabbedPaneDemo.java",
|
|
47 |
"com/sun/swingset3/demos/ResourceManager.java",
|
|
48 |
"com/sun/swingset3/demos/tabbedpane/resources/TabbedPaneDemo.properties",
|
|
49 |
"com/sun/swingset3/demos/tabbedpane/resources/images/blake.gif",
|
|
50 |
"com/sun/swingset3/demos/tabbedpane/resources/images/brooke.gif",
|
|
51 |
"com/sun/swingset3/demos/tabbedpane/resources/images/camille.jpg",
|
|
52 |
"com/sun/swingset3/demos/tabbedpane/resources/images/david.gif",
|
|
53 |
"com/sun/swingset3/demos/tabbedpane/resources/images/ewan.gif",
|
|
54 |
"com/sun/swingset3/demos/tabbedpane/resources/images/ewan.jpg",
|
|
55 |
"com/sun/swingset3/demos/tabbedpane/resources/images/miranda.jpg",
|
|
56 |
"com/sun/swingset3/demos/tabbedpane/resources/images/matthew.gif",
|
|
57 |
"com/sun/swingset3/demos/tabbedpane/resources/images/stephen.gif",
|
|
58 |
"com/sun/swingset3/demos/tabbedpane/resources/images/TabbedPaneDemo.gif"
|
|
59 |
}
|
|
60 |
)
|
|
61 |
public class TabbedPaneDemo extends JPanel implements ActionListener {
|
|
62 |
|
|
63 |
private static final ResourceManager resourceManager = new ResourceManager(TabbedPaneDemo.class);
|
|
64 |
public static final String BOUNCE = resourceManager.getString("TabbedPaneDemo.bounce");
|
|
65 |
public static final String EWAN = resourceManager.getString("TabbedPaneDemo.ewan");
|
|
66 |
public static final String MIRANDA = resourceManager.getString("TabbedPaneDemo.miranda");
|
|
67 |
public static final String CAMILLE = resourceManager.getString("TabbedPaneDemo.camille");
|
|
68 |
public static final String TAB_PLACEMENT = resourceManager.getString("TabbedPaneDemo.label");
|
|
69 |
public static final String RIGHT = resourceManager.getString("TabbedPaneDemo.right");
|
|
70 |
public static final String BOTTOM = resourceManager.getString("TabbedPaneDemo.bottom");
|
|
71 |
public static final String LEFT = resourceManager.getString("TabbedPaneDemo.left");
|
|
72 |
public static final String TOP = resourceManager.getString("TabbedPaneDemo.top");
|
|
73 |
public static final String DEMO_TITLE = TabbedPaneDemo.class.getAnnotation(DemoProperties.class).value();
|
|
74 |
|
|
75 |
private final HeadSpin spin;
|
|
76 |
|
|
77 |
private final JTabbedPane tabbedpane;
|
|
78 |
|
|
79 |
private final ButtonGroup group;
|
|
80 |
|
|
81 |
private final JRadioButton top;
|
|
82 |
private final JRadioButton bottom;
|
|
83 |
private final JRadioButton left;
|
|
84 |
private final JRadioButton right;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* main method allows us to run as a standalone demo.
|
|
88 |
*
|
|
89 |
* @param args
|
|
90 |
*/
|
|
91 |
public static void main(String[] args) {
|
|
92 |
JFrame frame = new JFrame(DEMO_TITLE);
|
|
93 |
|
|
94 |
frame.getContentPane().add(new TabbedPaneDemo());
|
|
95 |
frame.setPreferredSize(new Dimension(800, 600));
|
|
96 |
frame.pack();
|
|
97 |
frame.setLocationRelativeTo(null);
|
|
98 |
frame.setVisible(true);
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* TabbedPaneDemo Constructor
|
|
103 |
*/
|
|
104 |
public TabbedPaneDemo() {
|
|
105 |
setLayout(new BorderLayout());
|
|
106 |
|
|
107 |
// create tab position controls
|
|
108 |
JPanel tabControls = new JPanel();
|
|
109 |
tabControls.add(new JLabel(TAB_PLACEMENT));
|
|
110 |
top = (JRadioButton) tabControls.add(new JRadioButton(TOP));
|
|
111 |
left = (JRadioButton) tabControls.add(new JRadioButton(LEFT));
|
|
112 |
bottom = (JRadioButton) tabControls.add(new JRadioButton(BOTTOM));
|
|
113 |
right = (JRadioButton) tabControls.add(new JRadioButton(RIGHT));
|
|
114 |
add(tabControls, BorderLayout.NORTH);
|
|
115 |
|
|
116 |
group = new ButtonGroup();
|
|
117 |
group.add(top);
|
|
118 |
group.add(bottom);
|
|
119 |
group.add(left);
|
|
120 |
group.add(right);
|
|
121 |
|
|
122 |
top.setSelected(true);
|
|
123 |
|
|
124 |
top.addActionListener(this);
|
|
125 |
bottom.addActionListener(this);
|
|
126 |
left.addActionListener(this);
|
|
127 |
right.addActionListener(this);
|
|
128 |
|
|
129 |
// create tab
|
|
130 |
tabbedpane = new JTabbedPane();
|
|
131 |
add(tabbedpane, BorderLayout.CENTER);
|
|
132 |
|
|
133 |
String name = CAMILLE;
|
|
134 |
JLabel pix = new JLabel(resourceManager.createImageIcon("camille.jpg", name));
|
|
135 |
tabbedpane.add(name, pix);
|
|
136 |
|
|
137 |
name = MIRANDA;
|
|
138 |
pix = new JLabel(resourceManager.createImageIcon("miranda.jpg", name));
|
|
139 |
pix.setToolTipText(resourceManager.getString("TabbedPaneDemo.miranda.tooltip"));
|
|
140 |
tabbedpane.add(name, pix);
|
|
141 |
|
|
142 |
name = EWAN;
|
|
143 |
pix = new JLabel(resourceManager.createImageIcon("ewan.jpg", name));
|
|
144 |
tabbedpane.add(name, pix);
|
|
145 |
|
|
146 |
name = BOUNCE;
|
|
147 |
spin = new HeadSpin();
|
|
148 |
tabbedpane.add(name, spin);
|
|
149 |
|
|
150 |
tabbedpane.getModel().addChangeListener((ChangeEvent e) -> {
|
|
151 |
SingleSelectionModel model = (SingleSelectionModel) e.getSource();
|
|
152 |
if (model.getSelectedIndex() == tabbedpane.getTabCount() - 1) {
|
|
153 |
spin.go();
|
|
154 |
}
|
|
155 |
});
|
|
156 |
}
|
|
157 |
|
|
158 |
@Override
|
|
159 |
public void actionPerformed(ActionEvent e) {
|
|
160 |
if (e.getSource() == top) {
|
|
161 |
tabbedpane.setTabPlacement(JTabbedPane.TOP);
|
|
162 |
} else if (e.getSource() == left) {
|
|
163 |
tabbedpane.setTabPlacement(JTabbedPane.LEFT);
|
|
164 |
} else if (e.getSource() == bottom) {
|
|
165 |
tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
|
|
166 |
} else if (e.getSource() == right) {
|
|
167 |
tabbedpane.setTabPlacement(JTabbedPane.RIGHT);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
private class HeadSpin extends JComponent implements ActionListener {
|
|
172 |
|
|
173 |
private javax.swing.Timer animator;
|
|
174 |
|
|
175 |
private final ImageIcon[] icon = new ImageIcon[6];
|
|
176 |
|
|
177 |
private final static int numImages = 6;
|
|
178 |
|
|
179 |
private final double[] x = new double[numImages];
|
|
180 |
private final double[] y = new double[numImages];
|
|
181 |
|
|
182 |
private final int[] xh = new int[numImages];
|
|
183 |
private final int[] yh = new int[numImages];
|
|
184 |
|
|
185 |
private final double[] scale = new double[numImages];
|
|
186 |
|
|
187 |
private final Random rand = new Random();
|
|
188 |
|
|
189 |
public HeadSpin() {
|
|
190 |
setBackground(Color.black);
|
|
191 |
icon[0] = resourceManager.createImageIcon("ewan.gif", resourceManager.getString("TabbedPaneDemo.ewan"));
|
|
192 |
icon[1] = resourceManager.createImageIcon("stephen.gif", resourceManager.getString("TabbedPaneDemo.stephen"));
|
|
193 |
icon[2] = resourceManager.createImageIcon("david.gif", resourceManager.getString("TabbedPaneDemo.david"));
|
|
194 |
icon[3] = resourceManager.createImageIcon("matthew.gif", resourceManager.getString("TabbedPaneDemo.matthew"));
|
|
195 |
icon[4] = resourceManager.createImageIcon("blake.gif", resourceManager.getString("TabbedPaneDemo.blake"));
|
|
196 |
icon[5] = resourceManager.createImageIcon("brooke.gif", resourceManager.getString("TabbedPaneDemo.brooke"));
|
|
197 |
|
|
198 |
/*
|
|
199 |
for(int i = 0; i < 6; i++) {
|
|
200 |
x[i] = (double) rand.nextInt(500);
|
|
201 |
y[i] = (double) rand.nextInt(500);
|
|
202 |
}
|
|
203 |
*/
|
|
204 |
}
|
|
205 |
|
|
206 |
public void go() {
|
|
207 |
animator = new javax.swing.Timer(22 + 22 + 22, this);
|
|
208 |
animator.start();
|
|
209 |
}
|
|
210 |
|
|
211 |
@Override
|
|
212 |
public void paint(Graphics g) {
|
|
213 |
g.setColor(getBackground());
|
|
214 |
g.fillRect(0, 0, getWidth(), getHeight());
|
|
215 |
|
|
216 |
for (int i = 0; i < numImages; i++) {
|
|
217 |
if (x[i] > 3 * i) {
|
|
218 |
nudge(i);
|
|
219 |
squish(g, icon[i], xh[i], yh[i], scale[i]);
|
|
220 |
} else {
|
|
221 |
x[i] += .05;
|
|
222 |
y[i] += .05;
|
|
223 |
}
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
public void nudge(int i) {
|
|
228 |
x[i] += (double) rand.nextInt(1000) / 8756;
|
|
229 |
y[i] += (double) rand.nextInt(1000) / 5432;
|
|
230 |
int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
|
|
231 |
scale[i] = (double) tmpScale / 10;
|
|
232 |
int nudgeX = (int) (((double) getWidth() / 2) * .8);
|
|
233 |
int nudgeY = (int) (((double) getHeight() / 2) * .60);
|
|
234 |
xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
|
|
235 |
yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
|
|
236 |
}
|
|
237 |
|
|
238 |
public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {
|
|
239 |
if (isVisible()) {
|
|
240 |
g.drawImage(icon.getImage(), x, y,
|
|
241 |
(int) (icon.getIconWidth() * scale),
|
|
242 |
(int) (icon.getIconHeight() * scale),
|
|
243 |
this);
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
@Override
|
|
248 |
public void actionPerformed(ActionEvent e) {
|
|
249 |
if (isVisible()) {
|
|
250 |
repaint();
|
|
251 |
} else {
|
|
252 |
animator.stop();
|
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|