View Javadoc

1   
2   package org.molwind.chemical.view;
3   
4   /*
5    * This file is part of Molwind.
6    *
7    * Molwind is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Molwind is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  import java.awt.Color;
22  import java.awt.Graphics;
23  import java.awt.image.BufferedImage;
24  import java.util.Iterator;
25  
26  import org.molwind.chemical.model.ChemicalWorld;
27  import org.molwind.model.LayeredPosition;
28  import org.molwind.view.Tile;
29  import org.molwind.view.TileException;
30  import org.molwind.view.TileGenerator;
31  import org.molwind.view.TopologyManager;
32  
33  /**
34   * ChemicalTileGenerator generates tiles containing chemical compounds.
35   *
36   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
37   * @version 1.0
38   */
39  public class ChemicalTileGenerator implements TileGenerator {
40  
41      private ChemicalWorld world;
42  
43      private static BufferedImage emptyImage;
44  
45      /**
46       * Create a new tile generator based on the given chemical world.
47       *
48       * @param chemicalWorld
49       *      the chemical world
50       */
51      public ChemicalTileGenerator(final ChemicalWorld chemicalWorld) {
52          this.world = chemicalWorld;
53      }
54  
55      private BufferedImage getEmptyImage() {
56          if (emptyImage == null) {
57              emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
58              Graphics graphic = emptyImage.getGraphics();
59              graphic.setColor(Color.white);
60              graphic.fillRect(0, 0, 1, 1);
61          }
62          return emptyImage;
63      }
64  
65      private Iterator getVisibleEntities(final LayeredPosition position) {
66          TopologyManager topologyManager = world.getTopologyManager();
67  	topologyManager.setEntityIterator( world.getEntityIterator() );
68  	topologyManager.setRelationshipResolver( world.getRelationshipResolver() );
69          return topologyManager.getVisibleEntities(position);
70      }
71  
72  
73      /**
74       * Generates a tile from the given position.
75       *
76       * @param position
77       *      the position
78       * @return
79       *      a tile object represented by the given position
80       * @throws org.molwind.view.TileException
81       *      is thrown upon tile error
82       */
83      public Tile generateTile(final LayeredPosition position)
84      throws TileException {
85  	
86          ChemicalTile chemicalTile = new ChemicalTile(position);
87          chemicalTile.setWorldName(world.getName());
88          chemicalTile.addEntities(getVisibleEntities(position));
89          return chemicalTile;
90      }
91  
92  }