//  conceptGO Open Source - www.conceptGO.com
//  copyright (c)  2003, conceptGO
//
//  This software is licensed under the 
//  conceptGO Open Source License, available 
//  at http://www.conceptgo.com/cgooslicense.html
//

// JTiler - Tiled JPanel with components in subpanels

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTiler extends    JPanel
                    implements ActionListener,
                               WindowListener
{
// uncomment line for ImageIcon usage
//   ImageIcon  iiBackground;

// uncomment line for Icon usage
   Icon  iiBackground;

   int        iiBHeight, iiBWidth;
   JButton    jbOK = new JButton( "I'm a Button"),
              jb[] = new JButton[4];
   JComponent jcMatch[] = new JComponent[jb.length];
   JFrame     myFrame = new JFrame("Panel Tiler");
   JLabel     jl = new JLabel( "I'm a Label" ),
              jlOT = new JLabel("Click to toggle Opacity:");
   JPanel     jpNorth = new JPanel(),
              jpCenter = new JPanel(),
              jpFSouth = new JPanel();
   JTextField jtf = new JTextField("I'm some text.");


   public JTiler()
   {
     super( new BorderLayout() );

// background when opaque
     setBackground( 
        Color.PINK.brighter() ); 

     iiBackground = 
// uncomment this for Icon usage
     UIManager.getIcon("OptionPane.errorIcon");

// uncomment this for ImageIcon usage
// This image can be found at yourJDKdir/docs/images/
// in both JDK 1.1 and 1.2.  Copy it to a classloader available
// directory OR change to fully qualified path OR use your own
// available gif/jpeg - small, of course, to get tiled effect.
//       new ImageIcon(
//           JTiler.class.getResource( "cupHJbutton.gif" ));

     iiBHeight = iiBackground.getIconHeight();
     iiBWidth = iiBackground.getIconWidth();

     jpNorth.add( jl );
     jpNorth.add( jtf );
     jpCenter.add( jbOK );

// subpanels will block background if opaque
     jpNorth.setOpaque( false );
     jpCenter.setOpaque( false );

     this.add( jpNorth, BorderLayout.NORTH );
     this.add( jpCenter, BorderLayout.CENTER );

     jpFSouth = new JPanel();
     jb[0] = new JButton("Label");
     jcMatch[0] = jl;
     jb[1] = new JButton("North");
     jcMatch[1] = jpNorth;
     jb[2] = new JButton("Center");
     jcMatch[2] = jpCenter;
     jb[3] = new JButton("This");
     jcMatch[3] = this;

     jpFSouth.add( jlOT );
     for( int i = 0; i < jb.length; i++ )
     {
       jb[i].addActionListener( this );
       jpFSouth.add( jb[i] );
     }
      
     myFrame.addWindowListener( this );

     Container cp = myFrame.getContentPane();
     cp.add( this, BorderLayout.CENTER );
     cp.add( jpFSouth, BorderLayout.SOUTH );
     myFrame.pack();
     myFrame.show();
   }  // end constructor


   public void paintComponent(Graphics g)
   {
     Dimension dim = getSize();
     int x, y;

// ensure proper background handling
    if( isOpaque() ) { super.paintComponent( g ); }

// redraw image horizontal/vertical until panel is filled
     for( y = 0; y < dim.height; y += iiBHeight )
     {
       for( x = 0; x < dim.width; x += iiBWidth )
       {
	     iiBackground.paintIcon( this, g, x, y );
       }
     }
   } // end paintComponent


  // ActionListener Implementation
  public void actionPerformed(ActionEvent e)
  {
     Object oSource = e.getSource();

     for( int i = 0; i < jb.length; i++ )
     {
       if( oSource == jb[i] )
       {
         jcMatch[i].setOpaque( !jcMatch[i].isOpaque() );
         repaint();
         break;
       } // end if oSource
     } // end for

  } // end actionPerformed


// Window Listener Implementation
    public void windowOpened(WindowEvent e) {}
     public void windowClosing(WindowEvent e)
     {
       myFrame.dispose();
       System.exit(0);
     }
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
// End Window Listener Implementation


   public static void main(String[] args)
   {
      new JTiler();
   }

}  // end class JTiler

Copyright © 2000-2003, conceptGO