📄 countdown.java
字号:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
public class countdown extends Applet implements Runnable
{
URL dest ;
String saying, font ;
Thread timer ;
MediaTracker tracker ;
int lastsec, totalSec, t_hours, t_mins, t_secs, t_month, t_day,
t_year, t_daylight, tz_offset, rDays, rHours, rMinutes,
rSecs, digit_height, digit_width, colon_width,applet_width,
applet_height ;
int[] image_start_x ;
boolean tminus = false;
Image digit_image[], buffer_image, colon_image, frame_image,
tminus_image, tplus_image, blank_image ;
Graphics gc ;
public countdown()
{
digit_image = new Image[10];
digit_height = 21;
digit_width = 16;
colon_width = 9;
applet_width = 203;
applet_height = 42;
image_start_x = new int[14];
}
int getParameter(String s1, int s2)
{
String s = getParameter(s1) ;
return (s != null) ? Integer.parseInt(s) : s2 ;
}
String getParameter(String s1, String s2)
{
String s = getParameter(s1) ;
return (s != null) ? s : s2 ;
}
Color getParameter(String s1, Color s2)
{
String s = getParameter(s1);
return (s != null) ? new Color(Integer.parseInt(s, 16)):s2;
}
public void init()
{
tracker = new MediaTracker(this);
try
{
saying = getParameter("text");
font = getParameter("font");
try
{
dest = new URL(getDocumentBase(),
getParameter("dest", "http://127.0.0.1"));
}
catch(MalformedURLException _ex) {}
t_year = Integer.parseInt(getParameter("YEAR"));
t_month = Integer.parseInt(getParameter("MONTH"));
t_day = Integer.parseInt(getParameter("DAY"));
t_hours = getParameter("HOUR", 0);
t_mins = getParameter("MINUTE", 0);
t_secs = getParameter("SECOND", 0);
tz_offset = getParameter("TIMEZONE", 0);
t_daylight = getParameter("DAYLIGHT", 0);
}
catch(Exception _ex) { return; }
image_start_x[0] = 0;
image_start_x[1] = digit_width;
image_start_x[2] = digit_width * 2;
for(int i = 2; i < 14; i++)
if(i == 6 || i == 9 || i == 12)
image_start_x[i] = image_start_x[i-1]+colon_width;
else
image_start_x[i] = image_start_x[i-1]+digit_width;
for(int j = 0; j < 10; j++)
{
digit_image[j] = getImage(getCodeBase(),
j + font + ".gif");
tracker.addImage(digit_image[j], 0);
}
colon_image = getImage(getCodeBase(), "c_" + font + ".gif");
tracker.addImage(colon_image, 1);
tminus_image = getImage(getCodeBase(), "tm_"
+ font + ".gif");
tracker.addImage(tminus_image, 2);
tplus_image = getImage(getCodeBase(), "tp_"
+ font + ".gif");
tracker.addImage(tplus_image, 3);
blank_image = getImage(getCodeBase(), "b_"
+ font + ".gif");
tracker.addImage(blank_image, 4);
try
{
buffer_image = createImage(applet_width, applet_height);
gc = buffer_image.getGraphics();
return;
}
catch(Exception _ex) {}
gc = null;
}
public void paintDigClk(Graphics g)
{
int j = 0;
int i1 = 0;
Date date = new Date();
int i = date.getSeconds();
if(totalSec == -1) tminus = false;
if(tminus)
g.drawImage(tminus_image, image_start_x[i1++], 0, this);
else
g.drawImage(tplus_image, image_start_x[i1++], 0, this);
if(i != j)
{
totalSec--;
int l = Math.abs(totalSec);
rDays = (int)Math.floor((double)l / 86400D);
rHours = (int)Math.floor((double)(l % 0x15180) / 3600D);
rMinutes = (int)Math.floor((double)(l % 3600) / 60D);
rSecs = l % 60;
if(rDays / 1000 != 0)
g.drawImage(digit_image[rDays / 1000],
image_start_x[i1++], 0, this);
else
g.drawImage(blank_image, image_start_x[i1++],
0, this);
g.drawImage(digit_image[(rDays % 1000) / 100],
image_start_x[i1++], 0, this);
g.drawImage(digit_image[(rDays % 100) / 10],
image_start_x[i1++], 0, this);
g.drawImage(digit_image[rDays % 10],
image_start_x[i1++], 0, this);
g.drawImage(colon_image,
image_start_x[i1++], 0, this);
g.drawImage(digit_image[rHours / 10],
image_start_x[i1++], 0, this);
g.drawImage(digit_image[rHours % 10],
image_start_x[i1++], 0, this);
g.drawImage(colon_image, image_start_x[i1++], 0, this);
g.drawImage(digit_image[rMinutes / 10],
image_start_x[i1++], 0, this);
g.drawImage(digit_image[rMinutes % 10],
image_start_x[i1++], 0, this);
g.drawImage(colon_image, image_start_x[i1++], 0, this);
g.drawImage(digit_image[rSecs / 10],
image_start_x[i1++], 0, this);
g.drawImage(digit_image[rSecs % 10],
image_start_x[i1++], 0, this);
}
}
public void start()
{
int ai[][] = {
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int ai1[] = {366, 365, 365, 365};
int l2 = 0;
int i3 = 0;
int j3 = 0;
Date date = new Date();
int k = date.getSeconds();
int j = date.getMinutes();
int i = date.getHours();
int l = date.getMonth();
l++;
int i1 = date.getDate();
int j1 = date.getYear();
j3 = date.getTimezoneOffset();
j1 += 1900;
boolean flag = false;
int k2 = 0x15180;
i3 += t_secs;
i3 += t_mins * 60;
i3 += t_hours * 60 * 60;
i3 += (t_day - 1) * k2;
for(int k1 = 1; k1 < t_month; k1++)
i3 += ai[t_year % 4][k1 - 1] * k2;
i3 += tz_offset * 60;
for(int l1 = 1970; l1 < t_year; l1++)
i3 += ai1[l1 % 4] * k2;
if(t_daylight == 1) i3 -= 3600;
l2 += k;
l2 += j * 60;
l2 += i * 60 * 60;
l2 += (i1 - 1) * k2;
for(int i2 = 1; i2 < l; i2++)
l2 += ai[j1 % 4][i2 - 1] * k2;
for(int j2 = 1970; j2 < j1; j2++)
l2 += ai1[j2 % 4] * k2;
if(flag) l2 -= 3600;
l2 += tz_offset * 60;
totalSec = i3 - l2;
if(tz_offset != 0) totalSec += j3 * 60;
if(totalSec <= 0) tminus = false;
else tminus = true;
rDays = totalSec / 0x15180;
rHours = (totalSec % 0x15180) / 3600;
rMinutes = (totalSec % 3600) / 60;
rSecs = totalSec % 60;
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void stop()
{
timer = null;
}
public void run()
{
try
{
tracker.waitForID(0);
tracker.waitForID(1);
tracker.waitForID(2);
tracker.waitForID(3);
}
catch(InterruptedException _ex) { return; }
while(timer != null)
{
try
{
Thread.sleep(1000L);
}
catch(InterruptedException _ex) { }
repaint();
}
}
public void update(Graphics g)
{
if(buffer_image != null)
{
paintDigClk(gc);
g.drawImage(buffer_image, 0, 0, this);
return;
}
else
{
g.clearRect(0, 0, applet_width, applet_height);
paintDigClk(g);
return;
}
}
public boolean mouseEnter(Event event, int i, int j)
{
showStatus(saying);
return true;
}
public boolean mouseUp(Event event, int i, int j)
{
getAppletContext().showDocument(dest);
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -