1 package org.woehlke.computer.kurzweil.tabs.cca;
2
3
4 import lombok.EqualsAndHashCode;
5 import lombok.Getter;
6 import lombok.ToString;
7 import lombok.extern.log4j.Log4j2;
8 import org.woehlke.computer.kurzweil.commons.tabs.TabCanvas;
9 import org.woehlke.computer.kurzweil.commons.layouts.LayoutCanvas;
10 import org.woehlke.computer.kurzweil.tabs.cca.canvas.CyclicCellularAutomatonColorScheme;
11
12 import javax.swing.*;
13 import javax.swing.border.Border;
14 import java.awt.*;
15 import java.io.Serializable;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20
21
22
23
24
25
26
27
28
29
30
31
32 @Log4j2
33 @Getter
34 @ToString(callSuper = true, exclude = {"tabCtx","border","preferredSize","layout","colorScheme","cyclicCellularAutomatonModel"})
35 @EqualsAndHashCode(callSuper=true, exclude = {"tabCtx","border","preferredSize","layout","colorScheme","cyclicCellularAutomatonModel"})
36 public class CyclicCellularAutomatonCanvas extends JComponent implements
37 Serializable, TabCanvas, CyclicCellularAutomaton, Future<Void> {
38
39 private static final long serialVersionUID = -3057254130516052936L;
40
41 private final CyclicCellularAutomatonContext tabCtx;
42 private final Border border;
43 private final Dimension preferredSize;
44 private final LayoutCanvas layout;
45 private final CyclicCellularAutomatonColorScheme colorScheme;
46 private final CyclicCellularAutomatonModel cyclicCellularAutomatonModel;
47
48 private final static int startX = 0;
49 private final static int startY = 0;
50 private final int worldX;
51 private final int worldY;
52
53 public CyclicCellularAutomatonCanvas(CyclicCellularAutomatonContext tabCtx) {
54 this.tabCtx = tabCtx;
55 this.border = this.tabCtx.getCtx().getCanvasBorder();
56 this.worldX = this.tabCtx.getCtx().getWorldDimensions().getX();
57 this.worldY = this.tabCtx.getCtx().getWorldDimensions().getY();
58 this.layout = new LayoutCanvas(this);
59 this.preferredSize = new Dimension(worldX,worldY);
60 this.colorScheme = new CyclicCellularAutomatonColorScheme();
61 this.setLayout(layout);
62 this.setPreferredSize(preferredSize);
63 this.setMinimumSize(preferredSize);
64 this.setMaximumSize(preferredSize);
65 this.setSize(this.worldX,this.worldY);
66 this.cyclicCellularAutomatonModel = new CyclicCellularAutomatonModel(this.tabCtx);
67 showMe();
68 }
69
70 public void paint(Graphics g) {
71
72 int x;
73 int y;
74 int state;
75 Color stateColor;
76 if (this.cyclicCellularAutomatonModel.getLattice() != null) {
77 for (y = 0; y < worldY; y++) {
78 for (x = 0; x < worldX; x++) {
79 state = this.cyclicCellularAutomatonModel.getState(x,y);
80 stateColor = this.colorScheme.getColorForState(state);
81 g.setColor(stateColor);
82 g.drawLine(x, y, x, y);
83 }
84 }
85 }
86 super.paintComponent(g);
87
88 }
89
90 public void update(Graphics g) {
91
92 paint(g);
93 }
94
95 @Override
96 public void showMe() {
97 log.info("showMe "+this.toString());
98 }
99
100 @Override
101 public boolean cancel(boolean mayInterruptIfRunning) {
102 return false;
103 }
104
105 @Override
106 public boolean isCancelled() {
107 return false;
108 }
109
110 @Override
111 public boolean isDone() {
112 return false;
113 }
114
115 @Override
116 public Void get() throws InterruptedException, ExecutionException {
117 return null;
118 }
119
120 @Override
121 public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
122 return null;
123 }
124 }