📄 capplet.java
字号:
package aclock;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Date;
import java.net.URL;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class CApplet
extends Applet
implements Runnable {
Thread timer = null;
int lxS, lyS, lxM, lyM, lxH, lyH;
int xCenter = 100, yCenter = 100;
int radius = 80;
int xToday = 45, yToday = 220;
Image img;
MediaTracker mediaTracker = new MediaTracker(this);
Panel panel1 = new Panel();
BorderLayout borderLayout1 = new BorderLayout();
Button button1 = new Button();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
TextField inHour = new TextField();
TextField inMin = new TextField();
TextField inSec = new TextField();
int AHour,AMin,ASec;
AudioClip au;
//Construct the applet
public CApplet() {
}
//Initialize the applet
public void init() {
try {
jbInit();
imgInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
button1.setLabel("确 定");
button1.setLocale(java.util.Locale.getDefault());
button1.addActionListener(new CApplet_button1_actionAdapter(this));
label1.setText("时");
label2.setText("分");
label3.setText("秒");
inHour.setColumns(2);
//textField2.setCaretPosition(10);
inMin.setColumns(2);
inMin.setText("");
inSec.setColumns(2);
inSec.setText("");
this.add(panel1, BorderLayout.SOUTH);
panel1.add(label1, null);
panel1.add(inHour, null);
panel1.add(label2, null);
panel1.add(inMin, null);
panel1.add(label3, null);
panel1.add(inSec, null);
panel1.add(button1, null);
}
void imgInit() {
au = getAudioClip(getCodeBase(), "au/alarm.au");
img = getImage(getDocumentBase(), "img/clock.jpg");
//装载总的大图片
mediaTracker.addImage(img, 0);
try {
mediaTracker.waitForAll();
}
catch (Exception e) {
System.out.println("图片装载出错");
}
}
public void drawScale(Graphics g) {
int xHour, yHour, xMinute, yMinute;
g.setColor(Color.yellow);
for (int i = 0; i < 12; i++) {
xHour = (int) (Math.cos( (i * 30) * 3.14f / 180 - 3.14f / 2) * radius +
xCenter);
yHour = (int) (Math.sin( (i * 30) * 3.14f / 180 - 3.14f / 2) * radius +
yCenter);
g.fill3DRect(xHour - 3, yHour - 3, 6, 6, true);
}
for (int i = 0; i < 60; i++) {
xMinute = (int) (Math.cos( (i * 30) * 3.14f / 180 - 3.14f / 2) * radius +
xCenter);
yMinute = (int) (Math.sin( (i * 30) * 3.14f / 180 - 3.14f / 2) * radius +
yCenter);
g.fill3DRect(xMinute - 1, yMinute - 1, 2, 2, true);
}
}
public void paint(Graphics g) {
int xHour, yHour, xMinute, yMinute, xSecond, ySecond, second, minute, hour;
String today;
g.drawImage(img, 0, 0, this);
this.drawScale(g);
Date date = new Date();
second = date.getSeconds();
minute = date.getMinutes();
hour = date.getHours();
today = date.toLocaleString();
xSecond = (int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * radius +
xCenter);
ySecond = (int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * radius +
yCenter);
xMinute = (int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * (radius - 10) +
xCenter);
yMinute = (int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * (radius - 10) +
yCenter);
xHour = (int) (Math.cos( (hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) *
(radius - 30) + xCenter);
yHour = (int) (Math.sin( (hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) *
(radius - 30) + yCenter);
g.setColor(Color.white);
g.drawString(today, xToday, yToday);
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
g.setColor(Color.green);
g.drawLine(xCenter, yCenter - 2, xMinute, yMinute);
g.drawLine(xCenter - 2, yCenter, xMinute, yMinute);
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter - 3, xHour, yHour);
g.drawLine(xCenter - 3, yCenter, xHour, yHour);
if(AHour == hour && AMin == minute && ASec == second){
au.play();
}
}
public void start() {
if (timer == null) {
timer = new Thread(this);
timer.start();
}
}
public void stop() {
timer = null;
}
public void run() {
while (true) {
try {
timer.sleep(100);
}
catch (Exception e) {}
//this.repaint();
this.update(this.getGraphics());
//this.paint(this.getGraphics());
}
}
void button1_actionPerformed(ActionEvent e) {
if(inHour.getText() != null)
AHour = Integer.parseInt(inHour.getText());
else AHour = 0;
if(inMin.getText() != null)
AMin = Integer.parseInt(inMin.getText());
else AMin = 0;
if(inSec.getText() != null)
ASec = Integer.parseInt(inSec.getText());
else ASec = 0;
}
}
class CApplet_button1_actionAdapter implements java.awt.event.ActionListener {
CApplet adaptee;
CApplet_button1_actionAdapter(CApplet adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -