Path: blob/master/src/demo/share/jfc/SwingSet2/TabbedPaneDemo.java
41149 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* JTabbedPane Demo51*52* @author Jeff Dinkins53*/54public class TabbedPaneDemo extends DemoModule implements ActionListener {55HeadSpin spin;5657JTabbedPane tabbedpane;5859ButtonGroup group;6061JRadioButton top;62JRadioButton bottom;63JRadioButton left;64JRadioButton right;6566/**67* main method allows us to run as a standalone demo.68*/69public static void main(String[] args) {70TabbedPaneDemo demo = new TabbedPaneDemo(null);71demo.mainImpl();72}7374/**75* TabbedPaneDemo Constructor76*/77public TabbedPaneDemo(SwingSet2 swingset) {78// Set the title for this demo, and an icon used to represent this79// demo inside the SwingSet2 app.80super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");8182// create tab position controls83JPanel tabControls = new JPanel();84tabControls.add(new JLabel(getString("TabbedPaneDemo.label")));85top = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.top")));86left = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.left")));87bottom = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.bottom")));88right = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.right")));89getDemoPanel().add(tabControls, BorderLayout.NORTH);9091group = new ButtonGroup();92group.add(top);93group.add(bottom);94group.add(left);95group.add(right);9697top.setSelected(true);9899top.addActionListener(this);100bottom.addActionListener(this);101left.addActionListener(this);102right.addActionListener(this);103104// create tab105tabbedpane = new JTabbedPane();106getDemoPanel().add(tabbedpane, BorderLayout.CENTER);107108String name = getString("TabbedPaneDemo.laine");109JLabel pix = new JLabel(createImageIcon("tabbedpane/laine.jpg", name));110tabbedpane.add(name, pix);111112name = getString("TabbedPaneDemo.ewan");113pix = new JLabel(createImageIcon("tabbedpane/ewan.jpg", name));114tabbedpane.add(name, pix);115116name = getString("TabbedPaneDemo.hania");117pix = new JLabel(createImageIcon("tabbedpane/hania.jpg", name));118tabbedpane.add(name, pix);119120name = getString("TabbedPaneDemo.bounce");121spin = new HeadSpin();122tabbedpane.add(name, spin);123124tabbedpane.getModel().addChangeListener(125new ChangeListener() {126public void stateChanged(ChangeEvent e) {127SingleSelectionModel model = (SingleSelectionModel) e.getSource();128if(model.getSelectedIndex() == tabbedpane.getTabCount()-1) {129spin.go();130}131}132}133);134}135136public void actionPerformed(ActionEvent e) {137if(e.getSource() == top) {138tabbedpane.setTabPlacement(JTabbedPane.TOP);139} else if(e.getSource() == left) {140tabbedpane.setTabPlacement(JTabbedPane.LEFT);141} else if(e.getSource() == bottom) {142tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);143} else if(e.getSource() == right) {144tabbedpane.setTabPlacement(JTabbedPane.RIGHT);145}146}147148class HeadSpin extends JComponent implements ActionListener {149javax.swing.Timer animator;150151ImageIcon[] icon = new ImageIcon[6];152153int tmpScale;154155static final int numImages = 6;156157double[] x = new double[numImages];158double[] y = new double[numImages];159160int[] xh = new int[numImages];161int[] yh = new int[numImages];162163double[] scale = new double[numImages];164165public HeadSpin() {166setBackground(Color.black);167icon[0] = createImageIcon("tabbedpane/ewan.gif", getString("TabbedPaneDemo.ewan"));168icon[1] = createImageIcon("tabbedpane/stephen.gif", getString("TabbedPaneDemo.stephen"));169icon[2] = createImageIcon("tabbedpane/david.gif", getString("TabbedPaneDemo.david"));170icon[3] = createImageIcon("tabbedpane/matthew.gif", getString("TabbedPaneDemo.matthew"));171icon[4] = createImageIcon("tabbedpane/blake.gif", getString("TabbedPaneDemo.blake"));172icon[5] = createImageIcon("tabbedpane/brooke.gif", getString("TabbedPaneDemo.brooke"));173174/*175for(int i = 0; i < 6; i++) {176x[i] = (double) rand.nextInt(500);177y[i] = (double) rand.nextInt(500);178}179*/180}181182public void go() {183animator = new javax.swing.Timer(22 + 22 + 22, this);184animator.start();185}186187public void paint(Graphics g) {188g.setColor(getBackground());189g.fillRect(0, 0, getWidth(), getHeight());190191for(int i = 0; i < numImages; i++) {192if(x[i] > 3*i) {193nudge(i);194squish(g, icon[i], xh[i], yh[i], scale[i]);195} else {196x[i] += .05;197y[i] += .05;198}199}200}201202Random rand = new Random();203204public void nudge(int i) {205x[i] += (double) rand.nextInt(1000) / 8756;206y[i] += (double) rand.nextInt(1000) / 5432;207int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);208scale[i] = (double) tmpScale / 10;209int nudgeX = (int) (((double) getWidth()/2) * .8);210int nudgeY = (int) (((double) getHeight()/2) * .60);211xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;212yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;213}214215public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {216if(isVisible()) {217g.drawImage(icon.getImage(), x, y,218(int) (icon.getIconWidth()*scale),219(int) (icon.getIconHeight()*scale),220this);221}222}223224public void actionPerformed(ActionEvent e) {225if(isVisible()) {226repaint();227} else {228animator.stop();229}230}231}232}233234235