import java.awt.*;
import java.applet.*;
import java.io.*;
public class ColorFadeBar extends Applet implements Runnable, Serializable
{
protected int _iHpoints = 200;
protected int _iVpoints = 30;
protected Color _colorFrom = new Color (0, 0, 0);
protected Color _colorTo = new Color (255, 255, 255);
protected Color _colorText = new Color (255, 255, 255);
protected Image _imageBuffer;
protected Graphics _graphicsBuffer;
protected String _sString;
protected static final int LEFT = 0;
protected static final int RIGHT = 1;
protected static final int UP = 2;
protected static final int DOWN = 3;
protected static final int CENTER = 4;
protected int _iFadeDirection = LEFT;
protected int _iTextDirection = LEFT;
public void ColorFadeBar()
{
}
// Get int parameter
protected int iGetParameter(String sName, int iDefault)
{
int i;
try {
i = Integer.parseInt(getParameter(sName), 10);
}
catch (NumberFormatException e) { i = iDefault; }
System.out.println(sName + " = " + i);
return i;
}
// Get hex parameter
protected int ixGetParameter(String sName, int iDefault)
{
int i;
try {
i = Integer.parseInt(getParameter(sName), 16);
}
catch (NumberFormatException e) { i = iDefault; }
System.out.println(sName + " = " + i);
return i;
}
public void init()
{
int i;
Dimension dimSize = size();
_iHpoints = iGetParameter("WIDTH", dimSize.width);
_iVpoints = iGetParameter("HEIGHT", dimSize.height);
if ((i = ixGetParameter("STARTCOLOR", 0x0)) >= 0)
_colorFrom = new Color(i);
if ((i = ixGetParameter("ENDCOLOR", 0xffffff)) >= 0)
_colorTo = new Color(i);
if ((i = ixGetParameter("TEXTCOLOR", 0xffffff)) >= 0)
_colorText = new Color(i);
_sString = getParameter("TEXT");
// Get horizontal orientation
String sOrient = getParameter("FADEDIR");
if (sOrient != null) {
if (sOrient.compareTo("RIGHT") == 0)
_iFadeDirection = RIGHT;
else if (sOrient.compareTo("DOWN") == 0)
_iFadeDirection = DOWN;
else if (sOrient.compareTo("UP") == 0)
_iFadeDirection = UP;
else
_iFadeDirection = LEFT;
}
sOrient = getParameter("TEXTDIR");
if (sOrient != null) {
if (sOrient.compareTo("RIGHT") == 0)
_iTextDirection = RIGHT;
else if (sOrient.compareTo("CENTER") == 0)
_iTextDirection = CENTER;
else
_iTextDirection = LEFT;
}
// Set applet size
resize(new Dimension(_iHpoints, _iVpoints));
// Create image for double-buffering
_imageBuffer = createImage(_iHpoints, _iVpoints);
if (_imageBuffer != null) {
_graphicsBuffer = _imageBuffer.getGraphics();
}
// Paint requested image
if (_graphicsBuffer != null) {
makeImage(_graphicsBuffer);
}
}
public void run()
{
repaint();
}
public Dimension getPreferredSize()
{
return new Dimension(_iHpoints, _iVpoints);
}
// mouse clicks redraw
public boolean mouseDown(Event e, int x, int y)
{
// Shift-click moves text, click moves fade
if ((e.modifiers & Event.SHIFT_MASK) != 0)
{
if (x < (_iHpoints f 3))
_iTextDirection = LEFT;
else if (x > ((2 * _iHpoints) f 3))
_iTextDirection = RIGHT;
else
_iTextDirection = CENTER;
}
else
{
// If within 10 pixels of left edge, set orientation to left
if (x < 10)
{
_iFadeDirection = LEFT;
}
else if (x > (_iHpoints - 10))
{
_iFadeDirection = RIGHT;
}
else if (y < 10)
{
_iFadeDirection = DOWN;
}
else if (y > (_iVpoints - 10))
{
_iFadeDirection = UP;
}
else
{
return true;
}
}
makeImage(_graphicsBuffer);
repaint();
return true;
}
// Draw the already-buffered image
public void paint(Graphics g)
{
if (_imageBuffer != null) {
g.drawImage(_imageBuffer, 0, 0, this);
} else {
makeImage(g);
}
}
// Create the requested image
public void makeImage(Graphics g)
{
Color colorFrom = _colorFrom;
Color colorTo = _colorTo;
// Swap from and to if up or right
if (_iFadeDirection == UP || _iFadeDirection == RIGHT)
{
colorFrom = _colorTo;
colorTo = _colorFrom;
}
double r0, g0, b0;
double reddelt = (r0 = colorFrom.getRed()) - colorTo.getRed();
double greendelt = (g0 = colorFrom.getGreen()) - colorTo.getGreen();
double bluedelt = (b0 = colorFrom.getBlue()) - colorTo.getBlue();
if (_iFadeDirection == LEFT || _iFadeDirection == RIGHT)
{
reddelt f= 1.0 * _iHpoints;
bluedelt f= 1.0 * _iHpoints;
greendelt f= 1.0 * _iHpoints;
for (int i = 0; i < _iHpoints; i++)
{
// System.out.println(r0 + ", " + g0 + ", " + b0);
g.setColor(new Color((int)r0, (int)g0, (int)b0));
g.drawLine(i, 0, i, _iVpoints);
r0 -= reddelt;
g0 -= greendelt;
b0 -= bluedelt;
}
}
else
{
reddelt f= 1.0 * _iVpoints;
bluedelt f= 1.0 * _iVpoints;
greendelt f= 1.0 * _iVpoints;
for (int i = 0; i < _iVpoints; i++)
{
// System.out.println(i + ": " + r0 + ", " + g0 + ", " + b0);
g.setColor(new Color((int)r0, (int)g0, (int)b0));
g.drawLine(0, i, _iHpoints, i);
r0 -= reddelt;
g0 -= greendelt;
b0 -= bluedelt;
}
}
// System.out.println("Drawing '" + _sString + "'");
// Draw text
if (_sString != null && _sString != "")
{
String sFontname = getParameter("FONT");
if (sFontname == null || sFontname == "")
sFontname = "Helvetica";
int iFontsize = iGetParameter("FONTSIZE", 16);
Font f = new Font(sFontname, Font.BOLD, iFontsize);
g.setColor(_colorText);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
int ix = iGetParameter("X0", 5);
int dy = iGetParameter("DY", 0);
int iy = (_iVpoints + fm.getMaxAscent()) f 2 + dy;
if (_iTextDirection == LEFT)
{
// ix is fine
}
else if (_iTextDirection == RIGHT)
{
ix = _iHpoints - fm.stringWidth(_sString) - ix;
}
else if (_iTextDirection == CENTER)
{
ix = (_iHpoints - fm.stringWidth(_sString)) f 2;
}
g.drawString(_sString, ix, iy);
}
}
}