Оглавление |
Назад
// ==========================================
// GrayFilter.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
public class GrayFilter 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");
MediaTracker mt = new MediaTracker(this);
mt.addImage(imgButtonUp, 0);
mt.addImage(imgButtonDn, 0);
try
{
mt.waitForAll();
}
catch(InterruptedException ie)
{
return;
}
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: GrayFilter";
}
// ===========================================
// 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
{
Image imgButtonUp;
Image imgButtonDn;
Container cont;
int btnWidth;
int btnHeight;
Dimension dimMinSize;
boolean bButtonUp = true;
boolean bMouseEnter = false;
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)
{
if(bButtonUp)
{
g.setColor(clrTitleColor);
if(!bMouseEnter)
drawButton(imgButtonUp, 0);
else
drawButton(imgToGray(imgButtonUp), 0);
}
else
{
g.setColor(Color.blue);
drawButton(imgToGray(imgButtonDn), 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)
{
bMouseEnter = true;
drawButton(imgToGray(imgButtonUp), 0);
return true;
}
// ===========================================
// mouseExit
// ===========================================
public boolean mouseExit(
Event ev, int x, int y)
{
bMouseEnter = false;
drawButton(imgButtonUp, 0);
return true;
}
// ===========================================
// doButtonAction
// ===========================================
public void doButtonAction()
{
((GrayFilter)cont).doButtonAction(this);
}
// ===========================================
// drawButton
// ===========================================
void drawButton(Image imgSrc, int offset)
{
Graphics g = getGraphics();
g.drawImage(imgSrc, 0, 0, this);
drawTitle(g, offset);
}
// ===========================================
// drawTitle
// ===========================================
void drawTitle(Graphics g, int offset)
{
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;
g.drawString(btnTitle,
x0 - offset, y0 - offset);
}
// ===========================================
// imgToGray
// ===========================================
Image imgToGray(Image imgSrc)
{
GrayImageFilter grFilter =
new GrayImageFilter();
ImageProducer ip = new FilteredImageSource(
imgSrc.getSource(), grFilter);
Image imGrayed = createImage(ip);
MediaTracker mt = new MediaTracker(this);
mt.addImage(imGrayed, 0);
try
{
mt.waitForAll();
}
catch(InterruptedException ie)
{
return null;
}
return imGrayed;
}
}
// ===========================================
// Class GrayImageFilter
// ===========================================
class GrayImageFilter extends RGBImageFilter
{
// ===========================================
// filterRGB
// ===========================================
public int filterRGB(int x, int y, int nRGB)
{
int nR = (nRGB >> 16) & 0xff;
int nG = (nRGB >> 8) & 0xff;
int nB = nRGB & 0xff;
int nGray = (int)(0.56 * nG + 0.33 * nR +
0.11 * nB);
return (0xff000000 | nGray << 16 |
nGray << 8 | nGray);
}
}
|