Source code: com/sunwheeltech/examples/boidbox/BoidBox.java
1 package com.sunwheeltech.examples.boidbox;
2
3 /* GPL
4 ulunum java libraries for complex simulation modelling,
5 3d graphics, peer-to-peer networking and other purposes
6
7 version 0.1 released December 2001
8 see the file contents.html for a quick description of whats in
9 each package, and what you can expect to do with it
10
11 Copyright (C) December 2001 Dave Crane dave@cranepeople.co.uk
12
13
14 Find the GNU public license at:
15
16 http://www.gnu.org/copyleft/gpl.html
17
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 as published by the Free Software Foundation; either version 2
21 of the License, or (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 */
32
33 //--- required by the ObjectBrowser bean ----------------------------------
34 //the package holding the Object Browser bean
35 import com.sunwheeltech.sirius.*;
36 import com.sunwheeltech.sirius.objbrowser.*;
37
38 //--- required by the object model for this example only ------------------
39 //package containing the legacy bird-flocking application
40 import ulu.sim.boid.*;
41 //package containing support graphics classes for the bird-flocks
42 import ulu.pict.*;
43 //utility package holding some scripting support amongst other things
44 import ulu.ut.*;
45
46 //--- standard java libraries ---------------------------------------------
47 import java.awt.*;
48 import java.awt.event.*;
49 import java.io.*;
50 import java.net.*;
51 import java.util.*;
52 import javax.swing.*;
53
54 /**<p>A simple example application using the Uncle Unc framework to expose the methods and
55 * properties of a java application.
56 *
57 *<p>The application being used here is a bird-flock simulation that I wrote a few years ago.
58 *Bird flocking algorithms are a cute technique for creating organic-looking movement in
59 *particles, and was commonly used in the days of java applets to make pretty pictures on a
60 *web page. Something about our hunter-getherer origins makes them appealing to watch.
61 *
62 *<p>The bird-flock was <b>not</b> designed with Uncle Unc in mind, and the object browser has
63 *been fitted onto it retroactively. This choice was deliberate, as the boid box is intended to
64 *demonstrate how easy it is to apply the Objectbrowser to any existing piece of java code.
65 *
66 *@author Dave Crane Sunwheel Technologies Ltd June 2003
67 */
68 public class BoidBox extends JFrame{
69
70 /** the browser component */
71 ObjectBrowser browser=null;
72 /** the drawing surface used to visualise the flock of birds */
73 PictPanel panel=null;
74 /** the object that renders the simulation */
75 BoidPict pict=null;
76 /** the bird flock data model */
77 Flock flock=null;
78
79 /** constructor sets up the GUI (including the browser) and the bird-flock
80 application's data model, and links them together
81 @param url url of startup script
82 */
83 public BoidBox(URL url){
84
85 Container cp=getContentPane();
86
87 browser=new ObjectBrowser();
88 flock=new Flock();
89 browser.addToScript("flock",flock);
90
91 setTitle("boid box");
92 cp.setLayout(new GridLayout(1,1));
93
94 pict=new BoidPict();
95 pict.setFlock(flock);
96 panel=new PictPanel();
97 panel.setPreferredSize(new Dimension(300,500));
98 panel.setPict(pict);
99 panel.setBackground(Color.white);
100 JPanel pholder=new JPanel(new FlowLayout(FlowLayout.CENTER));
101 pholder.add(panel);
102
103 browser.addTreeBrowserListener(
104 new TreeBrowserListener(){
105 public void respond(TreeBrowserEvent event){
106 try{
107 Boid boid=(Boid)(event.getItemReference());
108 pict.setSelected(boid);
109 panel.repaint();
110 }catch (Exception ex){
111 pict.setSelected(null);
112 ex.printStackTrace();
113 }
114 }
115 }
116 );
117 browser.addToScript("panel",panel);
118
119
120 try{
121 browser.setScriptSource(url);
122 }catch (IOException ex){ ex.printStackTrace(); }
123 browser.setObject(flock);
124 browser.addMethod("run","Sim","startSim()");
125 browser.addMethod("pause","Sim","stopSim()");
126
127 JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,browser,pholder);
128 split.setDividerLocation(300);
129 split.setDividerSize(8);
130 cp.add(split);
131
132
133 addWindowListener(
134 new WindowAdapter(){
135 public void windowClosing(WindowEvent ev){
136 ev.getWindow().dispose();
137 System.exit(0);
138 }
139 }
140 );
141 pack();
142 setVisible(true);
143 }
144
145 /** command-line invocation simply starts up a new boid box */
146 public static void main(String[] args){
147 Object[] options = {"Start BoidBox","Cancel"};
148 Icon icon=null;
149 try{
150 ulu.view.io.URLUtil.ensureURLHandlersLoaded();
151 icon=new ImageIcon(new URL("jarclass://com.sunwheeltech.sirius.objbrowser.ObjectBrowser/img/beans/objbrowser.png"));
152 }catch (Exception ex){}
153 int n = JOptionPane.showOptionDialog(
154 null,
155 "Double-clicking on this jar file\n will start up the example application,\n called 'BoidBox'.\n\nIt's described in more detail\n in the tutorial book that\n accompanies this component.\n\nThis is also the main deployment jar\n for using the bean, we just\n bundled them all together for\n convenience.\n\nDo you want to launch the BoidBox now?",
156 "Sunwheel Technologies Ltd.",
157 JOptionPane.YES_NO_OPTION,
158 JOptionPane.QUESTION_MESSAGE,
159 icon,
160 options,
161 options[0]
162 );
163 if (n==JOptionPane.YES_OPTION){
164 launchApp(args);
165 }else{
166 System.exit(0);
167 }
168 }
169
170 private static void launchApp(String[] args){
171 URL scripturl=null;
172 if (args.length==0){
173 //default to the startup script located in the deployment jar file using a special Uncle Unc
174 //protocol handler that will always be
175 try{
176 scripturl=new URL("jarclass://com.sunwheeltech.sirius.objbrowser.ObjectBrowser/com/sunwheeltech/examples/boidbox/startup.py");
177 }catch (Exception ex){
178 System.out.println("Can't locate default startup script");
179 ex.printStackTrace();
180 }
181 }else{
182 try{
183 File f=new File(args[0]);
184 scripturl=f.toURL();
185 }catch (Exception ex){
186 System.out.println("can't find the startup file '"+args[0]+"'");
187 ex.printStackTrace();
188 }
189 }
190 BoidBox box=new BoidBox(scripturl);
191 }
192 }
193