?? clock.java~6~
字號:
package untitled1;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Date;public class clock extends Applet implements Runnable{ Thread timer = null; int lastXSecond = 0; int lastYSecond = 0; int lastXMinute = 0; int lastYMinute = 0; int lastXHour = 0; int lastYHour = 0; int xCenter = 100; int yCenter = 100; int radius = 80; int xToday = 45; int yToday = 220; Date dummy = new Date(); String lastDate = dummy.toLocaleString(); boolean isStandalone = false; /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public clock() { } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } public void paint(Graphics g){ int xHour,yHour,xMinute,yMinute,xSecond,ySecond,second,minute,hour; String today; 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-5)+xCenter); yMinute = (int)(Math.sin(minute *3.14f/30 - 3.14f/2)*(radius-5)+yCenter); xHour = (int)(Math.cos((hour*30+minute/2)*3.14f/180-3.14f/2)*(radius-15)+xCenter); yHour = (int)(Math.sin((hour*30+minute/2)*3.14f/180-3.14f/2)*(radius-15)+yCenter); if (xSecond != lastXSecond ||ySecond != lastYSecond){ g.drawLine(xCenter,yCenter,lastXSecond,lastYSecond); g.drawString(lastDate,xToday,yToday); } if (xMinute!=lastXMinute || yMinute != lastYMinute){ g.drawLine(xCenter,yCenter-1,lastXMinute,lastYMinute) ; g.drawLine(xCenter-1,yCenter,lastXMinute,lastYMinute) ; } if (xHour != lastXHour||yHour != lastYHour){ g.drawLine(xCenter,yCenter-1,lastXHour,lastYHour); g.drawLine(xCenter-1,yCenter,lastXHour,lastYHour); } g.setColor(Color.blue); g.drawString(today,xToday,yToday); g.setColor(Color.pink); g.drawLine(xCenter,yCenter,xSecond,ySecond) ; g.setColor(Color.orange) ; g.drawLine(xCenter,yCenter-1,xMinute,yMinute); g.drawLine(xCenter-1,yCenter,xMinute,yMinute); g.setColor(Color.green); g.drawLine(xCenter,yCenter-1,xHour,yHour); g.drawLine(xCenter-1,yCenter,xHour,yHour); lastXSecond = xSecond; lastYSecond = ySecond; lastXMinute = xMinute; lastYMinute = yMinute; lastXHour = xHour; lastYHour = yHour; lastDate = today; } public void drawScale(Graphics g){ int xHour,yHour,xMinute,yMinute; g.setColor(Color.darkGray); 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.draw3DRect(xHour - 2,yHour - 2,4,4,true); } g.setColor(Color.gray); for (int j = 0;j<60;j++){ xMinute = (int)(Math.cos(j*3.14f/30-3.14f/2)*radius + xCenter); yMinute = (int)(Math.sin(j*3.14f/30-3.14f/2)*radius + yCenter); g.draw3DRect(xMinute-1,yMinute-1,2,2,true); } } public void start(){ if(timer == null){ timer = new Thread(this); timer.start() ; } } public void stop(){ timer = null; } public void run(){ while (timer != null){ try { Thread.sleep(100); } catch (InterruptedException e){} } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -