1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import javax.swing.text.*;
  37 import javax.swing.border.*;
  38 import javax.swing.colorchooser.*;
  39 import javax.swing.filechooser.*;
  40 import javax.accessibility.*;
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import java.beans.*;
  45 import java.util.*;
  46 import java.io.*;
  47 import java.applet.*;
  48 import java.net.*;
  49 
  50 /**
  51  * JTabbedPane Demo
  52  *
  53  * @author Jeff Dinkins
  54  */
  55 public class TabbedPaneDemo extends DemoModule implements ActionListener {
  56     HeadSpin spin;
  57 
  58     JTabbedPane tabbedpane;
  59 
  60     ButtonGroup group;
  61 
  62     JRadioButton top;
  63     JRadioButton bottom;
  64     JRadioButton left;
  65     JRadioButton right;
  66 
  67     /**
  68      * main method allows us to run as a standalone demo.
  69      */
  70     public static void main(String[] args) {
  71         TabbedPaneDemo demo = new TabbedPaneDemo(null);
  72         demo.mainImpl();
  73     }
  74 
  75     /**
  76      * TabbedPaneDemo Constructor
  77      */
  78     public TabbedPaneDemo(SwingSet2 swingset) {
  79         // Set the title for this demo, and an icon used to represent this
  80         // demo inside the SwingSet2 app.
  81         super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
  82 
  83         // create tab position controls
  84         JPanel tabControls = new JPanel();
  85         tabControls.add(new JLabel(getString("TabbedPaneDemo.label")));
  86         top    = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.top")));
  87         left   = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.left")));
  88         bottom = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.bottom")));
  89         right  = (JRadioButton) tabControls.add(new JRadioButton(getString("TabbedPaneDemo.right")));
  90         getDemoPanel().add(tabControls, BorderLayout.NORTH);
  91 
  92         group = new ButtonGroup();
  93         group.add(top);
  94         group.add(bottom);
  95         group.add(left);
  96         group.add(right);
  97 
  98         top.setSelected(true);
  99 
 100         top.addActionListener(this);
 101         bottom.addActionListener(this);
 102         left.addActionListener(this);
 103         right.addActionListener(this);
 104 
 105         // create tab
 106         tabbedpane = new JTabbedPane();
 107         getDemoPanel().add(tabbedpane, BorderLayout.CENTER);
 108 
 109         String name = getString("TabbedPaneDemo.laine");
 110         JLabel pix = new JLabel(createImageIcon("tabbedpane/laine.jpg", name));
 111         tabbedpane.add(name, pix);
 112 
 113         name = getString("TabbedPaneDemo.ewan");
 114         pix = new JLabel(createImageIcon("tabbedpane/ewan.jpg", name));
 115         tabbedpane.add(name, pix);
 116 
 117         name = getString("TabbedPaneDemo.hania");
 118         pix = new JLabel(createImageIcon("tabbedpane/hania.jpg", name));
 119         tabbedpane.add(name, pix);
 120 
 121         name = getString("TabbedPaneDemo.bounce");
 122         spin = new HeadSpin();
 123         tabbedpane.add(name, spin);
 124 
 125         tabbedpane.getModel().addChangeListener(
 126            new ChangeListener() {
 127               public void stateChanged(ChangeEvent e) {
 128                   SingleSelectionModel model = (SingleSelectionModel) e.getSource();
 129                   if(model.getSelectedIndex() == tabbedpane.getTabCount()-1) {
 130                       spin.go();
 131                   }
 132               }
 133            }
 134         );
 135     }
 136 
 137     public void actionPerformed(ActionEvent e) {
 138         if(e.getSource() == top) {
 139             tabbedpane.setTabPlacement(JTabbedPane.TOP);
 140         } else if(e.getSource() == left) {
 141             tabbedpane.setTabPlacement(JTabbedPane.LEFT);
 142         } else if(e.getSource() == bottom) {
 143             tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
 144         } else if(e.getSource() == right) {
 145             tabbedpane.setTabPlacement(JTabbedPane.RIGHT);
 146         }
 147     }
 148 
 149     class HeadSpin extends JComponent implements ActionListener {
 150         javax.swing.Timer animator;
 151 
 152         ImageIcon[] icon = new ImageIcon[6];
 153 
 154         int tmpScale;
 155 
 156         static final int numImages = 6;
 157 
 158         double[] x = new double[numImages];
 159         double[] y = new double[numImages];
 160 
 161         int[] xh = new int[numImages];
 162         int[] yh = new int[numImages];
 163 
 164         double[] scale = new double[numImages];
 165 
 166         public HeadSpin() {
 167             setBackground(Color.black);
 168             icon[0] = createImageIcon("tabbedpane/ewan.gif", getString("TabbedPaneDemo.ewan"));
 169             icon[1] = createImageIcon("tabbedpane/stephen.gif", getString("TabbedPaneDemo.stephen"));
 170             icon[2] = createImageIcon("tabbedpane/david.gif", getString("TabbedPaneDemo.david"));
 171             icon[3] = createImageIcon("tabbedpane/matthew.gif", getString("TabbedPaneDemo.matthew"));
 172             icon[4] = createImageIcon("tabbedpane/blake.gif", getString("TabbedPaneDemo.blake"));
 173             icon[5] = createImageIcon("tabbedpane/brooke.gif", getString("TabbedPaneDemo.brooke"));
 174 
 175             /*
 176             for(int i = 0; i < 6; i++) {
 177                 x[i] = (double) rand.nextInt(500);
 178                 y[i] = (double) rand.nextInt(500);
 179             }
 180             */
 181         }
 182 
 183         public void go() {
 184             animator = new javax.swing.Timer(22 + 22 + 22, this);
 185             animator.start();
 186         }
 187 
 188         public void paint(Graphics g) {
 189             g.setColor(getBackground());
 190             g.fillRect(0, 0, getWidth(), getHeight());
 191 
 192             for(int i = 0; i < numImages; i++) {
 193                 if(x[i] > 3*i) {
 194                     nudge(i);
 195                     squish(g, icon[i], xh[i], yh[i], scale[i]);
 196                 } else {
 197                     x[i] += .05;
 198                     y[i] += .05;
 199                 }
 200             }
 201         }
 202 
 203         Random rand = new Random();
 204 
 205         public void nudge(int i) {
 206             x[i] += (double) rand.nextInt(1000) / 8756;
 207             y[i] += (double) rand.nextInt(1000) / 5432;
 208             int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
 209             scale[i] = (double) tmpScale / 10;
 210             int nudgeX = (int) (((double) getWidth()/2) * .8);
 211             int nudgeY = (int) (((double) getHeight()/2) * .60);
 212             xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
 213             yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
 214         }
 215 
 216         public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {
 217             if(isVisible()) {
 218                 g.drawImage(icon.getImage(), x, y,
 219                             (int) (icon.getIconWidth()*scale),
 220                             (int) (icon.getIconHeight()*scale),
 221                             this);
 222             }
 223         }
 224 
 225         public void actionPerformed(ActionEvent e) {
 226             if(isVisible()) {
 227                 repaint();
 228             } else {
 229                 animator.stop();
 230             }
 231         }
 232     }
 233 }