001 package aima.gui.framework; 002 003 /** 004 * In this framework a graphical agent application consists of at least 005 * three parts: An {@link AgentAppModel}, an {@link AgentAppFrame}, and 006 * an {@link AgentAppController}. This class demonstrates, how this three 007 * parts are plugged together. The easiest way to create a new graphical agent 008 * application is to create subclasses of the three parts as needed, and 009 * then to subclass this class and override the three factory methods. 010 * @author R. Lunde 011 */ 012 public class SimpleAgentAppDemo { 013 /** 014 * Creates an agent application, makes the parts know 015 * each other, and finally sets the frame visible. 016 */ 017 public void startApplication() { 018 AgentAppModel model = createModel(); 019 AgentAppFrame frame = createFrame(); 020 AgentAppController controller = createController(); 021 frame.setController(controller); 022 frame.setModel(model); 023 controller.setFrame(frame); 024 controller.setModel(model); 025 model.addModelChangedListener(frame); 026 frame.setVisible(true); 027 frame.setDefaultSelection(); 028 } 029 030 /** Factory method, responsible for creating the model. */ 031 public AgentAppModel createModel() { 032 return new AgentAppModel(); 033 } 034 035 /** 036 * Factory method, responsible for creating the frame. This 037 * implementation shows how the {@code AgentAppFrame} can be 038 * configured with respect to the needs of the application 039 * even without creating a subclass. 040 */ 041 public AgentAppFrame createFrame() { 042 AgentAppFrame result = new AgentAppFrame(); 043 result.setAgentView(new AgentView()); 044 result.setSelectors( 045 new String[]{"XSelect", "YSelect"}, 046 new String[]{"Select X", "Select Y"}); 047 result.setSelectorItems("XSelect", new String[]{"X1 (Small)", "X2 (Large)"}, 1); 048 result.setSelectorItems("YSelect", new String[]{"Y=1", "Y=2", "Y=3"}, 0); 049 result.setTitle("Demo Agent Application"); 050 result.setSplitPaneResizeWeight(0.5); // puts split bar in center position. 051 result.setSize(600, 400); 052 result.setUpdateDelay(500); 053 return result; 054 } 055 056 /** Factory method, responsible for creating the controller. */ 057 public AgentAppController createController() { 058 return new AgentAppController(); 059 } 060 061 062 ///////////////////////////////////////////////////////////////// 063 // main method for testing 064 065 /** 066 * Starts a simple test frame application. 067 */ 068 public static void main(String args[]) { 069 new SimpleAgentAppDemo().startApplication(); 070 } 071 } 072