home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / panel3d.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.5 KB  |  367 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     Panel3D.java
  15.  
  16.         a 3 dimesional panel.
  17.  
  18.  
  19.     Authors:
  20.  
  21.         jlee          James Lee
  22.  
  23.  
  24.     Version Ident:
  25.  
  26.         $Header$
  27.  
  28.  
  29.     History:
  30.  
  31.         4-Mar-96 jlee     Initial Creation
  32.        12-Mar-96 jlee     Modified Panel3D so that it can display without border.
  33.        25-Mar-96 jlee     Modified layoutCOntainer so that it dosn't repeat the same layout change.
  34.                           And used line drawing to make 3d effect instead panel drawing.
  35. ---------------------------------------------------------------------------*/
  36.  
  37. package pj.awt;
  38.  
  39. import collections.Assertable;
  40. import collections.ImplementationCheckable;
  41. import collections.ImplementationError;
  42.  
  43. import java.awt.Canvas;
  44. import java.awt.Color;
  45. import java.awt.Component;
  46. import java.awt.Container;
  47. import java.awt.Dimension;
  48. import java.awt.Event;
  49. import java.awt.Graphics;
  50. import java.awt.GridLayout;
  51. import java.awt.Insets;
  52. import java.awt.LayoutManager;
  53. import java.awt.Panel;
  54. import java.awt.Rectangle;
  55. import java.lang.String;
  56.  
  57. /**
  58.  * A 3 dimensional panel implemented using Panel3DLayout.
  59.  *
  60.  * @version 0.00 4-Mar-96
  61.  * @author James lee
  62. */
  63. public class Panel3D extends Panel
  64.     {
  65.     // --- Instance variables
  66.     private int nBorderWidth;
  67.  
  68.     // --- Public constructors
  69.  
  70.     /**
  71.      * Construct a 3 dimensional panel, the width of border is 2.
  72.     */
  73.     public Panel3D()
  74.         {
  75.         initBorderWidth();
  76.         setLayout( new Panel3DLayout() );
  77.         } // Panel3D
  78.  
  79.     /**
  80.      * Construct a 3 dimensional panel, with specified border width.
  81.      * @param nBW  Border width
  82.     */
  83.     public Panel3D( int nBW )
  84.         {
  85.         nBorderWidth = (nBW <= 0) ? 1: nBW;//Fix: somehow, zero Border Width prevents text drawing, so this hack.
  86.         setLayout( new Panel3DLayout() );
  87.         } // Panel3D
  88.  
  89.     /**
  90.      * Construct a 3 dimensional panel, with specified component.
  91.      * @param cpnt A component that's going to be contained int this Panel3D.
  92.     */
  93.     public Panel3D( Component cpnt )
  94.         {
  95.         initBorderWidth();
  96.         setLayout( new Panel3DLayout() );
  97.         add( cpnt );
  98.         } // Panel3D
  99.  
  100.     /**
  101.      * Construct a 3 dimensional panel, with specified component & border width.
  102.      * @param cpnt A component that's going to be contained int this Panel3D.
  103.      * @param nBW Border Width
  104.      */
  105.     public Panel3D( Component cpnt, int nBW )
  106.         {
  107.         nBorderWidth = (nBW <= 0) ? 1: nBW;//Fix: somehow, zero Border Width prevents text drawing, so this hack.
  108.         setLayout( new Panel3DLayout() );
  109.         add( cpnt );
  110.         } // Panel3D
  111.  
  112.     // --- Public operations
  113.  
  114.     public int getBorderWidth()
  115.         {
  116.         return nBorderWidth;
  117.         }
  118.  
  119.     public void paint( Graphics g )
  120.         {
  121.         for ( int j = 0; j < 4; j++ )
  122.             {
  123.             for ( int i = 0; i < nBorderWidth; i++ )
  124.                 {
  125.                 switch ( j )
  126.                     {
  127.                     case 0://east
  128.                             g.setColor( Color.white );
  129.                             g.drawLine( size().width - i, 0, size().width - i, size().height );
  130.                         break;
  131.  
  132.                     case 2://north
  133.                             g.setColor( Color.gray );
  134.                             g.drawLine( 0, i, size().width, i );
  135.                         break;
  136.  
  137.                     case 3://south
  138.                             g.setColor( Color.white );
  139.                             g.drawLine( 0, size().height - i, size().width, size().height - i );
  140.                         break;
  141.  
  142.                     case 1://west
  143.                             g.setColor( Color.gray );
  144.                             g.drawLine( i, 0, i, size().height );
  145.                         break;
  146.                     }//switch
  147.                 }//for i
  148.             }//for j
  149.         }
  150.  
  151.     // --- Private operations
  152.  
  153.     private protected void initBorderWidth()
  154.         {
  155.         nBorderWidth = 2;
  156.         }
  157.  
  158.  
  159.     } // TabBar
  160.  
  161. /*---------------------------------------------------------------------------
  162.  
  163.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  164.  
  165.     Dow Jones makes no representations or warranties about 
  166.     the suitability of this software, either express or 
  167.     implied, including but not limited to the implied warranties 
  168.     of merchantability, fitness for a particular purpose, 
  169.     or non-infringement.  Dow Jones will not be liable for 
  170.     any damages suffered by a user as a result of using, 
  171.     modifying or distributing this software or its derivatives.
  172.  
  173.  
  174.     @(#)Panel3DLayout.java   0.00 4-Mar-96
  175.  
  176.         A Panel3DLayout that implements 3D panel.
  177.  
  178.     Authors:
  179.  
  180.         jlee        James Lee
  181.  
  182.     Version Ident:
  183.  
  184.         $Header$
  185.  
  186.     History:
  187.  
  188.          4-Mar-96    jlee   Initial creation.
  189.  
  190. ---------------------------------------------------------------------------*/
  191.  
  192. /**
  193.  * Panel3DLayout is used to layout Tabs panel and 3D lines in a panel.
  194.  * @version     0.00, 4-Mar-96
  195.  * @author      James Lee
  196.  */
  197. class Panel3DLayout implements LayoutManager {
  198.  
  199.     // --- Instance variables
  200.     private int hgap;
  201.     private int vgap;
  202.  
  203.     private int nWidth;
  204.     private int nHeight;
  205.  
  206.     private Rectangle recLast = new Rectangle(0, 0, 0, 0);
  207.  
  208.  
  209.     // --- Public constructors
  210.     /**
  211.      * Constructs a new Panel3DLayout
  212.      * Default value for hgap and vgap is 0.
  213.      */
  214.     public Panel3DLayout()
  215.         {
  216.         this(0, 0);
  217.         }
  218.  
  219.     /**
  220.      * Constructs a new Panel3DLayout with the specified gap values.
  221.      * @param hgap the horizontal gap variable
  222.      * @param vgap the vertical gap variable
  223.      */
  224.     public Panel3DLayout(int hgap, int vgap)
  225.         {
  226.         this.hgap = hgap;
  227.         this.vgap = vgap;
  228.         }
  229.  
  230.     // --- Public operations
  231.  
  232.     /**
  233.      * Adds the specified component to the layout. Not used by this class.
  234.      * @param name the name of the component
  235.      * @param comp the the component to be added
  236.      */
  237.     public void addLayoutComponent(String name, Component comp)
  238.         {
  239.         }
  240.  
  241.     /**
  242.      * Removes the specified component from the layout. Not used by
  243.      * this class.
  244.      * @param comp the component to remove
  245.      */
  246.     public void removeLayoutComponent(Component comp)
  247.         {
  248.         }
  249.  
  250.     /**
  251.      * Returns the preferred dimensions for this layout given the components
  252.      * in the specified target container.
  253.      * @param target the component which needs to be laid out
  254.      * @see Container
  255.      * @see #minimumLayoutSize
  256.      */
  257.     public Dimension preferredLayoutSize(Container target)
  258.         {
  259.         Dimension dim = new Dimension(0, 0);
  260.         int nmembers = target.countComponents();
  261.  
  262.         Component m;
  263.  
  264.         if ( nmembers > 0 )
  265.             {
  266.             m = target.getComponent(nmembers - 1);
  267.  
  268.             if (m.isVisible())
  269.                 {
  270.                 Dimension d = m.preferredSize();
  271.                 dim.height += d.height;
  272.                 dim.width  += d.width;
  273.  
  274.                 dim.width += ((Panel3D)target).getBorderWidth();
  275.                 dim.height += ((Panel3D)target).getBorderWidth();
  276.                 }
  277.             }
  278.  
  279.         Insets insets = target.insets();
  280.         dim.width += insets.left + insets.right + hgap*2;
  281.         dim.height += insets.top + insets.bottom + vgap*2;
  282.         return dim;
  283.         }
  284.  
  285.     /**
  286.      * Returns the minimum dimensions needed to layout the components
  287.      * contained in the specified target container.
  288.      * @param target the component which needs to be laid out
  289.      * @see #preferredLayoutSize
  290.      */
  291.     public Dimension minimumLayoutSize(Container target)
  292.         {
  293.         Dimension dim = new Dimension(0, 0);
  294.         int nmembers = target.countComponents();
  295.  
  296.         Component m;
  297.  
  298.         if ( nmembers > 0 )
  299.             {
  300.             m = target.getComponent(nmembers - 1);
  301.  
  302.             if (m.isVisible())
  303.                 {
  304.                 Dimension d = m.minimumSize();
  305.                 dim.height += d.height;
  306.                 dim.width  += d.width;
  307.  
  308.                 dim.width += ((Panel3D)target).getBorderWidth();
  309.                 dim.height += ((Panel3D)target).getBorderWidth();
  310.                 }
  311.             }
  312.  
  313.         Insets insets = target.insets();
  314.         dim.width += insets.left + insets.right + hgap*2;
  315.         dim.height += insets.top + insets.bottom + vgap*2;
  316.  
  317.         return dim;
  318.         }
  319.  
  320.  
  321.     /**
  322.      * Lays out the container. This method will actually reshape the
  323.      * components in the target in order to satisfy the constraints of
  324.      * the Panel3DLayout object.
  325.      * @param target the specified component being laid out.
  326.      * @see Container
  327.      */
  328.     public void layoutContainer(Container target)
  329.         {
  330.         int nBorderWidth = ((Panel3D)target).getBorderWidth();
  331.         int nmembers = target.countComponents();
  332.  
  333.         Insets insets = target.insets();
  334.         Rectangle rec = target.bounds();
  335.  
  336.         if ( target.isValid() &&
  337.             rec.x == recLast.x && rec.y == recLast.y &&
  338.             rec.width == recLast.width && rec.height == recLast.height  )
  339.             return;
  340.         else
  341.             recLast = rec;
  342.  
  343.         nWidth        = rec.width;
  344.         nHeight       = rec.height;
  345.  
  346.         for (int i = 0 ; i < nmembers ; i++)
  347.             {
  348.             Component m = target.getComponent(i);
  349.  
  350.             if ( m.isVisible() )
  351.                 {
  352.                 if ( i == 0 ) //Center
  353.                     m.reshape( nBorderWidth, nBorderWidth, rec.width - 2 * nBorderWidth, rec.height - 2 * nBorderWidth );
  354.                 }//if
  355.             }//for
  356.         }
  357.  
  358.     /**
  359.      * Returns the String representation of this Panel3DLayout's values.
  360.      */
  361.     public String toString()
  362.         {
  363.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
  364.                 "[width=" + nWidth + ",height=" + nHeight + "]";
  365.         }
  366.     } //Panel3DLayout
  367.