View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.dla;
2   
3   import lombok.EqualsAndHashCode;
4   import lombok.Getter;
5   import lombok.ToString;
6   import lombok.extern.log4j.Log4j2;
7   import org.woehlke.computer.kurzweil.commons.model.LatticePoint;
8   import org.woehlke.computer.kurzweil.commons.layouts.LayoutCanvas;
9   import org.woehlke.computer.kurzweil.commons.tabs.TabCanvas;
10  
11  import javax.swing.*;
12  import javax.swing.border.Border;
13  import java.awt.*;
14  import java.io.Serializable;
15  
16  
17  /**
18   * Diffusion Limited Aggregation.
19   *
20   * (C) 2006 - 2013 Thomas Woehlke.
21   * https://thomas-woehlke.blogspot.com/2016/01/diffusion-limited-aggregation.html
22   * @author Thomas Woehlke
23   *
24   * Date: 05.02.2006
25   * Time: 00:51:51
26   */
27  @Log4j2
28  @Getter
29  @ToString(callSuper = true, exclude = {"tabCtx","border","preferredSize","layout","tabModel"})
30  @EqualsAndHashCode(callSuper=true, exclude = {"tabCtx","border","preferredSize","layout","tabModel"})
31  public class DiffusionLimitedAggregationCanvas extends JComponent implements
32      Serializable, TabCanvas, DiffusionLimitedAggregation {
33  
34      private final DiffusionLimitedAggregationContext tabCtx;
35      private final DiffusionLimitedAggregationModel tabModel;
36      private final Border border;
37      private final Dimension preferredSize;
38      private final LayoutCanvas layout;
39  
40      private final Color MEDIUM = Color.BLACK;
41      private final Color PARTICLES = Color.BLUE;
42      private final int directionsFirst = 0;
43      private final static int startX = 0;
44      private final static int startY = 0;
45      private final int worldX;
46      private final int worldY;
47  
48      public DiffusionLimitedAggregationCanvas(
49          DiffusionLimitedAggregationContext tabCtx
50      ) {
51          this.tabCtx = tabCtx;
52          this.tabModel = new DiffusionLimitedAggregationModel( this.tabCtx );
53          this.border = this.tabCtx.getCtx().getCanvasBorder();
54          worldX = this.tabCtx.getCtx().getWorldDimensions().getX();
55          worldY = this.tabCtx.getCtx().getWorldDimensions().getY();
56          this.layout = new LayoutCanvas(this);
57          this.preferredSize = new Dimension(worldX,worldY);
58  
59          this.setBackground(MEDIUM);
60          this.setLayout(layout);
61          this.setPreferredSize(preferredSize);
62          this.setMinimumSize(preferredSize);
63          this.setMaximumSize(preferredSize);
64          this.setSize(worldX,worldY);
65  
66      }
67  
68      public void paint(Graphics g) {
69          //log.info("paint");
70          super.paintComponent(g);
71          g.setColor(MEDIUM);
72          g.fillRect(startX, startY, worldX, worldY);
73          g.setColor(PARTICLES);
74          Color ageColor;
75          int x;
76          int y;
77          //paint moving Particles
78          for(LatticePoint particle :  this.tabModel.getParticles()){
79              x = particle.getX();
80              y = particle.getY();
81              g.drawLine(x,y,x,y);
82          }
83          int myAge = 0;
84          //paint dendrite Particles
85          for(y=0; y < worldY; y++){
86              for(x=0; x < worldX; x++){
87                  myAge =   this.tabModel.getAgeFor(x,y);
88                  //myAge = worldMap[x][y];
89                  //if is part of dendrite
90                  if(myAge > 0) {
91                      // color from age
92                      myAge /= 25;
93                      int blue = (myAge / 256) % (256 * 256);
94                      int green = (myAge % 256);
95                      int red = 255;
96                      ageColor = new Color(red, green, blue);
97                      g.setColor(ageColor);
98                      g.drawLine(x,y,x,y);
99                      //log.info("paint: age "+myAge+" x="+x+",y="+y+" with color: red="+red+", green="+green+", blue="+blue+" ");
100                 }
101             }
102         }
103     }
104 
105     public void update(Graphics g) {
106         //log.info("update(Graphics g)");
107         paint(g);
108     }
109 
110     @Override
111     public void showMe() {
112 
113     }
114 }