大家好,我是尋覓:??????????????????????????????????????????????????????????????????????????????????????????????????????? 代碼下載
????????
????????????????? 大家元宵快樂啊!!我這次拿這個代碼改了一下,希望大家喜歡!
使用方法前一篇文章講過就不多說了。這里主要介紹:java的元宵快樂閃光字。還有,
初步解釋了applet 執行過程 與 this 抽象數據類型
代碼:
??1
import
?java.awt.
*
;
??2
import
?java.util.
*
;
//
timer
??3
??4
public
?
class
?yuanxiao?
extends
?java.applet.Applet?
{
??5
????
private
?Timer?timer;??????????????
//
?閃光進度計時器
??6
????
private
?String?labelString;???????
//
?閃光標簽
??7
????
private
?
int
?delay;????????????????
//
?閃光進度
??8
??9
????
public
?
void
?init()?
{??????????????????????????
//
這里是初始化,顧名思義,是對前面定義的參數進行
?10
?????????????????????????????????????????????????
//
賦值,也稱初始化
?11
?????????????????????????????????????????????????
?12
????????String?blinkFrequency?
=
?getParameter(
"
speed
"
);?????????
//
獲取參數speed(這是applet變量獲取參數的方法啊)
?13
????????delay?
=
?(blinkFrequency?
==
?
null
)?
?
?
400
?:?(
1000
?
/
?Integer.parseInt(blinkFrequency));
//
判斷,事現閃爍頻率
?14
????????labelString?
=
?getParameter(
"
lbl
"
);??????
//
獲取來自調用這個Applet的HTML文件,的參數lbl,要顯示給大家看的內容
?15
????????
if
?(labelString?
==
?
null
)
?16
????????????labelString?
=
?
"
Blink
"
;??????????????
//
判斷,如果lbl沒接收到參數,則賦值
?17
????????Font?font?
=
?
new
?java.awt.Font(
"
Serif
"
,?Font.PLAIN,?
24
);
//
設置文字屬性
?18
????????setFont(font);
?19
????????
?20
????????
?21
??????????????????????????????????????????????
?22
????}
?23
?24
????
public
?
void
?start()?
{???????
//
這里說起來有點無聊,因為似乎這里面的內容可以放到init()里面
?25
???????????????????????????????
//
因為我覺得這樣好象能提高效率。這個問題我還會繼續研究下去的,希望
?26
???????????????????????????????
//
能給大家一個說法,如果您有什么想法,請留言,還望不吝賜教!
?27
?????
?28
??????timer?
=
?
new
?Timer();?????
//
創建計時器
?29
????????timer.schedule(
new
?TimerTask()
//
這里可以看的timer的作用,它可以通過創建一個計時器任務來周期性地運行run()函數
?30
?????????
{?
?31
????????????
public
?
void
?run()?
{??
?32
????????????????repaint();
?33
????????????}
?34
????????}
?35
????????????,?delay,?delay);
//
指定timer的執行時間delay,和執行各后續任務之間的時間間隔delay??????????????????????????
?36
???????
?37
????}
?38
?39
????
public
?
void
?paint(Graphics?g)?
{??????????????????
//
這里是核心函數,注意applet沒有main函數,原因我覺得applet是
?40
????????????????????????????????????????????????????
//
基于web的html開發的,運行在客戶機上,解決網絡不流暢的問題
?41
????????????????????????????????????????????????????
//
所以,從網絡角度講applet被設計成一種好象數據流的東西,將
?42
????????????????????????????????????????????????????
//
核心代碼夾在start()和stop()之間,函數名可以隨便起,不影響
?43
????????????????????????????????????????????????????
//
代碼的運行。
?44
????????
int
?fontSize?
=
?g.getFont().getSize();????????
//
這里獲取(Graphics?g)中的g,字體大小24
?45
????????
/**/
/*
?46
????????這里有些東西要講清楚:
?47
????????大家看(Graphics?g)是不是有點類似于常用的main的(String[]?args)
?48
????????這些是參數,用于接收外來的東西;在這里,其實就是paint用來接收外界給它信息的接口,
?49
????????這是有人就會問,那函數怎么發送信息啊?通過返回值也就是函數內的return函數。
?50
????????另一個角度看這個問題,Graphics?g本身也是變量的定義,有點像:我們在函數體內寫
?51
????????Graphics?g;道理是一樣的,只不過放在paint()里多了個功能,能接收外來的參數而已;
?52
????????如,paint(***);這樣寫就把***傳給了paint().
?53
????????
*/
?54
????????
?55
????????
int
?x?
=
?
0
,?y?
=
?fontSize,?space;??????????
//
定義三個int變量,其中y為字體大小24
?56
????????
int
?red?
=
???(
int
)?(?
50
?
*
?Math.random());
//
調用math的隨機函數ramdom
?57
????????
int
?green?
=
?(
int
)?(?
50
?
*
?Math.random());
//
產生三原色
?58
????????
int
?blue?
=
??(
int
)?(
256
?
*
?Math.random());
?59
????????Dimension?d?
=
?
this
.getSize();?????????????
//
獲取單個字體的高和寬
?60
????????
/**/
/*
?61
?????????這里原來是“Dimension?d?=?getSize()”;,本人做了修改是為了闡述網友關于this問題,
?62
????????我們知道在其他的計算機語言中,很少見到this這種抽象數據類型(有人說this是關鍵字
?63
????????,這樣講未免過于籠統),這是java獨有的;從這個例子中我們可以看到,有沒有this都一樣,
?64
????????所以也就能推出
?65
??????????????????????this?=?(Graphics?g)?????????
?66
????????這樣講大家就明白了,this就是用于調用本函數接口的;
?67
????????如,我定義函數
?68
????????????????void?test(int?a?,?Stirng?b){
?69
????????????????//現在我用this來調用接口中的變量;
?70
????????????????this.a?=?a;
?71
????????????????this.b?=?b;
?72
????????????????//這樣寫似乎有點傻瓜,大家會覺得毫無意義
?73
????????????????//但如果你玩過C你就會知道,這樣寫起來方便
?74
????????????????//在C里我們會這樣寫:
?75
????????????????//int?aa?=?a;
?76
????????????????//char[]?bb?=?b;
?77
????????????????//這樣大家就看到了,用this時,我們可以省去,從新定義變量的無奈
?78
????????????????//簡化了我們的程序。
?79
????????????????//只要你接觸過另一個抽象數據類型super,你就會清楚這個特性其實也是
?80
????????????????//java類封裝所應具有的特性。
?81
????????????????}??
?82
????????????????
?83
????????????????下面是核心算法?????
?84
????????
?85
????????
*/
?86
????????g.setColor(Color.black);?????????????????
//
設置字體顏色為黑色
?87
????????FontMetrics?fm?
=
?g.getFontMetrics();??????
//
建立字形的信息函數
?88
????????space?
=
?fm.stringWidth(
"
?
"
);??????????????
//
建立一個已經獲取字符串的寬度的變量:space=0;
?89
????????
for
?(StringTokenizer?t?
=
?
new
?StringTokenizer(labelString);?t.hasMoreTokens();)?
?90
????????
//
既然是變化當然離不開循環了?,于是這里用到了for(字符串初始特征;更多特征;無內容)
?91
????????
//
從這里我們就知道,這個for每做一次會不斷獲取字符串信息
?92
????????
{
?93
????????????String?word?
=
?t.nextToken();??????????
//
將字符串的下一個特征賦給word
?94
????????????
?95
????????????
int
?w?
=
?fm.stringWidth(word)?
+
?space;
//
使用word和space
?96
????????????
//
下面的內容我想講起來比較抽象,還是得靠大家自己動動腦子,英文注釋保留給大家
?97
????????????
//
中文詳細解釋,過一段日子會我補上的,請大家放心,呵呵
?98
????????????
if
?(x?
+
?w?
>
?d.width)?
{
?99
????????????????x?
=
?
0
;
100
????????????????y?
+=
?fontSize;???????
//
move?word?to?next?line?if?it?doesn't?fit
101
????????????}
102
????????????
if
?(Math.random()?
<
?
0.5
)
103
????????????????g.setColor(
new
?java.awt.Color((red?
+
?y
*
30
)?
%
?
256
,?
104
??????????????????????????????????????????????(green?
+
?x
/
3
)?
%
?
256
,?blue));
105
????????????
else
106
????????????????g.setColor(getBackground());
107
????????????g.drawString(word,?x,?y);
108
????????????x?
+=
?w;??
//
shift?to?the?right?to?draw?the?next?word
109
????????}
//
for結束
110
????}
111
????
112
????
public
?
void
?stop()?
{
113
????????timer.cancel();??
//
關閉記時器
114
????}
115
116
/**/
/*
下面的代碼實際上沒有用到,我在這里將他屏蔽
117
118
????public?String?getAppletInfo()?{
119
????????return?"Title:?Blinker\n"
120
????????????+?"Author:?Arthur?van?Hoff\n"?
121
????????????+?"Displays?multicolored?blinking?text.";
122
????}
123
????
124
????public?String[][]?getParameterInfo()?{???//這是java二維數組,返回其頭指針
125
????????String?pinfo[][]?=?{
126
????????????{"speed",?"string",?"The?blink?frequency"},
127
????????????{"lbl",?"string",?"The?text?to?blink."},
128
????????};
129
????????return?pinfo;
130
????}
*/
131
}
132
133
HTML文件:
?1
<
html
>
?2
??
<
head
>
?3
??????
<
title
>
Blinking?Text
</
title
>
?4
??
</
head
>
?5
??
<
body
>
?6
??????
<
h1
>
元宵快樂閃光字
</
h1
>
?7
??????
<
hr
>
?8
??????
<
applet?
code
="yuanxiao.class"
?width
=780?
height
=100
>
?9
????????
<
param?
name
=lbl?
value
="這是給大家的元宵小禮物??呵呵!
10
???????????????????大??家??元??宵??快??樂??啊?!"
>
11
??????????
<
param?
name
=speed?
value
="4"
>
12
????????????alt="Your?browser?understands?the?
<
APPLET
>
?tag?but?isn't?runni
13
??????????????????????????????ng?the?applet,?for?some?reason."
14
????????????Your?browser?is?completely?ignoring?the?
<
APPLET
>
?tag!
15
??????
</
applet
>
16
??????
<
hr
>
17
??????
<
a?
href
="yuanxiao.java"
?temp_href
="yuanxiao.java"
>
The?source.
</
a
>
18
??
</
body
>
19
</
html
>
20
?
地震讓大伙知道:居安思危,才是生存之道。
posted on 2007-03-05 14:30
小尋 閱讀(610)
評論(0) 編輯 收藏