1 36 37 40 41 42 package java2d; 43 44 import java.awt.*; 45 import javax.swing.JPanel ; 46 import javax.swing.border.EmptyBorder ; 47 import javax.swing.border.SoftBevelBorder ; 48 import javax.swing.border.CompoundBorder ; 49 50 import static java2d.CustomControlsContext.State.*; 51 52 56 public class DemoPanel extends JPanel { 57 58 public Surface surface; 59 public CustomControlsContext ccc; 60 public Tools tools; 61 public String className; 62 63 64 public DemoPanel(Object obj) { 65 setLayout(new BorderLayout()); 66 try { 67 if (obj instanceof String ) { 68 className = (String ) obj; 69 obj = Class.forName(className).newInstance(); 70 } 71 if (obj instanceof Component) { 72 add((Component) obj); 73 } 74 if (obj instanceof Surface) { 75 add("South", tools = new Tools(surface = (Surface) obj)); 76 } 77 if (obj instanceof CustomControlsContext) { 78 ccc = (CustomControlsContext) obj; 79 Component cmps[] = ccc.getControls(); 80 String cons[] = ccc.getConstraints(); 81 for (int i = 0; i < cmps.length; i++) { 82 add(cmps[i], cons[i]); 83 } 84 } 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 91 public void start() { 92 if (surface != null) 93 surface.startClock(); 94 if (tools != null && surface != null) { 95 if (tools.startStopB != null && tools.startStopB.isSelected()) { 96 surface.animating.start(); 97 } 98 } 99 if (ccc != null 100 && Java2Demo.ccthreadCB != null 101 && Java2Demo.ccthreadCB.isSelected()) 102 { 103 ccc.handleThread(START); 104 } 105 } 106 107 108 public void stop() { 109 if (surface != null) { 110 if (surface.animating != null) { 111 surface.animating.stop(); 112 } 113 surface.bimg = null; 114 } 115 if (ccc != null) { 116 ccc.handleThread(STOP); 117 } 118 } 119 120 121 public void setDemoBorder(JPanel p) { 122 int top = (p.getComponentCount()+1 >= 3) ? 0 : 5; 123 int left = ((p.getComponentCount()+1) % 2) == 0 ? 0 : 5; 124 EmptyBorder eb = new EmptyBorder (top,left,5,5); 125 SoftBevelBorder sbb = new SoftBevelBorder (SoftBevelBorder.RAISED); 126 setBorder(new CompoundBorder (eb, sbb)); 127 } 128 } 129 | Popular Tags |