桌⾯时钟代码_⼆进制时钟代码实现(表驱动法)
在openSuse系统桌⾯⼩部件中有⼀个⼆进制时钟,当时虽然了解⼆进制但也看不懂,后来还在⽕车上还遇到有⼈带⼆进制⼿表,作为程序员的我感觉很酷呀ヾ( ∀ ゞ)
图1:Linux系统⼆进制时钟桌⾯部件(截图)
于是上⽹查了⼀下如何看⼆进制时钟,终于会看了ヾ(*´▽‘*)
图2:0~9⼆进制与⼗进制对照表
4⾏6列版的⼆进制时钟,其中每格代表⼆进制的0或1,每列从上到下(4格)代表⼀个⼆进制数对应的⼗进制数(如图1时间为22:06:40,其中第⼀列0010=2,第⼆列0010=2,第三列为0000=0,第四列为0110=6,第五列为0100=4,第六列0000=0)
会看⼆进制时钟后,就想怎么⽤代码实现⼆进制时钟,怎么把时间转换为⼆进制时钟显⽰出来。
Java swing ⼆进制时钟
实现思路是将时间HHmmss中的每⼀位代表的⼆进制放到数组中,再将所有数组组成4⾏6列的⼆维数组,之后根据⼆维数组在界⾯中展⽰就可以了;
例:时间 12:34:56,其中1转换为[0 ,0, 0, 1],2转换为[0010],3转换为[0011],4转换为[0100],5转换为[0101],6转换为[0110],之后放到4⾏6列的⼆维数组中,最后在界⾯中展⽰。
// 12:34:56 ⼆进制时钟⼆维数组
[
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 1]
[0, 1, 1, 0, 0, 1]
[1, 0, 1, 0, 1, 0]
]
//[1, 2, 3, 4, 5, 6]
第⼀版完整实现代码(Java swing)
import java.awt.Color;
import java.awt.Graphics;
SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
/**
* BinaryClock
* ---------------
* [ 1 2:3 4:5 6 ]
*  ---⼆进制时钟----
* [ 0 0 0 0 0 0 ]
凉粉的做法* [ 0 0 0 1 1 1 ]
* [ 0 1 1 0 0 1 ]
* [ 1 0 1 0 1 0 ]
* ---------------
* @author www@yiynx
* @version 0.0.5
*/
public class BinaryClock extends JPanel {
private static final long serialVersionUID = 7034146833797696044L;
private Timer timer = new Timer(); // 定时器
/
*
* 时间分量[timeComponent]
* 1 2 :3 4 :5 6
* h1h0:m1m0:s1s0
*/
private byte h1,h0,m1,m0,s1,s0 = 0;
/*
* 时间分量的⼆进制形式[timeComponentBinary]
* 1 2 :3 4 :5 6
* h1b h0b :m1b m0b :s1b s0b
* 00010010:00110100:01010110
*/
private byte[] h1Binary,h0Binary,m1Binary,m0Binary,s1Binary,s0Binary = new byte[4];
/*
*[1 2:3 4:5 6]
*
* 0 0 0 0 0 0
* 0 0 0 1 1 1
* 0 1 1 0 0 1
* 1 0 1 0 1 0
*/
private byte[][] binaryClock = new byte[4][6]; // 存储⼆进制时间的⼆维数组
private static final BinaryClock clockPanel = new BinaryClock(); // 显⽰⼆进制时间的⾯板
private static final int ROWS = 4; // ⾏数
private static final int COLS = 6; // 列数
private static final int CELL_SIZE = 29;  // 格⼦⼤⼩
private static final Color BACKGROUND_COLOR = new Color(0xc3d5ea);  // 背景颜⾊
private static final Color BORDER_COLOR = new Color(0x667799); // 边框颜⾊
private static final String OS_NAME = Property("os.name"); //"Linux","Windows 7"
/**
* 创建并启动⼆进制时钟。
*/
public void setUpRealBinaryClock() {
JFrame frame = new JFrame();
frameConfig(frame);
panelConfig(clockPanel);
frame.add(clockPanel);
clockPanel.action();
}
/**
* 配置窗⼝。
* @param frame
*/
private void frameConfig(final JFrame frame) {
frameTitleTrendsForCurrentTime(frame);
frameSizeCompatibility(frame);
frame.setLocationRelativeTo(null);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(null);
frame.setVisible(true);
}
/**
* 设置标题时间为系统当前时间,并以每秒24帧刷新显⽰。
* @param frame
*/
private void frameTitleTrendsForCurrentTime(final JFrame frame) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
timer.schedule(new TimerTask() { // 设置标题时间为系统当前时间,并以每秒24帧刷新显⽰      public void run() {
frame.setTitle(sdf.format(System.currentTimeMillis()));
}
}, 1000, 1000/24);
}
/**
* 不同系统对窗体⼤⼩显⽰的有区别,此处进⾏⼿动微调。
* @param frame
*/
private void frameSizeCompatibility(final JFrame frame) {
if(OS_NAME.equalsIgnoreCase("Windows 7")) {
frame.setSize(CELL_SIZE * 7-22, CELL_SIZE * 5);
} else if (OS_NAME.equalsIgnoreCase("Windows 10")) {
frame.setSize(CELL_SIZE * 7-13, CELL_SIZE * 5+10);
} else {
frame.setSize(CELL_SIZE * 7-22, CELL_SIZE * 5);
}
}
/
**
* 配置⾯板。世界各地风俗
* @param clockPanel
*/
private void panelConfig(BinaryClock clockPanel) {
clockPanel.setLocation(0, 0);
clockPanel.setSize(CELL_SIZE * 6 + 1, CELL_SIZE * 4 + 1);
clockPanel.setBorder(new LineBorder(BORDER_COLOR));
}
/**
* 绘制背景和⼆进制时钟(重写⽗类绘制⽅法)。
*/
public void paint(Graphics graphics) {
paintBackground(graphics);
paintRealBinaryClock(graphics);
}
/**
* 绘制⾯板背景颜⾊
* @param graphics
*/
private void paintBackground(Graphics graphics) {
graphics.setColor(BACKGROUND_COLOR);
graphics.fillRect(0, 0, getWidth(), getHeight());
端午节祝福短信大全}
完美女人的标准/**
* 绘制⼆进制时钟
* @param graphics文章和马伊琍的孩子
*/
private void paintRealBinaryClock(Graphics graphics) {
private void paintRealBinaryClock(Graphics graphics) {
int x, y;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
byte binaryClockElement = binaryClock[row][col];
x = col * CELL_SIZE;
y = row * CELL_SIZE;
if (binaryClockElement == 1) { // ⼆维数组中值为1的填充格⼦
graphics.setColor(Color.BLACK);
graphics.fillRect(x, y, CELL_SIZE, CELL_SIZE);
}
graphics.setColor(BORDER_COLOR);
graphics.drawRect(x, y, CELL_SIZE, CELL_SIZE);
}
}
}
/**
* 激活时间流程
*/
private void action() {
flowTime();
}
/**
* 时间流程
*/
private void flowTime() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
timeComponentTransition(); // 时间转换流程1:系统时间转化为时间分量(按系统时间)
timeComponentBinaryTransition(); // 时间转换流程2:时间分量转换成⼆进制形式
timeComponentBinaryClockTransition(); // 时间转换流程3:⼆进制形式时间分量到⼆维数组        repaint(); // 绘制界⾯与⼆进制时钟
}
}, 0, 1000/24);
}
/**
* 时间转换流程1:系统时间转化为时间分量(按系统当前时间)
*/
private void timeComponentTransition() {
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
char[] hhmmss = sdf.format(System.currentTimeMillis()).toCharArray();
h1 = (byte) (hhmmss[0] - '0');
h0 = (byte) (hhmmss[1] - '0');
m1 = (byte) (hhmmss[2] - '0');
m0 = (byte) (hhmmss[3] - '0');
s1 = (byte) (hhmmss[4] - '0');
s0 = (byte) (hhmmss[5] - '0');
}
/**
* 时间分量转换成⼆进制形式
*/
private void timeComponentBinaryTransition() {
h1Binary = toBinary(h1);
h0Binary = toBinary(h0);
m1Binary = toBinary(m1);
m0Binary = toBinary(m0);
s1Binary = toBinary(s1);
s0Binary = toBinary(s0);
}
/**
* 时间转换流程2:返回以参数的⼆进制形式为元素的数组(4位)
* @param hmsAny
* @return
*/
private byte[] toBinary(byte b) {
byte c = 1;
byte[] hmsAnyBinary = new byte[4];
hmsAnyBinary[3] = (byte) ((c << 0 & b) > 0 ? 1 : 0);
hmsAnyBinary[2] = (byte) ((c << 1 & b) > 0 ? 1 : 0);
hmsAnyBinary[1] = (byte) ((c << 2 & b) > 0 ? 1 : 0);
hmsAnyBinary[0] = (byte) ((c << 3 & b) > 0 ? 1 : 0);
return hmsAnyBinary;
}
/**
* 时间转换流程3:⼆进制形式时间分量到⼆维数组
*/
private void timeComponentBinaryClockTransition() {
for (int i = 0; i < 4; i++) {
binaryClock[i][0] = h1Binary[i];
binaryClock[i][1] = h0Binary[i];
binaryClock[i][2] = m1Binary[i];
binaryClock[i][3] = m0Binary[i];
binaryClock[i][4] = s1Binary[i];
binaryClock[i][5] = s0Binary[i];
}
}
/**
* 程序⼊⼝
*/
public static void main(String[] args) {
BinaryClock clock = new BinaryClock();
clock.setUpRealBinaryClock();
}
}
rpg角扮演
Java ⼆进制时钟
最后将Java打包⽣成的.jar⽂件打包成.exe⽂件。ヾ(o◕∀◕) ヾ
⼆进制时钟 下载链接 | 438 KB
看《代码⼤全2》时看到了表驱动⽅法(第18章 表驱动⽅法),ヽ(✿ ▽ )ノ 可以直接把0~9放的⼆进制放到⼆维数组中,不⽤把时间中的每⼀位都转换为⼆进制数组,再组成⼆维数组了 。ヾ§  ̄▽)ゞ2333333
⼆进制时钟主体代码-⽹页版(表驱动⽅法)