home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 March
/
PCWK0397.iso
/
novell
/
webserv3
/
wstkb1.exe
/
DOCS
/
TOOLS
/
JAVA
/
COUNTER
/
COUNTER.JAV
< prev
next >
Wrap
Text File
|
1996-12-10
|
3KB
|
72 lines
import java.awt.*;
import java.net.*;
import java.lang.Math;
import java.lang.Integer;
import java.util.Date;
public class Counter extends java.applet.Applet
{
Color bgcolor,
fontcolor;
String count;
public Color nameToColor(String name) { //This function converts a color name String to a Color
//This takes a string form of a color name and converts it to a color
Color color;
if (name.equalsIgnoreCase("blue")) color = Color.blue;
else if (name.equalsIgnoreCase("cyan")) color = Color.cyan;
else if (name.equalsIgnoreCase("darkGray")) color = Color.darkGray;
else if (name.equalsIgnoreCase("darkGrey")) color = Color.darkGray;
else if (name.equalsIgnoreCase("gray")) color = Color.gray;
else if (name.equalsIgnoreCase("grey")) color = Color.gray;
else if (name.equalsIgnoreCase("green")) color = Color.green;
else if (name.equalsIgnoreCase("lightGray")) color = Color.lightGray;
else if (name.equalsIgnoreCase("lightGrey")) color = Color.lightGray;
else if (name.equalsIgnoreCase("magenta")) color = Color.magenta;
else if (name.equalsIgnoreCase("orange")) color = Color.orange;
else if (name.equalsIgnoreCase("pink")) color = Color.pink;
else if (name.equalsIgnoreCase("red")) color = Color.red;
else if (name.equalsIgnoreCase("white")) color = Color.white;
else if (name.equalsIgnoreCase("yellow")) color = Color.yellow;
else color = Color.black;
return color;
} // end of Counter.Color(String)
public void init() { //init routine gets variables and initializes variables
String tstr;
Integer tmpcount;
// Determine the count value
tstr = getParameter("count");
if (tstr == null)
count = String.valueOf(0);
else {
tmpcount = new Integer(tstr);
count = String.valueOf(tmpcount.intValue());
}
// Determine the background color
tstr = getParameter("bgcolor");
if (tstr == null) tstr="white";
this.bgcolor = nameToColor(tstr);
// Determine the font color
tstr = getParameter("fontcolor");
if (tstr == null) tstr="black";
this.fontcolor = nameToColor(tstr);
} // end of public void Counter.init()
public void paint(Graphics g) { //paint routine for applet
int fontsize = this.size().height < this.size().width ?
this.size().height-1 : this.size().width-1;
Font f = new Font("TimesRoman", Font.PLAIN, fontsize);
FontMetrics fm = getFontMetrics(f);
int strwidth = fm.stringWidth(count) + 4;
g.setColor(this.bgcolor);
g.fillRect(0, 0, this.size().width-1, this.size().height-1);
g.setColor(this.fontcolor);
g.setFont(f);
g.drawString(count, this.size().width - strwidth,
this.size().height / 2 + fm.getDescent());
} // end of public void Counter.paint(Graphics)
} // end of public class Counter