|
/* * Filename: QuickLabelPanel.java * Written By: Sunit Katkar * E-Mail:sunitkatkar@hotmail.com * Home-Page : www.vidyut.com/sunit * Java Page : www.vidyut.com/sunit/JavaPage.html *************************************************************** * Description: * QuickLabelPanel allows you to have a etched panel (like one of * the options from the QuickPanel) with a text label at the top * left of the panel. * 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 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. * Further use clipRect to clear the area you want and draw a text * string. There you have a panel with a text label and a etched effect * put together *************************************************************** * 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 QuickLabelPanel extends Panel
{
/**
* The QuickLabelPanel's text label at top left : panellabel.
*/
protected String panellabel ;
/**
* Check if the user passed a panellabel. If not then we just draw a
* panel with the borders visible
*/
protected boolean is_label_present = false;
/**
* The border within the QuickLabelPanel 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. Especially insets.top is high
//as we do not want any components added to this panel to overlap
//the text label.
protected Insets insets = new Insets(14, 6, 6, 6);
/**
* The font for the panellabel. We use a "Dialog" font by default
*/
protected Font panellabelfont;
/**
* Constructs a new QuickLabelPanel.
*/
public QuickLabelPanel()
{
this.panellabel = ""; //No label available, take a blank string
is_label_present = false;
}
/**
* Constructs a new QuickLabelPanel with a panellabel.
*/
public QuickLabelPanel(String panellabel)
{
this.panellabel = panellabel ;
is_label_present = true;
}
/**
* Creates the peer.
*/
public void addNotify()
{
super.addNotify();
}
/**
* Returns the QuickLabelPanel's panellabel.
*/
public String getQuickLabelPanelLabel()
{
return this.panellabel;
}
/**
* Sets the QuickLabelPanel's panellabel.
*/
public void setQuickLabelPanelLabel(String panellabel)
{
this.panellabel = panellabel;
}
/**
* Returns the border within the QuickLabelPanel 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;
}
/**
* Sets the border within the QuickLabelPanel 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;
}
/**
* Paints the QuickLabelPanel
*/
public void paint(Graphics g)
{
if (is_label_present)
{
paintEtchLabelPanel(g);
}
else
{
paintEtchPanel(g);
}
}
protected void paintEtchLabelPanel(Graphics g)
{
super.paint(g);
//Use a Dialog font
panellabelfont = new Font("Dialog",Font.PLAIN,12);
//Get the fontmetrics for the font
FontMetrics fmt = getFontMetrics (panellabelfont);
//Set the font
g.setFont(panellabelfont);
int left = 0;
int top = fmt.getAscent() - 5;
int width = size().width - 1;
int height = size().height - 1;
//Clear area
g.clearRect(location().x, location().y, size().width, size().height);
//Now the same line drawing logic as explained in QuickPanel source code
g.setColor(Color.gray);
//Line from top-left to bottom-left
g.drawLine(left, top, left, height - 1);
// Line from top-left to top-right
g.drawLine(left, top, width - 1, top);
g.setColor(Color.white);
// Line from top-left to bottom-left
g.drawLine(left + 1, top + 1, left + 1, height - 2);
// Line from top-left to top-right
g.drawLine(left + 1, top + 1, width - 3, top + 1);
g.setColor(Color.gray);
// Line from bottom-left to bottom-right
g.drawLine(left, height - 1, width - 1, height - 1);
// Line from top-right to bottom-right
g.drawLine(width - 1, top + 2, width - 1, height);
g.setColor(Color.white);
// Line from bottom-left to bottom-right
g.drawLine(left, height, width, height);
// Line from top-right to bottom-right
g.drawLine(width, top, width, height);
//Clear an area at top-left of panel (top-left is location where panel starts)
g.clearRect(10, top, fmt.stringWidth(panellabel) + 4 , 4);
g.setColor(Color.black);
g.drawString(panellabel, 12, fmt.getHeight() - fmt.getDescent());
}
/**
* NO label ? Okay ! Draw just a panel!
*/
protected void paintEtchPanel(Graphics g)
{
super.paint(g);
panellabelfont = new Font("Dialog",Font.PLAIN,12);
FontMetrics fmt = getFontMetrics (panellabelfont);
int left = 0;
int top = fmt.getAscent() - 5;
int width = size().width - 1;
int height = size().height - 1;
g.clearRect(location().x, location().y, size().width, size().height);
g.setColor(Color.gray);
//Line from top-left to bottom-left
g.drawLine(left, top, left, height - 1);
// Line from top-left to top-right
g.drawLine(left, top, width - 1, top);
g.setColor(Color.white);
// Line from top-left to bottom-left
g.drawLine(left + 1, top + 1, left + 1, height - 2);
// Line from top-left to top-right
g.drawLine(left + 1, top + 1, width - 3, top + 1);
g.setColor(Color.gray);
// Line from bottom-left to bottom-right
g.drawLine(left, height - 1, width - 1, height - 1);
// Line from top-right to bottom-right
g.drawLine(width - 1, top + 2, width - 1, height);
g.setColor(Color.white);
// Line from bottom-left to bottom-right
g.drawLine(left, height, width, height);
// Line from top-right to bottom-right
g.drawLine(width, top, width, height);
}
} //All ends :)
The source code for the applet that uses this class is here. See the applet in action.
|