📄 ch16l02.txt
字号:
Listing 16.2 Dial class.import java.awt.*;class Dial extends Canvas implements Runnable { boolean flash; Thread timeThread; long sweepTime; long startTime; int ticksEvery, highEnd, lowEnd; String name; double percent; Color flashColor; public Dial(String aName, int aLowVal, int aHighVal, int aTicksEvery, long aSweepTime, Color fColor) { name = aName; lowEnd = aLowVal; highEnd = aHighVal; ticksEvery = aTicksEvery; flashColor = fColor; percent = .33333; sweepTime = aSweepTime; startTime = java.lang.System.currentTimeMillis(); timeThread = new Thread(this); timeThread.setDaemon(true); timeThread.start(); } public Dial() { startTime = java.lang.System.currentTimeMillis(); } public void paint(Graphics g) { Rectangle myExtent = this.bounds(); g.drawOval(0, 0, myExtent.width, myExtent.height); if(flash == true){ g.setColor(flashColor); g.fillOval(0, 0, myExtent.width, myExtent.height); g.setColor(Color.black); g.drawString(name, 0, myExtent.height / 2); flash = false; }else{ //convert percent done into an arc... int arcDegrees = (int) (percent * 360); g.fillArc(0, 0, myExtent.width, myExtent.height, 90, arcDegrees); } }public void update(Graphics g) { paint(g); } public void run(){ flash = false; while(true){ long curTime = java.lang.System.currentTimeMillis(); double oldPercent = percent; percent = (double)(curTime - startTime)/(double) sweepTime; percent -= (int) percent; if(percent < oldPercent){ //Cycle completed flash = true; } repaint(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -