/*
 * Filename: QuickPanel.java
 * Written By: Sunit Katkar
 * E-Mail:sunitkatkar@hotmail.com
 * Home-Page : http://www.vidyut.com/sunit/
 * Java Page : www.vidyut.com/sunit/JavaPage.asp
 ***************************************************************
 * Description:
  * QuickPanel is a panel that displays borders within itself to give a visual
 * indication of the extent of the panel. There are 3 styles -
 *   1) RAISE
 *   2) SUNK
 *   3) ETCH
  * Supports all the functionality of the standard Panel class.
  * It is quite simple to implement. Draw lines of different shades
 * of a particular color, say gray, along the edges of the panel.
 * To start with this, I used a Windows icon editor software which lets you
 * make 32x32 or 16x16 pixels icons. There I painted a flat background
 * and then used darker and lighter shades of gray,white, etc to draw
 * borders around this flat color to get the desired effects.
 * Then saw the start and endpoint of each border.
 * Accordingly I calculated the line drawing geometry.
 ***************************************************************
 * Copyright (c) 1997 Sunit Katkar All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * for NON-COMMERCIAL or COMMERCIAL purposes and without fee
 * is hereby granted.
 *
 * Whew ! That was too much legalese..even to have copied and pasted
 * from some other place... PLEASE DO NOT BLAME ME in any way
 * if your system crashes because of this code, or if anything else
 * bad happens. In short "DO WHAT YOU WANT BUT DONT BLAME ME !"
 *****************************************************************/

import java.awt.*;

public class QuickPanel extends Panel
{
    //Variables which describe the type of panel we want
    public int typeofpanel=0;
    public static final int RAISE = 1;
    public static final int SUNK = 2;
    public static final int ETCH = 3;

    /**
     * The border within the QuickPanel to display between the rectangle
     * and the contained components. It is the padding between the
     * components and the edge of the container.
     */
    //Keep higher values if desired. We do not want components added to
    //our panel to crowd near the edges.
    protected Insets insets = new Insets(3,3,3,3);

    /**

     * Constructs a new QuickPanel with a etched border as default.

     */

    public QuickPanel()

    {

        this.typeofpanel = 3; //QuickPanel.ETCH = 3

    }



    /**

     * Constructs a new QuickPanel with the specified border.

     */

    public QuickPanel(int typeofpanel)

    {

        this.typeofpanel = typeofpanel;

    }



    /**

     * Sets the border within the QuickPanel to display between the rectangle

     * and the contained components. It is the padding between the 

     * components and the edge of the container.

     */

     public void setPanelInsets(Insets insets)

     {

        this.insets = insets;

     }



    /**

     * Returns the border within the QuickPanel to display between the rectangle

     * and the contained components. It is the padding between the 

     * components and the edge of the container.

     */

    public Insets insets()

    {

        return insets;

    }

    /**

     * Paints the QuickPanel and its components depending on the specified border.

     */

    public void paint(Graphics g)

    {

        if(typeofpanel == 1)

        {

            paintRaisedPanel(g);//QuickPanel.RAISE = 1

        }

        else if(typeofpanel == 2)

        {

            paintSunkPanel(g);//QuickPanel.SUNK = 2

        }

        else if(typeofpanel == 3)

        {

            paintEtchPanel(g);//QuickPanel.ETCH = 3

        }

    }



    /**

     * Paint the raised panel.

     */
    protected void paintRaisedPanel(Graphics g)

    {

        //Call to super class to request painting services

        super.paint(g);

        //Calculate width & height of the panel

        int width = size().width - 1;

        int height = size().height - 1;

        //Set the color to darkGray, though we may set it to orange!

        //But orange panels are ...... ;)

        g.setColor(Color.darkGray);

        /**

        * Start drawing lines to cover all four edges

        */   

        // Draw a line from bottom-left to bottom-right

        g.drawLine(0, height, width - 1, height); 

        // Draw a line from top-right to bottom-right

        g.drawLine(width, 0, width, height);      

        //Now set the color to a lighter shade of darkGray by specifying

        //gray as the color. You could also use the brighter()method of Color

        //class to get a lighter shade.

        g.setColor(Color.gray);

        // Draw a line from bottom-left to bottom-right        

        g.drawLine(0 + 1, height - 1, width - 2, height - 1); 

        // Draw a line from top-right to bottom-right

        g.drawLine(width - 1, 0 + 1, width - 1, height - 1);  

        //For the raised effect now use a lighter color like white

        g.setColor(Color.white);

        //Draw a line from top-left to bottom-left

        g.drawLine(0, 0, 0, height - 1); 

        //Draw a line from top-left to top-right        

        g.drawLine(0, 0, width - 1, 0);  

        /**

         * All this line drawing and the calculation of the x and y corrdinates is 

         * very easy. Use a icon maker/editor software. Start with a flat color icon.  

         * Then using two shades of a color (other than flat color) draw borders to 

         * give a 3D look to your flat square icon.

         * Since you are using a icon editor, you will see each pixel displayed as a

         * grid. Then you can see for yourself how to arrive at the x and y coordinates.

         * I have done the same and then arrived at the geometry. It is really simple.

         */ 



    }

    //Similarly for a sunk panel

     protected void  paintSunkPanel(Graphics g)

     {

        super.paint(g);

        int width = size().width - 1;

        int height = size().height - 1;

        //Set color to gray

        g.setColor(Color.gray);

        

        //Draw a line from top-left to top-right

        g.drawLine(0, 0, width - 1, 0);  

        //Draw a line from top-left to bottom-left

        g.drawLine(0, 0, 0, height - 1); 

        

        //Set color as black

        g.setColor(Color.black);

        

        //Draw a line from top-left to top-right

        g.drawLine(1, 1, width - 2, 1);  

        //Draw a line from top-left to bottom - left         

        g.drawLine(1, 1, 1, height - 2); 

        

        //Set a light color - white

        g.setColor(Color.white);

        

        //Draw a line from bottom-left to bottom-right but add

        g.drawLine(0, height, width, height);             

        //Draw a line from bottom-left to bottom-right but add 

        //a one pixel difference        

        g.drawLine(1, height - 1, width - 1, height - 1); 

        //Draw a line from top-right to bottom-right

        g.drawLine(width, 0, width, height);              

        //Draw a line from top-right to bottom-right but add

        //a one pixel difference

        g.drawLine(width - 1, 1, width - 1, height - 1); 

     }

    /**

     * Similarly for etched panel...figure out the line 

     * drawing and color setting for yourself, its easy!

     */

     protected void  paintEtchPanel(Graphics g)

     {

        super.paint(g);

        int width = size().width - 1;

        int height = size().height - 1;

        g.setColor(Color.gray);

        //Line from top-left to bottom-left

        g.drawLine(0, 0, 0, height - 1); 

        //Line from top-left to top-right

        g.drawLine(0, 0, width - 1, 0);  

        

        g.setColor(Color.white);

        

        //Line from top-left to bottom-left

        g.drawLine(1, 1, 1, height - 2); 

        g.drawLine(1, 1, width - 3, 1);  // Topleft to topright

        

        g.setColor(Color.gray);

        

        //Line from bottom-left to bottom-right

        g.drawLine(0, height - 1, width - 1, height - 1); 

        // top-right to bottom-right

        g.drawLine(width - 1, 2, width - 1, height);      

        

        g.setColor(Color.white);

        // Line from bottom-left to bottomright

        g.drawLine(0, height, width, height); 

        // Line from top-right to bottom-right

        g.drawLine(width, 0, width, height);  



    }



}//All ends :)



The source code for the applet that uses this class is here. See the applet in action.