next up previous contents
Next: net Up: Samples / No Previous: Util

Thread

meter.java

  1: /*
  2:  * meter.java  0.1  96/08/01
  3:  *
  4:  * Copyright (c) 1996  MARUYAMA Fujio  All Rights Reserved.
  5:  *
  6:  * Permission to use, copy, modify, and distribute this software
  7:  * and its documentation for NON-COMMERCIAL purposes and without
  8:  * fee is hereby granted provided that this copyright notice
  9:  * appears in all copies. 
 10:  *
 11:  *           Mail : maruyama@wakhok.ac.jp
 12:  *
 13:  */
 14: 
 15: import java.awt.*;
 16: import java.applet.*;
 17: 
 18: class Meter extends Canvas implements Runnable{
 19:    static final double Pi = Math.PI ;
 20:    public int centerX ;
 21:    public int centerY ;
 22:    public int Radius ;
 23:    public int newX ;
 24:    public int newY ;
 25:    public int priority ;
 26:    public Dimension minSize ;
 27:    public Thread moveMeter= null ;
 28:    public static Color carray[] = new Color[4];
 29: 
 30:    static{
 31:       carray[0] = Color.blue;
 32:       carray[1] = Color.green;
 33:       carray[2] = Color.yellow;
 34:       carray[3] = Color.red;
 35:    }
 36:    
 37:    Meter(int r ,int p){
 38:       centerX = r ;
 39:       centerY = r ;
 40:       Radius = r ;
 41:       priority = p ;
 42:       minSize = new Dimension(2*r+ 10, 2*r+ 20);
 43:    }
 44:    
 45:    public Dimension preferredSize(){
 46:      return minimumSize();
 47:    }
 48: 
 49:    public void start(){
 50:      if ( moveMeter == null ){
 51:         moveMeter = new Thread(this);
 52:         moveMeter.setPriority(priority);
 53:         moveMeter.start();
 54:      }
 55:    }
 56: 
 57:    public synchronized Dimension minimumSize(){
 58:      return minSize;
 59:    }
 60: 
 61:    public void disp( int degree){
 62:      double radian = degree * Pi / 180.0;
 63:      newX = (int ) ( Radius * Math.sin( radian )) + centerX ;
 64:      newY = -(int ) ( Radius * Math.cos( radian )) + centerY ;
 65:      repaint();
 66:      try {
 67:         Thread.sleep(10);
 68:      } catch (Exception e){}
 69:    }
 70: 
 71:    public void paint(Graphics g){
 72:      g.setColor( carray[priority-1] );
 73:      g.drawOval(0,0, 2*Radius , 2*Radius);
 74:      g.drawLine(centerX,centerY, newX,newY);
 75:    }
 76: 
 77:    public void run(){
 78:      long count = 0;
 79:      while( count++ < 3600 ){
 80:        int c = (int) ( count % 360 ) ;
 81:        disp( c );
 82:      }
 83:    }
 84: 
 85:    public static void main(String argv[]){
 86:       int r = 50 ;
 87:       Frame f = new Frame();
 88:       Panel p = new Panel();
 89:       p.setLayout(new GridLayout(1,0));
 90: 
 91:       Meter m1 = new Meter(r,1);
 92:       Meter m2 = new Meter(r,2);
 93:       Meter m3 = new Meter(r,3);
 94:       Meter m4 = new Meter(r,2);
 95:       Meter m5 = new Meter(r,4);
 96: 
 97:       p.add(m1);
 98:       p.add(m2);
 99:       p.add(m3);
100:       p.add(m4);
101:       p.add(m5);
102: 
103:       f.add("Center",p);
104:       f.resize(10*r+60, 2*r+40);
105:       f.show();
106:       f.validate();
107: 
108:       m1.start();
109:       m2.start();
110:       m3.start();
111:       m4.start();
112:       m5.start();
113:    }
114: }



maruyama@wakhok.ac.jp