1 29 30 package nextapp.echo2.testapp.thousandmonkeys.factories; 31 32 import nextapp.echo2.app.Color; 33 import nextapp.echo2.app.Component; 34 import nextapp.echo2.testapp.thousandmonkeys.ComponentFactory; 35 36 40 public class GenericFactory 41 implements ComponentFactory { 42 43 46 private Class componentClass; 47 48 54 public GenericFactory(Class componentClass) { 55 super(); 56 this.componentClass = componentClass; 57 } 58 59 62 public Component newInstance() { 63 try { 64 Component component = (Component) componentClass.newInstance(); 65 switch ((int) (Math.random() * 3)) { 66 case 1: 67 component.setForeground(new Color((int) (16777216 * Math.random()) & 0x7f7f7f)); 68 component.setBackground(new Color((int) (16777216 * Math.random()) | 0xb0b0b0)); 69 break; 70 case 2: 71 component.setBackground(new Color((int) (16777216 * Math.random()))); 72 break; 73 case 3: 74 component.setForeground(new Color((int) (16777216 * Math.random()))); 75 break; 76 } 77 return component; 78 } catch (InstantiationException ex) { 79 throw new RuntimeException (ex); 80 } catch (IllegalAccessException ex) { 81 throw new RuntimeException (ex); 82 } 83 } 84 } 85 | Popular Tags |