Report#6
Java Applet プログラム{〜01/29(Mon)}01: import java.awt.* ; 02: public class stopwatch extends java.applet.Applet implements Runnable{ 03: Thread threader = new Thread( this ) ; 04: Nthreads nth1 = new Nthreads(1) ; 05: Nthreads nth2 = new Nthreads(2) ; 06: Nthreads nth3 = new Nthreads(3) ; 07: TextField t1 = new TextField("0") ; 08: TextField t2 = new TextField("0") ; 09: TextField t3 = new TextField("0") ; 10: int alive1 = 9 , alive2 = 9 , alive3 = 9 ; 11: public void init(){ 12: setLayout( new BorderLayout() ) ; 13: Panel p1 = new Panel() ; 14: p1.setLayout( new GridLayout( 7 , 3 , 5 , 5 ) ) ; 15: p1.add( new Label("Watch-1")) ; 16: p1.add( new Label("Watch-2")) ; 17: p1.add( new Label("Watch-3")) ; 18: p1.add( t1 ) ; 19: p1.add( t2 ) ; 20: p1.add( t3 ) ; 21: p1.add( new Button( "Start1" ) ) ; 22: p1.add( new Button( "Start2" ) ) ; 23: p1.add( new Button( "Start3" ) ) ; 24: p1.add( new Button( "Stop1" ) ) ; 25: p1.add( new Button( "Stop2" ) ) ; 26: p1.add( new Button( "Stop3" ) ) ; 27: p1.add( new Button( "Reset1" ) ) ; 28: p1.add( new Button( "Reset2" ) ) ; 29: p1.add( new Button( "Reset3" ) ) ; 30: p1.add( new Button( "StartAll" ) ) ; 31: p1.add( new Button( "StopAll" ) ) ; 32: p1.add( new Button( "ResetAll" ) ) ; 33: add( "Center" , p1 ) ; 34: } 35: public void start(){ 36: threader.start() ; 37: } 38: public void stop(){ 39: threader.stop() ; 40: } 41: public void run(){ 42: while( true ){ 43: try{ 44: threader.sleep( 100 ) ; 45: }catch( InterruptedException e ){} 46: dispnum() ; 47: } 48: } 49: public void dispnum(){ 50: t1.setText( Integer.toString( nth1.ret1() ) ) ; 51: t2.setText( Integer.toString( nth2.ret2() ) ) ; 52: t3.setText( Integer.toString( nth3.ret3() ) ) ; 53: } 54: public boolean action( Event e , Object o ){ 55: if( e.target instanceof Button ){ 56: if( e.arg == "Start1" ){ 57: if( alive1 == 0 ) nth1.restart() ; 58: else nth1.start() ; 59: alive1 = 1 ; 60: } 61: if( e.arg == "Start2" ) 62: if( alive2 == 0 ) nth2.restart() ; 63: else nth2.start() ; 64: alive2 = 1 ; 65: } 66: if( e.arg == "Start3" ){ 67: if( alive3 == 0 ) nth3.restart() ; 68: else nth3.start() ; 69: alive3 = 1 ; 70: } 71: if( e.arg == "Stop1" ){ 72: nth1.pose() ; 73: alive1 = 0 ; 74: } 75: if( e.arg == "Stop2" ){ 76: nth2.pose() ; 77: alive2 = 0 ; 78: } 79: if( e.arg == "Stop3" ){ 80: nth3.pose() ; 81: alive3 = 0 ; 82: } 83: if( e.arg == "Reset1" ){ 84: if( alive1 == 0 ) 85: nth1.reset1() ; 86: } 87: if( e.arg == "Reset2" ){ 88: if( alive2 == 0 ) 89: nth2.reset2() ; 90: } 91: if( e.arg == "Reset3" ){ 92: if( alive3 == 0 ) 93: nth3.reset3() ; 94: } 95: if( e.arg == "StartAll" ){ 96: startAll() ; 97: } 98: if( e.arg == "StopAll" ){ 99: stopAll() ; 100: } 101: if( e.arg == "ResetAll" ){ 102: resetAll() ; 103: } 104: } 105: return true ; 106: } 107: public void startAll(){ 108: if( alive1 == 0 & alive2 == 0 ){ 109: if( alive3 == 0 ){ 110: nth1.restart() ; 111: nth2.restart() ; 112: nth3.restart() ; 113: } 114: } 115: else{ 116: nth1.start() ; 117: nth2.start() ; 118: nth3.start() ; 119: } 120: alive1 = 1 ; 121: alive2 = 1 ; 122: alive3 = 1 ; 123: } 124: public void stopAll(){ 125: nth1.pose() ; 126: nth2.pose() ; 127: nth3.pose() ; 128: alive1 = 0 ; 129: alive2 = 0 ; 130: alive3 = 0 ; 131: } 132: public void resetAll(){ 133: if( alive1 == 0 & alive2 == 0 ){ 134: if( alive3 == 0 ){ 135: nth1.reset1() ; 136: nth2.reset2() ; 137: nth3.reset3() ; 138: } 139: } 140: } 141: } 142: class Nthreads extends Thread{ 143: Thread th = new Thread( this ) ; 144: int cnt1 = 0 , cnt2 = 0 , cnt3 = 0 ; 145: int id ; 146: Nthreads( int num ){ 147: id = num ; 148: } 149: public void reset1(){ 150: cnt1 = 0 ; 151: } 152: public void reset2(){ 153: cnt2 = 0 ; 154: } 155: public void reset3(){ 156: cnt3 = 0 ; 157: } 158: public void start(){ 159: th.start() ; 160: } 161: public void restart(){ 162: th.resume() ; 163: } 164: public void pose(){ 165: th.suspend() ; 166: } 167: public void run(){ 168: while( true ){ 169: try{ 170: Thread.sleep( 100 ) ; 171: }catch( InterruptedException e ){} 172: if( id == 1 ) ++cnt1 ; 173: else if( id == 2 ) ++cnt2 ; 174: else if( id == 3 ) ++cnt3 ; 175: } 176: } 177: int ret1(){ 178: return cnt1 ; 179: } 180: int ret2(){ 181: return cnt2 ; 182: } 183: int ret3(){ 184: return cnt3 ; 185: } 186: }