
案例一:色形的动态变化 设计要求 编写一个A即pet,实现色彩的动态变化,交替变化的色彩从网页中读取,可定义颜色交 替变化数量,也可给出指定变化的颜色:交替变化的方式也由参数给出。 实例效果 查看源文件] import java.applet Applet: import java.awt.; import java.lo.Printstream: import java.util.StringTokenizer: public dass Bulets extends Applet implements Runnable private static final int NONE -0: private static final int LEFT 1; private static final int RIGHT 2; private static final int UP 3; private static final int DOWN =4: private static final int IN 5: private static final int OUT 6; private static final int CYCLE-0; private static final int FADE =1; private static Bullets threadMaster: private static Bullets uniqueAppList[ private long nextUpdateTime: private Thread mainThread: private int R;
案例一:色彩的动态变化 设计要求: 编写一个 Applet,实现色彩的动态变化,交替变化的色彩从网页中读取,可定义颜色交 替变化数量,也可给出指定变化的颜色;交替变化的方式也由参数给出。 实例效果 [查看源文件] import java.applet.Applet; import java.awt.*; import java.io.PrintStream; import java.util.StringTokenizer; public class Bullets extends Applet implements Runnable { private static final int NONE = 0; private static final int LEFT = 1; private static final int RIGHT = 2; private static final int UP = 3; private static final int DOWN = 4; private static final int IN = 5; private static final int OUT = 6; private static final int CYCLE = 0; private static final int FADE = 1; private static Bullets threadMaster; private static Bullets uniqueAppList[]; private long nextUpdateTime; private Thread mainThread; private int R;

private int G: private int B; private int ToR: private int ToG; private int ToB; private int fadeCounter; private int fadeAmount; private int colors private int colorlndex; private int displayCounter: private int displayColors; private int delay. private int pauseTime; private int smoothness; private int effect: private int direction; private Color currentColor; private Color bgColor: private Color displayList[l: public volatile Graphics offGraphic. private Image offlmage: public volatile Bullets slaveList[: private volatile Bullets master: private boclean isBullet: public boolean mouseDown(Event event,int i,int j) System.out.printin("uniqueApplist.length is "uniqueAppList.length): for[int k=0;k uniqueAppList.length;k++) System.outprintin["Master"k+"slavelist.length is"+uniqueApplist[]slaveList.length):
private int G; private int B; private int ToR; private int ToG; private int ToB; private int fadeCounter; private int fadeAmount; private int colors[]; private int colorIndex; private int displayCounter; private int displayColors; private int delay; private int pauseTime; private int smoothness; private int effect; private int direction; private Color currentColor; private Color bgColor; private Color displayList[]; public volatile Graphics offGraphic; private Image offImage; public volatile Bullets slaveList[]; private volatile Bullets master; private boolean isBullet; public boolean mouseDown(Event event, int i, int j) { System.out.println("uniqueAppList.length is " + uniqueAppList.length); for(int k = 0; k < uniqueAppList.length; k++) { System.out.println("Master " + k + " slavelist.length is " + uniqueAppList[k].slaveList.length);

for[int I=0;I uniqueAppList[k].slaveList_length ++ System.out.printin uniqueApplist k].slaveList[].getGraphicsDl: 1 return true; public String getAppletinfo() return "Bullets 10,copyright 1996,Macromedia,Inc.-by Paul Madar"; private int conwertTime(String s,String s1,double d) double d1"d; 51=nu return (int)(d1 10000): try 4 d1 Double.valueof[s1)doublevalue()10000: catch(NumberformatException ex) System.out.printin"Invald value "+s1+"for parameter'+s+",using"+d): d1=d*10000: 1 return (int)d1:
for(int l = 0; l < uniqueAppList[k].slaveList.length; l++) System.out.println(uniqueAppList[k].slaveList[l].getGraphics()); } return true; } public String getAppletInfo() { return "Bullets 1.0, copyright 1996, Macromedia, Inc. - by Paul Madar"; } private int convertTime(String s, String s1, double d) { double d1 = d; if(s1 == null) return (int)(d1 * 1000D); try { d1 = Double.valueOf(s1).doubleValue() * 1000D; } catch(NumberFormatException _ex) { System.out.println("Invalid value " + s1 + " for parameter '" + s + "', using " + d); d1 = d * 1000D; } return (int)d1; }

public void init() inti=0 String s=getParameter("bgcolor"): bgColor new ColorgetColor(s,getBackground(l.getRGB(: currentColor bgColor; s"getParameter["fps"l: 作=nu try { i-Integer.parselnt(sh 1 catch(NumberformatException_ex){} dely=ic=07100:1000/e s getParameter["style" if作Iand&&s.equalslgnoreCase('separator"》 is Bullet false: s=getParameter("smoothness"]: f华=nu try smoothness Integer.parselnt(s): catch(NumberFormatException_ex)[] if(smoothness <1) 1 System.out.printin"smoothness value of "smoothness +is invalid,setting value to 1" smoothness 1: s getParameter(fadeAmount"):
public void init() { int i = 0; String s = getParameter("bgcolor"); bgColor = new Color(getColor(s, getBackground().getRGB())); currentColor = bgColor; s = getParameter("fps"); if(s != null) try { i = Integer.parseInt(s); } catch(NumberFormatException _ex) { } delay = i <= 0 ? 100 : 1000 / i; s = getParameter("style"); if(s != null && s.equalsIgnoreCase("separator")) isBullet = false; s = getParameter("smoothness"); if(s != null) try { smoothness = Integer.parseInt(s); } catch(NumberFormatException _ex) { } if(smoothness < 1) { System.out.println("smoothness value of " + smoothness + " is invalid, setting value to 1"); smoothness = 1; } s = getParameter("fadeAmount");

作修-nu try 1 tadeAmount Integer parselnt(sl: catch(NumberFormatException_ex] f fadeAmount 1) System.out.printin["fadeAmount value of"+fadeAmount +"is inwalid,setting value to 1"): fadeAmount 1; s getParameter(dispbyColors" if作1enu try { displayColors=intege虹.p四rselnt(s catch(NumberformatException ex] if displ由Calors<1 System.out.printin("displayColor value of "+displaycolors+"is invalid,setting value to 1") displayColors-1; s getParameter("effect"): if作1enu 1
if(s != null) try { fadeAmount = Integer.parseInt(s); } catch(NumberFormatException _ex) { if(fadeAmount < 1) { System.out.println("fadeAmount value of " + fadeAmount + " is invalid, setting value to 1"); fadeAmount = 1; } } s = getParameter("displayColors"); if(s != null) try { displayColors = Integer.parseInt(s); } catch(NumberFormatException _ex) { if(displayColors < 1) { System.out.println("displayColor value of " + displayColors + " is invalid, setting value to 1"); displayColors = 1; } } s = getParameter("effect"); if(s != null) {

s =s.tolowercasell:; if作-equals("fade"》 effect 1; direction 0: }else if作.equa水thet刀 elet■l direction .1: ]else if's.equals("faderight")) effect▣l: drection 2; )else f5.equals("fadeup°刀 1 effect 1: drection 3: ]else i作.equals("fadedown'刀 effect 1; drection 4; ]else if作.equals("fadein"》 1 effect 1; drection S:
s = s.toLowerCase(); if(s.equals("fade")) { effect = 1; direction = 0; } else if(s.equals("fadeleft")) { effect = 1; direction = 1; } else if(s.equals("faderight")) { effect = 1; direction = 2; } else if(s.equals("fadeup")) { effect = 1; direction = 3; } else if(s.equals("fadedown")) { effect = 1; direction = 4; } else if(s.equals("fadein")) { effect = 1; direction = 5;

]else if作.equals("fadeout'切 effect 1; direction =6; 】else if作.equals("cycleleft刀 1 effect -0; direction 1; 】ele if华.equals'cyclerigh情》 effect-0; direction 2. ]else in's.equals("cycleup")) effect =0; direction 3. Jelse ifs作.equals("cycledown'刀 1 effect=0; direction=4; }else if华.equals("cycein'》 1 effect■0 direction S;
} else if(s.equals("fadeout")) { effect = 1; direction = 6; } else if(s.equals("cycleleft")) { effect = 0; direction = 1; } else if(s.equals("cycleright")) { effect = 0; direction = 2; } else if(s.equals("cycleup")) { effect = 0; direction = 3; } else if(s.equals("cycledown")) { effect = 0; direction = 4; } else if(s.equals("cyclein")) { effect = 0; direction = 5;

】else ifs.equals("cycleout")) 1 effect -0; direction=6; 】else if华.equals("cycle") effect =0; direction =0; switch(direction) default: brealc ca5e5:/∥V005 ca5e6/∥006' if(sizeD.width>size()height) if displayColors sizel)width 2] displayColors =size().width /2; break iNdisplayColors sizel)height /2) displayColors"size()height /2: break cas01:/∥0o1 case2:/∥0o2
} else if(s.equals("cycleout")) { effect = 0; direction = 6; } else if(s.equals("cycle")) { effect = 0; direction = 0; } switch(direction) { default: break; case 5: // '\005' case 6: // '\006' if(size().width > size().height) { if(displayColors > size().width / 2) displayColors = size().width / 2; break; } if(displayColors > size().height / 2) displayColors = size().height / 2; break; case 1: // '\001' case 2: // '\002

fdisplayColors sizel)width) displayColors size()width: break case3:/∥003 case4:∥004 iN(displayColors>size()height) displayColors =size()height; break case 0:∥ dsplayColors 1; break if(displayColors >smoothness) smoothness-displayColors: displaylist new Colorlsmoothness]: forlintj=0:j<smoothness:++) displayList]-currentColor; s=getParameter["pause"]: pauseTime comvertTime["pause",s,0.00); s=getParameter["colors"]: if's =null I s.equalsignoreCase["rainbow)) colors =new int6]: colors10]-0xf0000; colors[1]=Oxffc800; colors(2]Oxffft00:
if(displayColors > size().width) displayColors = size().width; break; case 3: // '\003' case 4: // '\004' if(displayColors > size().height) displayColors = size().height; break; case 0: // '\0' displayColors = 1; break; } } if(displayColors > smoothness) smoothness = displayColors; displayList = new Color[smoothness]; for(int j = 0; j < smoothness; j++) displayList[j] = currentColor; s = getParameter("pause"); pauseTime = convertTime("pause", s, 0.0D); s = getParameter("colors"); if(s == null || s.equalsIgnoreCase("rainbow")) { colors = new int[6]; colors[0] = 0xff0000; colors[1] = 0xffc800; colors[2] = 0xffff00;

colors[3]65280; colors(4]255; colors 5]Chc800ff; 】ele int k=0; StringTokenizer stringtokenizer"new StringTokenizer(s,"t\n\," colors new int|stringtokenizer.countTokensD]: while(stringtokenizer.hasMoreTokens()] colors [k]getColor(stringtokenizer.nextToken()0: k+粉 1 displayCounter smoothness; R bgColor.getRGB()>>16 &Oxff; G.baColor.getRGB(1>8 Oxff: B=bgColor.getRGB()&Oxff; if effect =1) 1 TeR■colors0]>>16&0xl ToG=colors[o]so 8 Oxff: ToB colors |0]0xff. public void start) appStart(this):
colors[3] = 65280; colors[4] = 255; colors[5] = 0xc800ff; } else { int k = 0; StringTokenizer stringtokenizer = new StringTokenizer(s, "\t\n\r,"); colors = new int[stringtokenizer.countTokens()]; while(stringtokenizer.hasMoreTokens()) { colors[k] = getColor(stringtokenizer.nextToken(), 0); k++; } } displayCounter = smoothness; R = bgColor.getRGB() >> 16 & 0xff; G = bgColor.getRGB() >> 8 & 0xff; B = bgColor.getRGB() & 0xff; if(effect == 1) { ToR = colors[0] >> 16 & 0xff; ToG = colors[0] >> 8 & 0xff; ToB = colors[0] & 0xff; } } public void start() { appStart(this); }