ch16l02.txt

来自「《Web编程专家指南》」· 文本 代码 · 共 76 行

TXT
76
字号
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 + =
减小字号Ctrl + -
显示快捷键?