home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / SwingSet2 / src / TabbedPaneDemo.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.3 KB  |  243 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)TabbedPaneDemo.java    1.7 02/06/13
  38.  */
  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. /**
  58.  * JTabbedPane Demo
  59.  *
  60.  * @version 1.7 06/13/02
  61.  * @author Jeff Dinkins
  62.  */
  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.     /**
  76.      * main method allows us to run as a standalone demo.
  77.      */
  78.     public static void main(String[] args) {
  79.     TabbedPaneDemo demo = new TabbedPaneDemo(null);
  80.     demo.mainImpl();
  81.     }
  82.  
  83.     /**
  84.      * TabbedPaneDemo Constructor
  85.      */
  86.     public TabbedPaneDemo(SwingSet2 swingset) {
  87.     // Set the title for this demo, and an icon used to represent this
  88.     // demo inside the SwingSet2 app.
  89.     super(swingset, "TabbedPaneDemo", "toolbar/JTabbedPane.gif");
  90.  
  91.     // create tab position controls
  92.     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.     // create tab 
  114.     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.         /*
  184.         for(int i = 0; i < 6; i++) {
  185.         x[i] = (double) rand.nextInt(500);
  186.         y[i] = (double) rand.nextInt(500);
  187.         }
  188.         */
  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.