/* Simple clock applet demonstrating access from JavaScript
by Danny Goodman (http://www.dannyg.com)
(To modify and recompile, rename file to 'ScriptableClock.java')
*/
import java.awt.*;
import java.util.Date;
public class ScriptableClock extends java.applet.Applet implements Runnable {
// control variables
final boolean GMT = true;
final boolean LOCALE = false;
String displayDate;
Font displayFont;
Thread thread;
// parameter options
Color bgColor;
Color fgColor;
Rectangle displayArea;
String fontName;
int fontSize;
int fontStyle;
int height, width;
boolean timeZone;
public void init() {
parseArgs();
displayFont = new Font(fontName, fontStyle, fontSize);
timeZone = LOCALE;
displayArea = bounds();
height = displayArea.height;
width = displayArea.width;
resize(width,height);
}
public void start() {
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void stop() {
if(thread != null) {
thread.stop();
thread = null;
}
}
public void run() {
while(thread != null) {
Date theDate = new Date();
displayDate = (timeZone) ? theDate.toGMTString() : theDate.toLocaleString();
repaint();
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {}
}
}
public void paint(Graphics g) {
g.setColor(bgColor);
g.fillRect(0,0,width,height);
g.setColor(fgColor);
g.setFont(displayFont);
FontMetrics fm = getFontMetrics(getFont());
int textWidth = fm.stringWidth(displayDate);
int horizOffset = (width / 2) - (textWidth / 2);
g.drawString(displayDate,5,35);
}
/*
Begin public methods for getting
and setting data via LiveConnect
*/
public void setTimeZone(String zone) {
stop();
timeZone = (zone.startsWith("GMT")) ? true : false;
start();
}
public void setFont(String newFont, String newStyle, String newSize) {
stop();
if (newFont != null && newFont != "")
fontName = newFont;
if (newStyle != null && newStyle != "")
setFontStyle(newStyle);
if (newSize != null && newSize != "")
setFontSize(newSize);
displayFont = new Font(fontName, fontStyle, fontSize);
start();
}
public void setColor(String newbgColor, String newfgColor) {
stop();
bgColor = parseColor(newbgColor);
fgColor = parseColor(newfgColor);
start();
}
public String getInfo() {
String result = "Info about ScriptableClock.class\r\n";
result += "Version/Date: 1.0d1/2 May 1996\r\n";
result += "Author: Danny Goodman (dannyg@dannyg.com)\r\n";
result += "Public Variables:\r\n";
result += " (None)\r\n\r\n";
result += "Public Methods:\r\n";
result += " setTimeZone(\"GMT\" | \"Locale\")\r\n";
result += " setFont(\"fontName\",\"Plain\" |\"Bold\" | \"Italic\", \"fontSize\")\r\n";
result += " setColor(\"bgColorName\", \"fgColorName\")\r\n";
result += " colors: Black, White, Red, Green, Blue, Yellow\r\n";
return result;
}
/*
End public methods for scripted access.
*/
private void setFontStyle(String style) {
try {
if(style.equalsIgnoreCase("Plain"))
fontStyle = Font.PLAIN;
else if(style.equalsIgnoreCase("Italic"))
fontStyle = Font.ITALIC;
else
fontStyle = Font.BOLD;
}
catch(Exception e) {
fontStyle = Font.BOLD;
}
}
private void setFontSize(String size) {
try {
fontSize = Integer.parseInt(size);
}
catch(Exception e) {
fontSize = 24;
}
}
private void parseArgs() {
fontName = getParameter("font");
if (fontName == null) {
fontName = new String("TimesRoman");
}
String n = getParameter("fontSize");
setFontSize(n);
n = getParameter("fontStyle");
setFontStyle(n);
bgColor = parseColor(getParameter("bgColor"));
if(bgColor == null)
bgColor = Color.white;
fgColor = parseColor(getParameter("fgColor"));
if(fgColor == null)
fgColor = Color.black;
}
private Color parseColor(String c) {
try {
if (c.equalsIgnoreCase("Black"))
return Color.black;
else if (c.equalsIgnoreCase("White"))
return Color.white;
else if (c.equalsIgnoreCase("Red"))
return Color.red;
else if (c.equalsIgnoreCase("Green"))
return Color.green;
else if (c.equalsIgnoreCase("Blue"))
return Color.blue;
else if (c.equalsIgnoreCase("Yellow"))
return Color.yellow;
else
return Color.black;
}
catch (Exception e) {
return null;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVA SCRIPT 优秀教程 (540个子文件)
Chopin.aif 640KB
Scriabin.aif 389KB
Beethoven.aif 273KB
d.aif 93KB
e.aif 92KB
c.aif 88KB
cSharp.aif 84KB
dSharp.aif 84KB
ScriptableClock.class 4KB
FileReader.class 2KB
JtoJSDemo.class 2KB
lst18-13.css 50B
Academy Awards.csv 2KB
arch.gif 174KB
arch.gif 174KB
arch.gif 174KB
nile.gif 49KB
nile.gif 49KB
desk2.gif 11KB
desk2.gif 11KB
desk4.gif 11KB
desk4.gif 11KB
cpu2.gif 10KB
cpu1.gif 9KB
cpu1.gif 9KB
dhNav.gif 9KB
dhNav.gif 9KB
desk1.gif 9KB
desk1.gif 9KB
cpu3.gif 9KB
desk1.gif 9KB
desk1.gif 9KB
desk3.gif 8KB
desk3.gif 8KB
desk3.gif 8KB
desk3.gif 8KB
desk3.gif 8KB
logo.gif 7KB
cpu4.gif 7KB
CR_KRAFT.GIF 7KB
lightnin.gif 5KB
dhIcon.gif 5KB
dhIcon.gif 5KB
resright.gif 4KB
resleft.gif 3KB
ca.gif 2KB
camap.gif 2KB
camap.gif 2KB
sb23.gif 1KB
us11.gif 1KB
Black.gif 1KB
wh86.gif 1KB
nv.gif 1KB
az.gif 1KB
RightUp.gif 1024B
LeftDown.gif 1024B
RightDown.gif 1024B
LeftNorm.gif 1024B
RightNorm.gif 1024B
LeftUp.gif 1024B
RightDown.gif 1024B
LeftUp.gif 1024B
LeftDown.gif 1024B
RightUp.gif 1024B
LeftNorm.gif 1024B
RightNorm.gif 1024B
updated.gif 1024B
ormap.gif 991B
or.gif 991B
ormap.gif 991B
id.gif 929B
Gold.gif 915B
None.gif 907B
Violet.gif 886B
Yellow.gif 855B
wa.gif 836B
wamap.gif 836B
wamap.gif 836B
Orange.gif 826B
Red.gif 819B
ut.gif 806B
info.gif 781B
spacer.gif 765B
Brown.gif 697B
Gray.gif 670B
Silver.gif 668B
White.gif 666B
Blue.gif 646B
Green.gif 645B
collapsd.gif 520B
collapsd.gif 520B
daughter.gif 520B
expanded.gif 520B
daughter.gif 520B
expanded.gif 520B
collapsd.gif 520B
daughter.gif 520B
expanded.gif 520B
chart.gif 512B
chart.gif 512B
共 540 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
- xiaohuip212012-09-05只有代码啊
- sinat_185327412014-10-28不是真正意义的教程
- linghangujue2012-08-23明明是代码,楼主骗人~
刺猬老
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功