jdk/test/sanity/client/lib/SwingSet3/src/com/sun/swingset3/demos/tabbedpane/TabbedPaneDemo.java
changeset 36744 a00905527ec2
child 37663 6293ca84c8dc
equal deleted inserted replaced
36743:bdc3f1b79fb7 36744:a00905527ec2
       
     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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    95         frame.getContentPane().add(new TabbedPaneDemo());
       
    96         frame.setPreferredSize(new Dimension(800, 600));
       
    97         frame.pack();
       
    98         frame.setLocationRelativeTo(null);
       
    99         frame.setVisible(true);
       
   100     }
       
   101 
       
   102     /**
       
   103      * TabbedPaneDemo Constructor
       
   104      */
       
   105     public TabbedPaneDemo() {
       
   106         setLayout(new BorderLayout());
       
   107 
       
   108         // create tab position controls
       
   109         JPanel tabControls = new JPanel();
       
   110         tabControls.add(new JLabel(TAB_PLACEMENT));
       
   111         top = (JRadioButton) tabControls.add(new JRadioButton(TOP));
       
   112         left = (JRadioButton) tabControls.add(new JRadioButton(LEFT));
       
   113         bottom = (JRadioButton) tabControls.add(new JRadioButton(BOTTOM));
       
   114         right = (JRadioButton) tabControls.add(new JRadioButton(RIGHT));
       
   115         add(tabControls, BorderLayout.NORTH);
       
   116 
       
   117         group = new ButtonGroup();
       
   118         group.add(top);
       
   119         group.add(bottom);
       
   120         group.add(left);
       
   121         group.add(right);
       
   122 
       
   123         top.setSelected(true);
       
   124 
       
   125         top.addActionListener(this);
       
   126         bottom.addActionListener(this);
       
   127         left.addActionListener(this);
       
   128         right.addActionListener(this);
       
   129 
       
   130         // create tab
       
   131         tabbedpane = new JTabbedPane();
       
   132         add(tabbedpane, BorderLayout.CENTER);
       
   133 
       
   134         String name = CAMILLE;
       
   135         JLabel pix = new JLabel(resourceManager.createImageIcon("camille.jpg", name));
       
   136         tabbedpane.add(name, pix);
       
   137 
       
   138         name = MIRANDA;
       
   139         pix = new JLabel(resourceManager.createImageIcon("miranda.jpg", name));
       
   140         pix.setToolTipText(resourceManager.getString("TabbedPaneDemo.miranda.tooltip"));
       
   141         tabbedpane.add(name, pix);
       
   142 
       
   143         name = EWAN;
       
   144         pix = new JLabel(resourceManager.createImageIcon("ewan.jpg", name));
       
   145         tabbedpane.add(name, pix);
       
   146 
       
   147         name = BOUNCE;
       
   148         spin = new HeadSpin();
       
   149         tabbedpane.add(name, spin);
       
   150 
       
   151         tabbedpane.getModel().addChangeListener((ChangeEvent e) -> {
       
   152             SingleSelectionModel model = (SingleSelectionModel) e.getSource();
       
   153             if (model.getSelectedIndex() == tabbedpane.getTabCount() - 1) {
       
   154                 spin.go();
       
   155             }
       
   156         });
       
   157     }
       
   158 
       
   159     @Override
       
   160     public void actionPerformed(ActionEvent e) {
       
   161         if (e.getSource() == top) {
       
   162             tabbedpane.setTabPlacement(JTabbedPane.TOP);
       
   163         } else if (e.getSource() == left) {
       
   164             tabbedpane.setTabPlacement(JTabbedPane.LEFT);
       
   165         } else if (e.getSource() == bottom) {
       
   166             tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
       
   167         } else if (e.getSource() == right) {
       
   168             tabbedpane.setTabPlacement(JTabbedPane.RIGHT);
       
   169         }
       
   170     }
       
   171 
       
   172     private class HeadSpin extends JComponent implements ActionListener {
       
   173 
       
   174         private javax.swing.Timer animator;
       
   175 
       
   176         private final ImageIcon[] icon = new ImageIcon[6];
       
   177 
       
   178         private final static int numImages = 6;
       
   179 
       
   180         private final double[] x = new double[numImages];
       
   181         private final double[] y = new double[numImages];
       
   182 
       
   183         private final int[] xh = new int[numImages];
       
   184         private final int[] yh = new int[numImages];
       
   185 
       
   186         private final double[] scale = new double[numImages];
       
   187 
       
   188         private final Random rand = new Random();
       
   189 
       
   190         public HeadSpin() {
       
   191             setBackground(Color.black);
       
   192             icon[0] = resourceManager.createImageIcon("ewan.gif", resourceManager.getString("TabbedPaneDemo.ewan"));
       
   193             icon[1] = resourceManager.createImageIcon("stephen.gif", resourceManager.getString("TabbedPaneDemo.stephen"));
       
   194             icon[2] = resourceManager.createImageIcon("david.gif", resourceManager.getString("TabbedPaneDemo.david"));
       
   195             icon[3] = resourceManager.createImageIcon("matthew.gif", resourceManager.getString("TabbedPaneDemo.matthew"));
       
   196             icon[4] = resourceManager.createImageIcon("blake.gif", resourceManager.getString("TabbedPaneDemo.blake"));
       
   197             icon[5] = resourceManager.createImageIcon("brooke.gif", resourceManager.getString("TabbedPaneDemo.brooke"));
       
   198 
       
   199             /*
       
   200              for(int i = 0; i < 6; i++) {
       
   201                  x[i] = (double) rand.nextInt(500);
       
   202                  y[i] = (double) rand.nextInt(500);
       
   203              }
       
   204              */
       
   205         }
       
   206 
       
   207         public void go() {
       
   208             animator = new javax.swing.Timer(22 + 22 + 22, this);
       
   209             animator.start();
       
   210         }
       
   211 
       
   212         @Override
       
   213         public void paint(Graphics g) {
       
   214             g.setColor(getBackground());
       
   215             g.fillRect(0, 0, getWidth(), getHeight());
       
   216 
       
   217             for (int i = 0; i < numImages; i++) {
       
   218                 if (x[i] > 3 * i) {
       
   219                     nudge(i);
       
   220                     squish(g, icon[i], xh[i], yh[i], scale[i]);
       
   221                 } else {
       
   222                     x[i] += .05;
       
   223                     y[i] += .05;
       
   224                 }
       
   225             }
       
   226         }
       
   227 
       
   228         public void nudge(int i) {
       
   229             x[i] += (double) rand.nextInt(1000) / 8756;
       
   230             y[i] += (double) rand.nextInt(1000) / 5432;
       
   231             int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10);
       
   232             scale[i] = (double) tmpScale / 10;
       
   233             int nudgeX = (int) (((double) getWidth() / 2) * .8);
       
   234             int nudgeY = (int) (((double) getHeight() / 2) * .60);
       
   235             xh[i] = (int) (Math.sin(x[i]) * nudgeX) + nudgeX;
       
   236             yh[i] = (int) (Math.sin(y[i]) * nudgeY) + nudgeY;
       
   237         }
       
   238 
       
   239         public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) {
       
   240             if (isVisible()) {
       
   241                 g.drawImage(icon.getImage(), x, y,
       
   242                         (int) (icon.getIconWidth() * scale),
       
   243                         (int) (icon.getIconHeight() * scale),
       
   244                         this);
       
   245             }
       
   246         }
       
   247 
       
   248         @Override
       
   249         public void actionPerformed(ActionEvent e) {
       
   250             if (isVisible()) {
       
   251                 repaint();
       
   252             } else {
       
   253                 animator.stop();
       
   254             }
       
   255         }
       
   256     }
       
   257 }