Оглавление |
Назад
// ==========================================
// CustomLabel.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.*;
import java.awt.*;
public class CustomLabel extends Applet
{
cLabel clblPlain;
cLabel clblLook3d;
cLabel clblSet;
// ===========================================
// init
// ===========================================
public void init()
{
clblPlain = new cLabel(this,
"Plain custom label", false);
clblLook3d = new cLabel(this,
"Look3d custom label", true);
clblSet = new cLabel(this, "", true);
clblSet.setText("Reshaping custom label");
add(clblPlain);
add(clblLook3d);
add(clblSet);
setBackground(new Color(200, 200, 200));
}
// ===========================================
// getAppletInfo
// ===========================================
public String getAppletInfo()
{
return
"Name: Custom Label, (C) Alexandr Frolov, 1998";
}
}
// ===========================================
// Class cLabel
// ===========================================
class cLabel extends Canvas
{
Container cont;
int nWindowWidth;
int nWindowHeight;
int x0;
int y0;
Dimension dimMinSize;
String lblTitle;
boolean look3d;
boolean bReshape = false;
// ===========================================
// cLabel
// ===========================================
public cLabel(Container parent,
String lblTitle, boolean look3d)
{
Graphics g;
cont = parent;
g = cont.getGraphics();
FontMetrics fm = g.getFontMetrics();
int nTitileWidth = fm.stringWidth(lblTitle);
int nTitleHeight = fm.getAscent() -
fm.getLeading() - fm.getDescent();
nWindowWidth = nTitileWidth + 20;
nWindowHeight = nTitleHeight + 20;
x0 = (nWindowWidth - nTitileWidth) / 2;
y0 = ((nWindowHeight - nTitleHeight) / 2) +
nTitleHeight;
this.lblTitle = lblTitle;
this.look3d = look3d;
dimMinSize = new Dimension(
nWindowWidth, nWindowHeight);
}
// ===========================================
// getPreferredSize
// ===========================================
public Dimension getPreferredSize()
{
return dimMinSize;
}
// ===========================================
// getMinimumSize
// ===========================================
public Dimension getMinimumSize()
{
return dimMinSize;
}
// ===========================================
// preferredSize
// ===========================================
public Dimension preferredSize()
{
return dimMinSize;
}
// ===========================================
// minimumSize
// ===========================================
public Dimension minimumSize()
{
return dimMinSize;
}
// ===========================================
// paint
// ===========================================
public void paint(Graphics g)
{
Color clr;
if(bReshape)
{
FontMetrics fm = g.getFontMetrics();
int nTitileWidth =
fm.stringWidth(lblTitle);
int nTitleHeight = fm.getAscent() -
fm.getLeading() - fm.getDescent();
nWindowWidth = nTitileWidth + 20;
nWindowHeight = nTitleHeight + 20;
x0 = (nWindowWidth - nTitileWidth) / 2;
y0 = ((nWindowHeight - nTitleHeight) / 2) +
nTitleHeight;
dimMinSize = new Dimension(
nWindowWidth, nWindowHeight);
resize(nWindowWidth, nWindowHeight);
cont.layout();
cont.repaint();
bReshape = false;
}
if(look3d)
{
clr = new Color(200, 200, 200);
g.setColor(clr);
g.fillRect(1, 1,
nWindowWidth - 2, nWindowHeight - 2);
clr = Color.white;
g.setColor(clr);
g.drawLine(0, 0, 0, nWindowHeight - 2);
g.drawLine(0, 0, nWindowWidth - 2, 0);
g.drawLine(2, nWindowHeight - 3,
nWindowWidth - 3, nWindowHeight - 3);
g.drawLine(nWindowWidth - 3, 2,
nWindowWidth - 3, nWindowHeight - 3);
clr = Color.black;
g.setColor(clr);
g.drawLine(0, nWindowHeight - 1,
nWindowWidth - 1, nWindowHeight - 1);
g.drawLine(nWindowWidth - 1, 0,
nWindowWidth - 1, nWindowHeight - 1);
g.drawLine(2, 2, 2, nWindowHeight - 3);
g.drawLine(2, 2, nWindowWidth - 3, 2);
}
else
{
clr = new Color(200, 200, 200);
g.setColor(clr);
g.fillRect(1, 1,
nWindowWidth - 2, nWindowHeight - 2);
clr = Color.black;
g.setColor(clr);
g.drawRect(0, 0,
nWindowWidth - 1, nWindowHeight - 1);
}
clr = Color.black;
g.setColor(clr);
g.drawString(lblTitle, x0, y0);
}
// ===========================================
// setText
// ===========================================
public void setText(String sz)
{
bReshape = true;
lblTitle = sz;
repaint();
}
// ===========================================
// getText
// ===========================================
public String getText()
{
return lblTitle;
}
}
|