Оглавление |
Назад
// ==========================================
// CustomButton.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
// ===========================================
// Class CustomButton
// ===========================================
public class CustomButton extends Applet
{
cButton btnRed;
cButton btnBlue;
cButton btnYellow;
Image imgButtonUp;
Image imgButtonDn;
// ===========================================
// init
// ===========================================
public void init()
{
setBackground(Color.yellow);
setForeground(Color.black);
imgButtonUp =
getImage(getCodeBase(), "btnup.gif");
imgButtonDn =
getImage(getCodeBase(), "btndn.gif");
btnRed = new cButton(this,
imgButtonUp, imgButtonDn,
"Red", 80, 20);
btnBlue = new cButton(this,
imgButtonUp, imgButtonDn,
"Blue", 80, 20);
btnYellow = new cButton(this,
imgButtonUp, imgButtonDn,
"Yellow", 80, 20);
add(btnRed);
add(btnBlue);
add(btnYellow);
}
// ===========================================
// getAppletInfo
// ===========================================
public String getAppletInfo()
{
return "Name: Custom Button";
}
// ===========================================
// doButtonAction
// ===========================================
public void doButtonAction(cButton btn)
{
if(btn.equals(btnRed))
setBackground(Color.red);
else if(btn.equals(btnBlue))
setBackground(Color.blue);
else if(btn.equals(btnYellow))
setBackground(Color.yellow);
repaint();
}
}
// ===========================================
// Class cButton
// ===========================================
class cButton extends Canvas
implements Runnable
{
Image imgButtonUp;
Image imgButtonDn;
Container cont;
int btnWidth;
int btnHeight;
Dimension dimMinSize;
boolean bButtonUp = true;
String btnTitle;
Thread threadAnimate = null;
Color clrTitleColor = Color.black;
// ===========================================
// cButton
// ===========================================
public cButton(Container parent,
Image imgUp, Image imgDn,
String btnTitle, int width, int height)
{
if(imgUp == null || imgDn == null)
{
System.err.println("Invalid image");
return;
}
imgButtonUp = imgUp;
imgButtonDn = imgDn;
cont = parent;
btnWidth = width;
btnHeight = height;
this.btnTitle = btnTitle;
dimMinSize = new Dimension(
btnWidth, btnHeight);
}
// ===========================================
// 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)
{
FontMetrics fm = g.getFontMetrics();
int nTitileWidth =
fm.stringWidth(btnTitle);
int nTitleHeight = fm.getAscent() -
fm.getLeading() - fm.getDescent();
int x0 = (btnWidth - nTitileWidth) / 2;
int y0 = ((btnHeight - nTitleHeight) / 2) +
nTitleHeight;
if(bButtonUp)
{
g.drawImage(imgButtonUp, 0, 0, this);
g.setColor(clrTitleColor);
g.drawString(btnTitle, x0, y0);
}
else
{
g.drawImage(imgButtonDn, 0, 0, this);
g.setColor(Color.blue);
g.drawString(btnTitle,
x0 - 1, y0 - 1);
}
}
// ===========================================
// mouseDown
// ===========================================
public boolean mouseDown(
Event ev, int x, int y)
{
if(bButtonUp)
{
bButtonUp = false;
repaint();
doButtonAction();
}
return true;
}
// ===========================================
// mouseUp
// ===========================================
public boolean mouseUp(
Event ev, int x, int y)
{
if(!bButtonUp)
{
bButtonUp = true;
repaint();
}
return true;
}
// ===========================================
// mouseEnter
// ===========================================
public boolean mouseEnter(
Event ev, int x, int y)
{
if(threadAnimate == null)
{
threadAnimate = new Thread(this);
threadAnimate.start();
}
return true;
}
// ===========================================
// mouseExit
// ===========================================
public boolean mouseExit(
Event ev, int x, int y)
{
if(threadAnimate != null)
{
threadAnimate.stop();
threadAnimate = null;
clrTitleColor = Color.black;
}
bButtonUp = true;
repaint();
return true;
}
// ===========================================
// run
// ===========================================
public void run()
{
boolean bFlipFlop = true;
while(true)
{
if(bFlipFlop)
{
clrTitleColor = Color.red;
bFlipFlop = false;
}
else
{
clrTitleColor = Color.black;
bFlipFlop = true;
}
repaint();
try
{
Thread.sleep(300);
}
catch (InterruptedException ex)
{
threadAnimate.stop();
}
}
}
// ===========================================
// doButtonAction
// ===========================================
public void doButtonAction()
{
((CustomButton)cont).doButtonAction(this);
}
}
|