标题:一个Clock程序(Applet & Application)
只看楼主
null
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2006-2-3
 问题点数:0 回复次数:4 
一个Clock程序(Applet & Application)
以下程序既可以作为applet 也可以作为Application, 供大家欣赏。
程序代码:

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Clock extends Applet implements Runnable
{
Thread thisThread;
Color faceColor, borderColor, minuteColor, hourColor, secondColor;
public void init()
{
//read in the colors for each of the hands and for the face/border
faceColor = readColor(getParameter(\"faceCol\"));
borderColor = readColor(getParameter(\"borderCol\"));
minuteColor = readColor(getParameter(\"minuteCol\"));
hourColor = readColor(getParameter(\"hourCol\"));
secondColor = readColor(getParameter(\"secondCol\"));
}

// This method creates a color based on a string.
// The string is assumed to be \"red, green, blue\" where each
// of the colors is represented by its integer equivalent.
public Color readColor(String aColor)
{
if(aColor == null)
return Color.black;
int r, g, b;

//break the string apart into each number
StringTokenizer st = new StringTokenizer(aColor, \",\");

try
{
r = Integer.valueOf(st.nextToken()).intValue();
g = Integer.valueOf(st.nextToken()).intValue();
b = Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e)
{
System.out.println(\"An exception occurred trying to convert a parameter to a color:\" + e);
return Color.black;
}
}
public String getParameter(String name)
{
String ST;
if(inApplet)
return super.getParameter(name);
//if you are not in an applet you default all of the values
if(name == \"hourcol\")
return \"255,00,00\";
if(name == \"minuteCol\")
return \"00,255,00\";
if(name == \"secondCol\")
return \"00,00,255\";
if(name == \"borderCol\")
return \"255,255,255\";
if(name == \"faceCol\")
return \"125,125,125\";
return null;
}
public void start()
{
thisThread = new Thread(this);
thisThread.start();
}

public void run()
{
while(true)
{
repaint();
try
{
thisThread.sleep(1000);
}catch(Exception e){}
}
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
//fill clock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);

//get the current time
Calendar d = Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle = (((double)(90 - d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle = ((((double)18 - d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40), 50+(int)(Math.cos(angle)*40));
//draw the second hand
g.setColor(secondColor);
angle = (((double)(90 - d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50), 50+(int)(Math.cos(angle)*50));
}
static boolean inApplet = true;
public static void main(String args[])
{
//set a boolean flag to show if you are in an applet or not
inApplet = false;

//create a frame to place our application in
//you can change the string value to show your desired label
//for the frame
Frame myFrame = new Frame(\"Clock as an Application\");
myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
//create a clock instance
Clock myApp = new Clock();

//add the current application to the frame
myFrame.add(\"Center\", myApp);

//resize the frame to the desired size and make it visible
myFrame.setSize(100,130);
myFrame.show();

//run the methods the browser normally would
myApp.init();
myApp.start();
}
}



html Code
程序代码:

<html>
<title>Clock</title>


<h1>Clock</h1>
<hr>
<applet code = \"Clock.class\" width=100 height=100>
<param name= hourCol value=255,00,00>
<param name= minuteCol value=00,255,00>
<param name= secondCol value=00,00,255>
<param name= borderCol value=255,255,255>
<param name= faceCol value=125,125,125>
</applet>
<hr>

</html>

[此贴子已经被作者于2006-4-7 22:53:49编辑过]

搜索更多相关主题的帖子: Applet Application Clock 
2006-04-07 22:51
null
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2006-2-3
得分:0 
show digital time in Statusbar

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Clock extends Applet implements Runnable
{
Thread thisThread;
Color faceColor, borderColor, minuteColor, hourColor, secondColor;
Calendar d;
public void init()
{
//read in the colors for each of the hands and for the face/border
faceColor = readColor(getParameter(\"faceCol\"));
borderColor = readColor(getParameter(\"borderCol\"));
minuteColor = readColor(getParameter(\"minuteCol\"));
hourColor = readColor(getParameter(\"hourCol\"));
secondColor = readColor(getParameter(\"secondCol\"));
}

// This method creates a color based on a string.
// The string is assumed to be \"red, green, blue\" where each
// of the colors is represented by its integer equivalent.
public Color readColor(String aColor)
{
if(aColor == null)
return Color.black;
int r, g, b;

//break the string apart into each number
StringTokenizer st = new StringTokenizer(aColor, \",\");

try
{
r = Integer.valueOf(st.nextToken()).intValue();
g = Integer.valueOf(st.nextToken()).intValue();
b = Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e)
{
System.out.println(\"An exception occurred trying to convert a parameter to a color:\" + e);
return Color.black;
}
}
public String getParameter(String name)
{
String ST;
if(inApplet)
return super.getParameter(name);
//if you are not in an applet you default all of the values
if(name == \"hourcol\")
return \"255,00,00\";
if(name == \"minuteCol\")
return \"00,255,00\";
if(name == \"secondCol\")
return \"00,00,255\";
if(name == \"borderCol\")
return \"255,255,255\";
if(name == \"faceCol\")
return \"125,125,125\";
return null;
}
public void start()
{
thisThread = new Thread(this);
thisThread.start();
}

public void run()
{
while(true)
{
repaint();
try
{
thisThread.sleep(1000);
}catch(Exception e){}
}
}

public void update(Graphics g)
{
paint(g);
if(inApplet)
{
int uhr = d.get(Calendar.HOUR_OF_DAY);
int minute = d.get(Calendar.MINUTE);
int sec = d.get(Calendar.SECOND);
String currentTime = new String(uhr + \":\" + minute + \":\" + sec);
showStatus(currentTime);
}
}

public void paint(Graphics g)
{
//fill clock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);

//get the current time
d = Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle = (((double)(90 - d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle = ((((double)18 - d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40), 50+(int)(Math.cos(angle)*40));
//draw the second hand
g.setColor(secondColor);
angle = (((double)(90 - d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50), 50+(int)(Math.cos(angle)*50));
}
static boolean inApplet = true;
public static void main(String args[])
{
//set a boolean flag to show if you are in an applet or not
inApplet = false;

//create a frame to place our application in
//you can change the string value to show your desired label
//for the frame
Frame myFrame = new Frame(\"Clock as an Application\");
myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
//create a clock instance
Clock myApp = new Clock();

//add the current application to the frame
myFrame.add(\"Center\", myApp);

//resize the frame to the desired size and make it visible
myFrame.setSize(100,130);
myFrame.show();

//run the methods the browser normally would
myApp.init();
myApp.start();
}
}



1. 坚决抵制日货 2. 批判共产主义,批判所谓的社会主义
2006-04-07 23:33
天涯海角
Rank: 2
等 级:新手上路
威 望:3
帖 子:507
专家分:0
注 册:2005-10-21
得分:0 
“批判共产主义,批判所谓的社会主义”、说这个话有罪!!!!

我的未来不是梦!
2006-04-09 20:51
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
得分:0 
这个Clock太粗糙了吧
建议求精

------Java 爱好者,论坛小混混,学习中------
2006-04-09 22:43
null
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2006-2-3
得分:0 
天涯海角,
你啊,真的很可怜啊!连思想的胆量都没有了!?

★王者至尊★,
你比3楼的高见一点。。。

1. 坚决抵制日货 2. 批判共产主义,批判所谓的社会主义
2006-04-11 01:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-55907-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.820677 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved