?? digitaldisplay.java
字號:
/*
該類實現(xiàn)顯示數(shù)字的功能
*/
import java.awt.*;
import java.util.*;
public class digitaldisplay extends Canvas {
int VerLedx[], VerLedy[], HorLedx[], HorLedy[];
int CifraXP, CifraYP; // 所有數(shù)字的x,y 位置
int CifraVal[]; // 用來顯示
boolean BigClock; // 判斷是否到時間
String Led[]; // leds的狀態(tài)
String Cifre[]; // 所有10個數(shù)字的結(jié)構(gòu)
Color bgColor; // 背景色
Color fgColor; // 前景色
String strValueDisplayed;
int NumCifre;
static int MAXNUMCIFRE=10;
//構(gòu)造函數(shù)
public digitaldisplay(boolean BigClock, int NumCifre, Color bgColor, Color fgColor) {
this.BigClock=BigClock;
this.NumCifre=NumCifre;
this.bgColor=bgColor;
this.fgColor=fgColor;
CifraVal=new int[MAXNUMCIFRE];
if (BigClock == true) {
VerLedx = getParameterIntList("2 3 5 5 3 2 0 0 2", ' ');
VerLedy = getParameterIntList("0 0 2 17 19 19 17 2 0", ' ');
HorLedx = getParameterIntList("2 17 19 19 17 2 0 0 2", ' ');
HorLedy = getParameterIntList("0 0 2 3 5 5 3 2 0", ' ');
Led = getParameterList("H 4 0,V 0 4,V 22 4,H 4 22,V 0 26,V 22 26,H 4 44", ',');
CifraYP = 5;
CifraXP = 37;
resize(new Dimension(CifraXP*NumCifre, 60));
} else {
VerLedx = getParameterIntList("0 1 2 3 3 2 1 0 0", ' ');
VerLedy = getParameterIntList("1 0 0 1 8 9 9 8 1", ' ');
HorLedx = getParameterIntList("0 1 8 9 9 8 1 0 0", ' ');
HorLedy = getParameterIntList("1 0 0 1 2 2 3 2 1", ' ');
Led = getParameterList("H 3 0,V 0 3,V 12 3,H 3 12,V 0 15,V 12 15,H 3 24", ',');
CifraYP = 3;
CifraXP = 20;
resize(new Dimension(CifraXP*NumCifre-CifraYP, 32));
}
Cifre = new String[10];
Cifre[0] = "1 1 1 0 1 1 1";
Cifre[1] = "0 0 1 0 0 1 0";
Cifre[2] = "1 0 1 1 1 0 1";
Cifre[3] = "1 0 1 1 0 1 1";
Cifre[4] = "0 1 1 1 0 1 0";
Cifre[5] = "1 1 0 1 0 1 1";
Cifre[6] = "0 1 0 1 1 1 1";
Cifre[7] = "1 0 1 0 0 1 0";
Cifre[8] = "1 1 1 1 1 1 1";
Cifre[9] = "1 1 1 1 0 1 0";
}
public void SetValue(String str) {
for (int i=0;i<MAXNUMCIFRE-1;i++){ CifraVal[i]=0; }
for (int i=0;i<str.length();i++){
CifraVal[NumCifre-str.length()+i]=Integer.parseInt(str.substring(i,i+1));
}
strValueDisplayed=str;
repaint();
}
// 獲取參數(shù)列表
String[] getParameterList(String param, char sep) {
String p = param;
String[] result = null;
if (param != null) {
int pos = 0;
int count = 0;
while ((pos = p.indexOf(sep)) >= 0) {
p = p.substring(pos + 1);
count++;
}
if (p.length() > 0) {
count++;
}
result = new String[count];
p = param;
for (int i=0; i<result.length; i++) {
pos = p.indexOf(sep);
if (pos < 0) {
result[i] = p.substring(0, p.length());
p = null;
} else {
result[i] = p.substring(0, pos);
p = p.substring(pos+1);
}
}
}
return result;
}
// 獲取參數(shù)初始值列表
int[] getParameterIntList(String param, char sep) {
String p = param;
int[] result = null;
if (param != null) {
int pos = 0;
int count = 0;
while ((pos = p.indexOf(sep)) >= 0) {
p = p.substring(pos + 1);
count++;
}
if (p.length() > 0) {
count++;
}
result = new int[count];
p = param;
for (int i=0; i<result.length; i++) {
pos = p.indexOf(sep);
if (pos < 0) {
result[i] = Integer.parseInt(p.substring(0, p.length()));
p = null;
} else {
result[i] = Integer.parseInt(p.substring(0, pos));
p = p.substring(pos+1);
}
}
}
return result;
}
public void PrintLed(Graphics g, int TheLed, int x, int y, Color TheColor) {
String pTemp[];
int DrawLedx[],
DrawLedy[];
DrawLedx = new int[9];
DrawLedy = new int[9];
pTemp = getParameterList(Led[TheLed],' ');
if (pTemp[0].compareTo("H") == 0) {
for (int j=0; j<=8; j++) {
DrawLedx[j] = 0;
DrawLedy[j] = 0;
DrawLedx[j] = HorLedx[j] + x + Integer.parseInt(pTemp[1]);
DrawLedy[j] = HorLedy[j] + y + Integer.parseInt(pTemp[2]);
}
} else {
for (int j=0; j<=8; j++) {
DrawLedx[j] = 0;
DrawLedy[j] = 0;
DrawLedx[j] = VerLedx[j] + x + Integer.parseInt(pTemp[1]);
DrawLedy[j] = VerLedy[j] + y + Integer.parseInt(pTemp[2]);
}
}
g.setColor(TheColor);
g.fillPolygon(DrawLedx, DrawLedy, 9);
}
// 顯示數(shù)字
public void PrintCifra(Graphics g, int LaCifra, int xpos, int ypos) {
String CT[];
CT = getParameterList(Cifre[LaCifra], ' ');
for (int i=0; i<=6; i++) {
if (CT[i].compareTo("1") == 0) {
PrintLed(g, i, xpos, ypos, fgColor);
} else {
PrintLed(g, i, xpos, ypos, bgColor);
}
}
}
// Paint()方法
public void paint(Graphics g) {
g.setColor(bgColor);
g.fillRect(0,0,size().width, size().height);
g.setColor(fgColor);
for (int i=0; i<NumCifre; i++) {
PrintCifra(g, CifraVal[i], CifraXP*i, CifraYP);
}
}
// 更新數(shù)據(jù)
public void update(Graphics g) { paint(g); }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -