1 package org.woehlke.computer.kurzweil.application; 2 3 import lombok.Getter; 4 import lombok.ToString; 5 import lombok.extern.log4j.Log4j2; 6 import org.woehlke.computer.kurzweil.commons.Startable; 7 import org.woehlke.computer.kurzweil.commons.model.LatticePoint; 8 import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.cell.CellCore; 9 import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.cell.CellLifeCycle; 10 11 import javax.swing.*; 12 import javax.swing.border.Border; 13 import javax.swing.border.CompoundBorder; 14 import java.util.Date; 15 import java.util.Random; 16 17 @Log4j2 18 @Getter 19 @ToString(exclude={"random","frame"},callSuper=true) 20 public class ComputerKurzweilContext implements Startable { 21 22 private final Random random; 23 private final ComputerKurzweilProperties properties; 24 private final ComputerKurzweilFrame frame; 25 26 public ComputerKurzweilContext( 27 ComputerKurzweilProperties computerKurzweilProperties, 28 ComputerKurzweilFrame frame 29 ) { 30 this.frame = frame; 31 this.properties = computerKurzweilProperties; 32 long seed = new Date().getTime(); 33 this.random = new Random(seed); 34 } 35 36 public CompoundBorder getTabbedPaneBorder() { 37 return getBorder(); 38 } 39 40 public CompoundBorder getFrameBorder(){ 41 return getBorder(); 42 } 43 44 public CompoundBorder getBorder(){ 45 int left = this.getProperties().getAllinone().getView().getBorderPaddingX(); 46 int right = this.getProperties().getAllinone().getView().getBorderPaddingX(); 47 int top = this.getProperties().getAllinone().getView().getBorderPaddingY(); 48 int bottom = this.getProperties().getAllinone().getView().getBorderPaddingY(); 49 return BorderFactory.createCompoundBorder( 50 BorderFactory.createEmptyBorder(), 51 BorderFactory.createEmptyBorder(left,right,top,bottom) 52 ); 53 } 54 55 public CompoundBorder getBorder(String label){ 56 int top = this.getProperties().getAllinone().getView().getBorderPaddingY(); 57 int left = this.getProperties().getAllinone().getView().getBorderPaddingX(); 58 int bottom = this.getProperties().getAllinone().getView().getBorderPaddingY(); 59 int right = this.getProperties().getAllinone().getView().getBorderPaddingX(); 60 return BorderFactory.createCompoundBorder( 61 BorderFactory.createTitledBorder(label), 62 BorderFactory.createEmptyBorder(top,left,bottom,right) 63 ); 64 } 65 66 private CompoundBorder getDoubleBorder(){ 67 int left = this.getProperties().getAllinone().getView().getBorderPaddingX(); 68 int right = this.getProperties().getAllinone().getView().getBorderPaddingX(); 69 int top = this.getProperties().getAllinone().getView().getBorderPaddingY(); 70 int bottom = this.getProperties().getAllinone().getView().getBorderPaddingY(); 71 return BorderFactory.createCompoundBorder( 72 BorderFactory.createEmptyBorder(left,right,top,bottom), 73 BorderFactory.createEmptyBorder(left,right,top,bottom) 74 ); 75 } 76 77 private CompoundBorder getDoubleBorder(String label){ 78 int left = this.getProperties().getAllinone().getView().getBorderPaddingX(); 79 int right = this.getProperties().getAllinone().getView().getBorderPaddingX(); 80 int top = this.getProperties().getAllinone().getView().getBorderPaddingY(); 81 int bottom = this.getProperties().getAllinone().getView().getBorderPaddingY(); 82 return BorderFactory.createCompoundBorder( 83 BorderFactory.createEmptyBorder(left,right,top,bottom), 84 BorderFactory.createEmptyBorder(left,right,top,bottom) 85 ); 86 } 87 88 public CompoundBorder getBottomButtonsPanelBorder(){ 89 return getDoubleBorder(); 90 } 91 92 public CompoundBorder getBottomButtonsPanelBorder(String label){ 93 return getDoubleBorder(label); 94 } 95 96 private Border getZeroBorder() { 97 int top = 0; 98 int left = 0; 99 int bottom = 0; 100 int right = 0; 101 return BorderFactory.createEmptyBorder(top,left,bottom,right); 102 } 103 104 public Border getTabBorder() { 105 return getZeroBorder(); 106 } 107 108 public Border getCanvasBorder() { 109 return getZeroBorder(); 110 } 111 112 public LatticePoint getWorldDimensions(){ 113 int x = this.properties.getAllinone().getLattice().getWidth(); 114 int y = this.properties.getAllinone().getLattice().getHeight(); 115 return new LatticePoint(x,y); 116 } 117 118 public LatticePoint getNextRandomLatticePoint() { 119 int x = this.properties.getAllinone().getLattice().getWidth(); 120 int y = this.properties.getAllinone().getLattice().getHeight(); 121 int nextX = this.getRandom().nextInt(x); 122 int nextY = this.getRandom().nextInt(y); 123 LatticePointeil/commons/model/LatticePoint.html#LatticePoint">LatticePoint p = new LatticePoint(nextX,nextY); 124 p.normalize(this.getWorldDimensions()); 125 p.absoluteValue(); 126 return p; 127 } 128 129 public CellLifeCycle getNewCellLifeCycle() { 130 return new CellLifeCycle(this.properties.getSimulatedevolution().getCellConf()); 131 } 132 133 public CellCore getNewCellCore() { 134 return new CellCore(this); 135 } 136 137 public void start() { 138 frame.start(); 139 } 140 141 public void stop() { 142 frame.stop(); 143 } 144 145 }