
案例四:文字的滚动显示 设计要求: 滚动显示的文字由用户给出,显示相应文字的颜色、文字显示的时间间隔也由用户定义。 实现文字的循环滚动显示。 实例效果 [查看源文件 import java.applet.Applet; import java.awt.*; import java.io.PrintStream public class VColorScroll extends Applet implements Runnable int MAX_TEXT_NUM; int real_text_num; int Width; int Height magetotal Imagescreen; Graphics totalG; Graphics offscreen Font displayFont: Thread me int offset; int wait; int ht; int speed;
案例四:文字的滚动显示 设计要求: 滚动显示的文字由用户给出,显示相应文字的颜色、文字显示的时间间隔也由用户定义。 实现文字的循环滚动显示。 实例效果 [查看源文件] import java.applet.Applet; import java.awt.*; import java.io.PrintStream; public class VColorScroll extends Applet implements Runnable { int MAX_TEXT_NUM; int real_text_num; int Width; int Height; Image total; Image screen; Graphics totalG; Graphics offscreen; Font displayFont; Thread me; int offset; int wait; int ht; int speed; int TextSize;

intstartDelay: int TextDelay(l: int finalDelay. String TextStringll: String theFont; Boolean rePeat; Color TextColorl. public VColorScroll( MAX_TEXT_NUM·1O0 1 public void init) Width =size[).width; Height size()height; offset =-Height; speed =50; thefont ="TimesRoman"; startDelay 1 finalDelay =1; rePeat Boolean.TRUE: TextString new String(MAX_TEXT_NUM]: TextDelay-new int[MAX_TEXT_NUM]: TextColor new Color[MAX_TEXT_NUM] wait startDelay: try TextSize=(new IrtegerlgetParameter(TextSize"])intvalueD:
int startDelay; int TextDelay[]; int finalDelay; String TextString[]; String theFont; Boolean rePeat; Color TextColor[]; public VColorScroll() { MAX_TEXT_NUM = 100; } public void init() { Width = size().width; Height = size().height; offset = -Height; speed = 50; theFont = "TimesRoman"; startDelay = 1; finalDelay = 1; rePeat = Boolean.TRUE; TextString = new String[MAX_TEXT_NUM]; TextDelay = new int[MAX_TEXT_NUM]; TextColor = new Color[MAX_TEXT_NUM]; wait = startDelay; try { TextSize = (new Integer(getParameter("TextSize"))).intValue(); }

catchException_ex) 1 Texi2e■20 displayFont new Font(theFont,0,TextSize); real_text_num =0: for[int i=0;i<MAX_TEXT_NUM;i++) try TextString]=getParameter("Text"+il; if(Text5 tringi]I-nu间 real_text_num++; 1 catch(Exception el System.out.printine): String tmpDelay: if (tmpDelay getParameter("Delay"+i==null) tmpDelay"10; String tmpColor: if([tmpColor getParameter["Color")null) tmpColor ="FF0000"; try TextDelayli]=(new Integer(tmpDelayl).intValue(l: catchException ex)
catch(Exception _ex) { TextSize = 20; } displayFont = new Font(theFont, 0, TextSize); real_text_num = 0; for(int i = 0; i < MAX_TEXT_NUM; i++) { try { TextString[i] = getParameter("Text" + i); if(TextString[i] != null) real_text_num++; } catch(Exception e) { System.out.println(e); } String tmpDelay; if((tmpDelay = getParameter("Delay" + i)) == null) tmpDelay = "10"; String tmpColor; if((tmpColor = getParameter("Color" + i)) == null) tmpColor = "FF0000"; try { TextDelay[i] = (new Integer(tmpDelay)).intValue(); } catch(Exception _ex) {

TextDelayli]10; try TextColori]"new Color[integer.parselnt(tmpColor,16] 1 catchException_ex) TextColor间■Color.red: 】 1 try i=Hegt·real_text_nur total createlmage(Width,ht]: totalG-totaLgetGraphics() screen■createlmage[Width.Height): offscreen screen.getGraphics(l: return; 1 catchException_ex) offscreen nult; 1 public void start() ifme null) me new Thread(thisl
TextDelay[i] = 10; } try { TextColor[i] = new Color(Integer.parseInt(tmpColor, 16)); } catch(Exception _ex) { TextColor[i] = Color.red; } } try { ht = Height * real_text_num; total = createImage(Width, ht); totalG = total.getGraphics(); screen = createImage(Width, Height); offscreen = screen.getGraphics(); return; } catch(Exception _ex) { offscreen = null; } } public void start() { if(me == null) { me = new Thread(this);

me.start(: public void stop() f me !null) me.stop(l: me null: 人 public void run(] DrawText totalGl: while(me I=nul) iftwait c 0] offset++; repaint月 try Thread.sleep(speedl: 1 catchllnterruptedException_ex][ ifwait<ad创 fo伤et=ht
me.start(); } } public void stop() { if(me != null) { me.stop(); me = null; } } public void run() { DrawText(totalG); while(me != null) { if(wait <= 0) { offset++; repaint(); } try { Thread.sleep(speed); } catch(InterruptedException _ex) { } if(wait <= 0) { if(offset == ht) {

offset--Height; wait finalDelay: ifrePeat=Boolean.FALSE) stop(: ]else iN(oltset%Height=o可 wait TextDelayfoffset Height]: 】cle wait-; public void update(Graphics g) paintig 1 public void paint(Graphicsg) f offscreen =null) g-drawString(new String("Sorry,ol'chap,dan't look good")0,0): return: 】else 1 offscreen.setColor(Color.bluel: offscreen.fillRect(0,0.Width.Height offscreen.drawimageltotal,0,-offset,this): g-drawlmage(screen,0.0.thisl: return:
offset = -Height; wait = finalDelay; if(rePeat == Boolean.FALSE) stop(); } else if(offset % Height == 0) wait = TextDelay[offset / Height]; } else { wait--; } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if(offscreen == null) { g.drawString(new String("Sorry, ol' chap, don't look good"), 0, 0); return; } else { offscreen.setColor(Color.blue); offscreen.fillRect(0, 0, Width, Height); offscreen.drawImage(total, 0, -offset, this); g.drawImage(screen, 0, 0, this); return;

public void DrawText(Graphks g) g-setFont(displayFont]: FontMetrics fontMet=g-getFontMetrics(); g.setColor(Color.yellowl gfillRect(0,0,Width,ht); int y =-2-fontMet.getMaDecent( for[int i-0:i MAX_TEXT_NUM;i++) if(TextString0I=u周 int x Width 2-fontMet.stringWidth(Textstringli/2: gsetColor(TextColori]): g-drawString(TextStringlil.x (i+1)*Height+yh:
} } public void DrawText(Graphics g) { g.setFont(displayFont); FontMetrics fontMet = g.getFontMetrics(); g.setColor(Color.yellow); g.fillRect(0, 0, Width, ht); int y = -2 - fontMet.getMaxDecent(); for(int i = 0; i < MAX_TEXT_NUM; i++) if(TextString[i] != null) { int x = Width / 2 - fontMet.stringWidth(TextString[i]) / 2; g.setColor(TextColor[i]); g.drawString(TextString[i], x, (i + 1) * Height + y); } } }