import sub_arctic.lib.*; import sub_arctic.constraints.std_function; import sub_arctic.constraints.constraint; import sub_arctic.input.callback_object; import sub_arctic.input.event; import sub_arctic.output.drawable; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.io.*; import java.util.*; /** * Applet to demo of spring example * @author Khai Truong */ public class spring_example extends debug_interactor_applet implements callback_object, cycle_handler { //---------------------------------------------------------------------------- // MAIN METHOD //---------------------------------------------------------------------------- /** * Main method * @param argv is the array of String command line arguments */ public static void main(String argv[]) { spring_example se = new spring_example(); interactor_frame f = new interactor_frame("Spring Example",se,650,300); f.setVisible(true); } // end of main() public static final int parent_width = 400; public static final int parent_height = 150; public static final int object_width = 50; public static final int object_height = 30; public static final int strut_width = 50; public static final int strut_height = 30; public static final int spring_height = 30; protected labeled_object parent_obj, obj1, obj2, obj3; protected labeled_strut st1, st2; protected labeled_spring sp1, sp2; protected scale a_slider; /** * Initialization of sub_arctic interface when applet starts */ public void build_ui(base_parent_interactor top) { Font lab_font = new Font("Helvetica",Font.BOLD,12); //// build overall column column ui_col = new column(2,column.LEFT_JUSTIFIED); ui_col.set_pos(10,10); top.add_child(ui_col); //// Put a title label in ui_col.add_child(new label("Springs, Struts & Objects example",new Font("Helvetica", Font.BOLD, 16))); //// build our objects parent_obj = new labeled_object(parent_width,parent_height,"parent_obj"); obj1 = new labeled_object(object_width,object_height,"obj1"); obj2 = new labeled_object(object_width,object_height,"obj2"); obj3 = new labeled_object(object_width,object_height,"obj3"); st1 = new labeled_strut(strut_width,strut_height,"st1"); st2 = new labeled_strut(strut_width,strut_height,"st2"); int sp_s = (parent_obj.w() - obj1.w() - obj2.w() - obj3.w() - st1.w() - st2.w()) / 2; sp1 = new labeled_spring(sp_s,spring_height,"sp1"); sp2 = new labeled_spring(sp_s,spring_height,"sp2"); //// put them in our test hierarchy parent_obj.add_child(st1); st1.set_x(0); st1.set_y(parent_obj.h()/2 - st1.h()/2); parent_obj.add_child(obj1); obj1.set_x(st1.w()); obj1.set_y(parent_obj.h()/2 - obj1.h()/2); parent_obj.add_child(sp1); sp1.set_x(st1.w() + obj1.w()); sp1.set_y(parent_obj.h()/2 - sp1.h()/2); parent_obj.add_child(obj2); obj2.set_x(parent_obj.w()/2-obj2.w()/2); obj2.set_y(parent_obj.h()/2 - obj2.h()/2); parent_obj.add_child(sp2); sp2.set_x(parent_obj.w()-st2.w()-obj3.w()-sp2.w()); sp2.set_y(parent_obj.h()/2 - sp2.h()/2); parent_obj.add_child(obj3); obj3.set_x(parent_obj.w()-st2.w()-obj3.w()); obj3.set_y(parent_obj.h()/2 - obj3.h()/2); parent_obj.add_child(st2); st2.set_x(parent_obj.w()-st2.w()); st2.set_y(parent_obj.h()/2 - st2.h()/2); ui_col.add_child(parent_obj); a_slider = new scale(5,0, 250, 10,200, 100, 10, this); a_slider.set_value(0); ui_col.add_child(a_slider); //// constraints //// keep struts where they are st1.set_x_constraint(std_function.offset(PARENT.X(), 0)); st2.set_x_constraint(std_function.offset(PARENT.W(), -(st2.w()+1))); //// keep obj2 centered obj2.set_x_constraint(std_function.centered(PARENT.W(), 0)); //// move obj1 and obj3 obj1.set_x_constraint(std_function.offset(OTHER.OBJ(st1).W(), 0)); obj3.set_x_constraint(std_function.offset(OTHER.OBJ(st2).X(), -obj3.w())); //// move sp1 and sp2 sp1.set_x_constraint(std_function.offset(OTHER.OBJ(obj1).X2(), 0)); sp1.set_w_constraint(std_function.subtract(OTHER.OBJ(obj2).X1(), OTHER.OBJ(obj1).X2(),0)); sp2.set_x_constraint(std_function.offset(OTHER.OBJ(obj2).X2(), 0)); sp2.set_w_constraint(std_function.subtract(OTHER.OBJ(obj3).X1(), OTHER.OBJ(obj2).X2(),0)); /* setup to catch cycles in constraints with callback to us */ manager.handle_cycles_with(manager.EXCEPTION_CUSTOM, this); } /** * Handle callbacks. */ public void callback(interactor from, event evt, int cb_num, Object cb_parm) { if (from == a_slider) { parent_obj.set_w(parent_width + a_slider.value()); } } /** * Handle cycle_detection */ public boolean handle_cycle(interactor in_obj, int part_code) { //// chance to handle cycles... return true; } } // end of spring_example class