001 package com.croftsoft.apps.cyborg; 002 003 import java.awt.*; 004 005 import com.croftsoft.core.lang.NullArgumentException; 006 import com.croftsoft.core.lang.lifecycle.Initializable; 007 import com.croftsoft.core.lang.lifecycle.Lifecycle; 008 import com.croftsoft.core.lang.lifecycle.LifecycleLib; 009 import com.croftsoft.core.lang.lifecycle.Updatable; 010 import com.croftsoft.core.gui.LifecycleWindowListener; 011 import com.croftsoft.core.util.loop.*; 012 013 /********************************************************************* 014 * Neural interface simulation using a joystick. 015 * 016 * @version 017 * $Date: 2005/10/17 16:47:51 $ 018 * @since 019 * 2005-03-14 020 * @author 021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 022 *********************************************************************/ 023 024 public final class CyborgMain 025 implements Lifecycle, Loopable, Runnable 026 ////////////////////////////////////////////////////////////////////// 027 ////////////////////////////////////////////////////////////////////// 028 { 029 030 private final Initializable [ ] initializables; 031 032 private final Updatable [ ] updatables; 033 034 private final Looper looper; 035 036 ////////////////////////////////////////////////////////////////////// 037 ////////////////////////////////////////////////////////////////////// 038 039 public static void main ( String [ ] args ) 040 ////////////////////////////////////////////////////////////////////// 041 { 042 System.out.println ( "\n" + CyborgConfig.INFO ); 043 044 CyborgModel cyborgModel = new CyborgModelImpl ( ); 045 046 CyborgOperator cyborgOperator = new CyborgOperator ( cyborgModel ); 047 048 CyborgController cyborgController = new CyborgController ( 049 cyborgModel, 050 cyborgOperator ); 051 052 CyborgFrame cyborgFrame 053 = new CyborgFrame ( cyborgModel, cyborgController ); 054 055 CyborgMain cyborgMain = new CyborgMain ( 056 new Initializable [ ] { cyborgFrame }, 057 new Updatable [ ] { 058 cyborgController, 059 cyborgModel, 060 cyborgFrame }, 061 cyborgModel.getLoopGovernor ( ) ); 062 063 LifecycleWindowListener.launchFrameAsDesktopApp ( 064 cyborgFrame, 065 new Lifecycle [ ] { cyborgMain }, 066 CyborgConfig.FRAME_SIZE, 067 CyborgConfig.SHUTDOWN ); 068 } 069 070 ////////////////////////////////////////////////////////////////////// 071 ////////////////////////////////////////////////////////////////////// 072 073 public CyborgMain ( 074 Initializable [ ] initializables, 075 Updatable [ ] updatables, 076 LoopGovernor loopGovernor ) 077 ////////////////////////////////////////////////////////////////////// 078 { 079 NullArgumentException.check ( 080 this.initializables = initializables ); 081 082 NullArgumentException.check ( this.updatables = updatables ); 083 084 looper = new Looper ( 085 this, // loopable 086 loopGovernor, 087 null, // exceptionHandler 088 "Cyborg Loop", // threadName 089 Thread.MIN_PRIORITY, 090 true ); // useDaemonThread 091 } 092 093 ////////////////////////////////////////////////////////////////////// 094 ////////////////////////////////////////////////////////////////////// 095 096 public void init ( ) 097 ////////////////////////////////////////////////////////////////////// 098 { 099 LifecycleLib.init ( initializables ); 100 101 LifecycleLib.init ( looper ); 102 103 LifecycleLib.start ( looper ); 104 } 105 106 public void start ( ) 107 ////////////////////////////////////////////////////////////////////// 108 { 109 } 110 111 public void stop ( ) 112 ////////////////////////////////////////////////////////////////////// 113 { 114 } 115 116 public void destroy ( ) 117 ////////////////////////////////////////////////////////////////////// 118 { 119 LifecycleLib.stop ( looper ); 120 121 LifecycleLib.destroy ( looper ); 122 } 123 124 ////////////////////////////////////////////////////////////////////// 125 ////////////////////////////////////////////////////////////////////// 126 127 public boolean loop ( ) 128 ////////////////////////////////////////////////////////////////////// 129 { 130 try 131 { 132 EventQueue.invokeAndWait ( this ); 133 134 return true; 135 } 136 catch ( InterruptedException ex ) 137 { 138 } 139 catch ( Exception ex ) 140 { 141 ex.printStackTrace ( ); 142 } 143 144 return false; 145 } 146 147 public void run ( ) 148 ////////////////////////////////////////////////////////////////////// 149 { 150 LifecycleLib.update ( updatables ); 151 } 152 153 ////////////////////////////////////////////////////////////////////// 154 ////////////////////////////////////////////////////////////////////// 155 }