package com.view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
public class MyLabel extends JLabel {
private String date;
private int fontLength= MyRect.getFontLength();
private int fontWeigh = MyRect.getFontWeigth();
private int space = MyRect.getSpace();
public MyLabel(){
new Thread(new ThreadDate()).start();
this.addMouseListener(new MouseHandle());
super.setSize(MyRect.getFontLength()*19, MyRect.getFontLength()*2+MyRect.getFontWeigth()*7);
}
public void paint(Graphics g) {
g.setColor(MyRect.getBgColor());
g.fillRect(0, 0,MyRect.getFontLength()*19, MyRect.getFontLength()*2+MyRect.getFontWeigth()*7);
g.setColor(MyRect.getColor());
s(g);
g.dispose();
}
private class MouseHandle implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int count = e.getClickCount();
if(e.getButton() == MouseEvent.BUTTON1 && count==2){
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public class ThreadDate implements Runnable{
public void run() {
while(true){
SimpleDateFormat f = new SimpleDateFormat("hh:mm:ss:SS");
date = f.format(new Date());
repaint();
}
}
}
public void s(Graphics g){
int left = MyRect.getFontLength()*2;
int top = MyRect.getFontWeigth()*2;
for(int i=0;i<date.length();++i){
String str = date.substring(i, i+1);
if(str.equals("1")){
this.paintNumber1(g, left+space*(i-1), top);
}else if(str.equals("2")){
this.paintNumber2(g, left+space*(i-1), top);
}
else if(str.equals("3")){
this.paintNumber3(g,left+space*(i-1), top);
}
else if(str.equals("4")){
this.paintNumber4(g,left+space*(i-1), top);
}
else if(str.equals("5")){
this.paintNumber5(g, left+space*(i-1), top);
}
else if(str.equals("6")){
this.paintNumber6(g, left+space*(i-1), top);
}
else if(str.equals("7")){
this.paintNumber7(g, left+space*(i-1), top);
}
else if(str.equals("8")){
this.paintNumber8(g, left+space*(i-1), top);
}
else if(str.equals("9")){
this.paintNumber9(g, left+space*(i-1), top);
}
else if(str.equals("0")){
this.paintNumber0(g,left+space*(i-1), top);
}else{
this.paintDot(g, left+space*(i-1), top);
}
}
}
public void paintNumber0(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
this.paintStroke(g, x, y, 5);
this.paintStroke(g, x, y, 6);
}
public void paintNumber1(Graphics g,int x,int y){
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
}
public void paintNumber2(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 7);
this.paintStroke(g, x, y, 5);
this.paintStroke(g, x, y, 4);
}
public void paintNumber3(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 7);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
}
public void paintNumber4(Graphics g,int x,int y){
this.paintStroke(g, x, y, 6);
this.paintStroke(g, x, y, 7);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
}
public void paintNumber5(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 6);
this.paintStroke(g, x, y, 7);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
}
public void paintNumber6(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 6);
this.paintStroke(g, x, y, 7);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
this.paintStroke(g, x, y, 5);
}
public void paintNumber7(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
}
public void paintNumber8(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
this.paintStroke(g, x, y, 5);
this.paintStroke(g, x, y, 6);
this.paintStroke(g, x, y, 7);
}
public void paintNumber9(Graphics g,int x,int y){
this.paintStroke(g, x, y, 1);
this.paintStroke(g, x, y, 2);
this.paintStroke(g, x, y, 3);
this.paintStroke(g, x, y, 4);
this.paintStroke(g, x, y, 6);
this.paintStroke(g, x, y, 7);
}
public void paintNumberDot(Graphics g,int x,int y){
this.paintStroke(g, x, y, 4);
}
/**
* 最上面开始,顺时针标注,,最上面是1,中间是7,笔画
* @param g
* @param x1
* @param y1
*/
protected void paintStroke(Graphics g,int x,int y,int strokeType){
int x1 = 0,y1=0;
boolean isHorizontalLine = true;
switch(strokeType){
case 1:x1 = x;y1 =y;isHorizontalLine = true;break;
case 2:x1 = x + fontLength;y1 =y+fontWeigh;isHorizontalLine = false;break;
case 3:x1 = x+fontLength;y1 =y+fontLength+2*fontWeigh;isHorizontalLine = false;break;
case 4:x1 = x;y1 =y + 2*fontLength+2*fontWeigh;isHorizontalLine = true;break;
case 5:x1 = x-fontWeigh;y1 =y + fontLength+2*fontWeigh;isHorizontalLine = false;break;
case 6:x1 = x-fontWeigh;y1 =y+fontWeigh;isHorizontalLine = false;break;
case 7:x1 = x;y1 =y + fontLength+fontWeigh;isHorizontalLine = true;break;
}
if(isHorizontalLine){
this.paintHorizontalLine(g, x1, y1);
}else{
this.paintVerticalLine(g, x1, y1);
}
}
void paintVerticalLine(Graphics g,int x,int y){
for(int i=0;i<fontWeigh;++i){
g.drawLine(x+i, y, x+i, y+fontLength);
}
}
void paintHorizontalLine(Graphics g,int x,int y){
for(int i=0;i<fontWeigh;++i){
g.drawLine(x, y+i, x+fontLength, y+i);
}
}
void paintDot(Graphics g,int x,int y){
g.fillOval(x+fontLength/3, y+fontLength/3+fontWeigh, fontLength/3, fontLength/3);
g.fillOval(x+fontLength/3, y+4*fontLength/3+2*fontWeigh, fontLength/3,fontLength/3);
}
}