这是一个Java版的世界时钟示例,移植自Gerrit创建的同名Swing应用(http://www.jug-muenster.de/swing-worldclock-427 ),需要运行在JRE1.5或以上环境当中。
移植此示例主要是因为前一阵移植过Gerrit的swing原子钟示例,所谓好事成双,这个世界时钟的示例自然不能放过(话说Gerrit为什么那么喜欢做时钟?……),毕竟Java桌面应用实例本就不多,但凡有参考价值的还是要多做汇总才好。
另外,此示例中关于多时区的分辨与显示部分,在非桌面应用中也具备一定的参考价值。
PS:由于此示例以LGame-Simple-0.2.5开发,所以也可以看作是世界时钟的AWT实现。
运行代码如下所示(构建过程省略了Swing原版的一些步骤):
package org.loon.test;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import org.loon.framework.game.simple.GameScene;
import org.loon.framework.game.simple.core.graphics.Deploy;
import org.loon.framework.game.simple.core.graphics.Screen;
import org.loon.framework.game.simple.utils.GraphicsUtils;
/**
* Copyright 2008 - 2009
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @project loonframework
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class Main extends Screen {
// 此示例移植自http://www.jug-muenster.de/swing-worldclock-427/同名应用
public Main() {
// 设置窗体背景
this.setBackground(createBackgroundImage());
// 创建时间精灵
AnalogClock clock1 = new AnalogClock(City.Beijing, 25, 18, 74, 74);
AnalogClock clock2 = new AnalogClock(City.NewYork, 25, 108, 74, 74);
AnalogClock clock3 = new AnalogClock(City.SanFrancisco, 25, 198, 74, 74);
AnalogClock clock4 = new AnalogClock(City.Dakka, 25, 288, 74, 74);
// 添加精灵
this.add(clock1);
this.add(clock2);
this.add(clock3);
this.add(clock4);
}
public void draw(Graphics2D g) {
}
/**
* 创建背景图案
*
* @return
*/
private BufferedImage createBackgroundImage() {
final java.awt.image.BufferedImage IMAGE = GraphicsUtils.createImage(
getWidth(), getHeight(), java.awt.Transparency.TRANSLUCENT);
java.awt.Graphics2D g2 = IMAGE.createGraphics();
g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
java.awt.geom.Point2D BACKGROUND_START = new java.awt.geom.Point2D.Double(
0, 0);
java.awt.geom.Point2D BACKGROUND_STOP = new java.awt.geom.Point2D.Double(
0, getHeight());
final float[] BACKGROUND_FRACTIONS = { 0.0f, 1.0f };
final java.awt.Color[] BACKGROUND_COLORS = {
new java.awt.Color(0x505652), new java.awt.Color(0x393E3A) };
final java.awt.Shape BACKGROUND = new java.awt.geom.RoundRectangle2D.Double(
0, 0, getWidth(), getHeight(), 5, 5);
final java.awt.LinearGradientPaint BACKGROUND_GRADIENT = new java.awt.LinearGradientPaint(
BACKGROUND_START, BACKGROUND_STOP, BACKGROUND_FRACTIONS,
BACKGROUND_COLORS);
g2.setPaint(BACKGROUND_GRADIENT);
g2.fill(BACKGROUND);
java.awt.geom.Point2D INNER_BACKGROUND_START = new java.awt.geom.Point2D.Double(
0, 10);
java.awt.geom.Point2D INNER_BACKGROUND_STOP = new java.awt.geom.Point2D.Double(
0, getHeight() - 10);
final float[] INNER_BACKGROUND_FRACTIONS = { 0.0f, 1.0f };
final java.awt.Color[] INNER_BACKGROUND_COLORS = {
new java.awt.Color(0x4D5651), new java.awt.Color(0x393E3A) };
final java.awt.Shape INNER_BACKGROUND = new java.awt.geom.RoundRectangle2D.Double(
10, 10, getWidth() - 20, getHeight() - 20, 5, 5);
final java.awt.LinearGradientPaint INNER_BACKGROUND_GRADIENT = new java.awt.LinearGradientPaint(
INNER_BACKGROUND_START, INNER_BACKGROUND_STOP,
INNER_BACKGROUND_FRACTIONS, INNER_BACKGROUND_COLORS);
g2.setPaint(INNER_BACKGROUND_GRADIENT);
g2.fill(INNER_BACKGROUND);
g2.setColor(new java.awt.Color(0x2F362E));
g2.drawLine(10, 10, 10, getHeight() - 10);
g2.drawLine(getWidth() - 10, 10, getWidth() - 10, getHeight() - 10);
g2.drawLine(10, 10, getWidth() - 10, 10);
g2.setColor(new java.awt.Color(0x6B7167));
g2.drawLine(10, getHeight() - 10, getWidth() - 10, getHeight() - 10);
g2.dispose();
return IMAGE;
}
public void leftClick(MouseEvent e) {
}
public void middleClick(MouseEvent e) {
}
public void rightClick(MouseEvent e) {
}
public void onKey(KeyEvent e) {
}
public void onKeyUp(KeyEvent e) {
}
public static void main(String[] args) {
GameScene frame = new GameScene("Java版世界时钟", 398, 391);
Deploy deploy = frame.getDeploy();
deploy.setScreen(new Main());
deploy.setShowFPS(true);
deploy.setLogo(false);
deploy.setFPS(100);
deploy.mainLoop();
frame.showFrame();
}
}
以下为运行效果: