View Javadoc

1   /* $Id: Route.java 26 2013-04-14 15:22:02Z mihlon $ */
2   
3   //////////////////////////////////////////////////////////////////////////////
4   //                                                                          //
5   //   This program is free software: you can redistribute it and/or modify   //
6   //   it under the terms of the GNU General Public License as published by   //
7   //   the Free Software Foundation, either version 3 of the License, or      //
8   //   at your option any later version.                                      //
9   //                                                                          //
10  //   This program is distributed in the hope that it will be useful,        //
11  //   but WITHOUT ANY WARRANTY; without even the implied warranty of         //
12  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
13  //   GNU General Public License for more details.                           //
14  //                                                                          //
15  //   You should have received a copy of the GNU General Public License      //
16  //   along with this program.  If not, see <http://www.gnu.org/licenses/>.  //
17  //                                                                          //
18  //////////////////////////////////////////////////////////////////////////////
19  
20  package cs.pancava.caltha.worlds;
21  
22  import java.awt.Color;
23  import java.awt.Graphics2D;
24  import java.awt.geom.Line2D;
25  
26  /**
27   * <p><b>Tato trida definuje vlastnosti spojů (cest) mezi jednotlivymi mistnosti
28   * ve vytvorenem svete.</b></p>
29   *
30   * @author Milan Vaclavik<br />
31   * @version $Revision: 26 $<br />
32   * $LastChangedBy: mihlon $<br />
33   */
34  public class Route extends GameEntity
35  {
36      /**
37       * Graficka reprezentace mistnosti v editoru.
38       */
39      private Line2D graphicsObject;
40  
41      /**
42       * Barva spoje.
43       */
44      private final Color ROUTE_COLOR = Color.MAGENTA;
45  
46      /**
47       * Herni objekt (prvni), ktery spojujeme.
48       */
49      private GameEntity prvniGE;
50  
51      /**
52       * Herni objekt (druhy), ktery spojujeme.
53       */
54      private GameEntity druhyGE;
55  
56      /**
57       * Konstruktor vytvarejici spoj (cestu) mezi dvema objekty (mistnostmi).
58       * @param ge1 : Herni objekt (prvni), ktery spojujeme.
59       * @param ge2 : Herni objekt (druhy), ktery spojujeme.
60       */
61      public Route(final GameEntity ge1, final GameEntity ge2)
62      {
63          super(World.WORLD_ROUTE);
64  
65          this.prvniGE = ge1;
66          this.druhyGE = ge2;
67  
68          this.graphicsObject = new Line2D.Double(this.prvniGE.getCenterX(), this.prvniGE.getCenterY(),
69                                                  this.druhyGE.getCenterX(), this.druhyGE.getCenterY());
70      }
71  
72      /**
73       * Vraci stred objektu v ose X.
74       * @return int : Stred objektu v ose X.
75       */
76      @Override
77      public final int getCenterX()
78      {
79          final int x1 = this.prvniGE.getCenterX();
80          final int x2 = this.druhyGE.getCenterX();
81  
82          final int x = Math.abs(x1 - x2) / 2;
83  
84          if (x1 < x2)
85          {
86              return x1 + x;
87          }
88          else
89          {
90              return x2 + x;
91          }
92      }
93  
94      /**
95       * Vraci stred objektu v ose Y.
96       * @return int : Stred objektu v ose Y.
97       */
98      @Override
99      public final int getCenterY()
100     {
101         final int y1 = this.prvniGE.getCenterY();
102         final int y2 = this.druhyGE.getCenterY();
103 
104         final int y = Math.abs(y1 - y2) / 2;
105 
106         if (y1 < y2)
107         {
108             return y1 + y;
109         }
110         else
111         {
112             return y2 + y;
113         }
114     }
115 
116     /**
117      * Vrati prvni mistnost, ktera je spojena s druhou mistnosti.
118      * @return gameEntity : Prvni mistnost, ktera je spojena s druhou mistnosti.
119      */
120     @Override
121     public final GameEntity getFirstRoom()
122     {
123         return this.prvniGE;
124     }
125 
126     /**
127      * Vraci grafickou reprezentaci spoje (cesty) v editoru.
128      * @return Line2D : Graficka reprezentace spoje (cesty) v editoru.
129      */
130     @Override
131     public final Object getGraphicsObject()
132     {
133         return this.graphicsObject;
134     }
135 
136     /**
137      * Vrati druhou mistnost, ktera je spojena s prvni mistnosti.
138      * @return gameEntity : Druha mistnost, ktera je spojena s prvni mistnosti.
139      */
140     @Override
141     public final GameEntity getSecondRoom()
142     {
143         return this.druhyGE;
144     }
145 
146     /**
147      * Zobrazi dany spoj (cestu)  v editoru.
148      * @param g2 Graphics2D : Graficky kontext, do ktereho se bude kreslit.
149      */
150     @Override
151     public final void showGraphicsObject(final Graphics2D g2)
152     {
153         // Nastaveni grafickeho objektu na novou (aktualni) pozici.
154         this.graphicsObject.setLine(this.prvniGE.getCenterX(), this.prvniGE.getCenterY(),
155                                     this.druhyGE.getCenterX(), this.druhyGE.getCenterY());
156 
157         g2.setColor(this.ROUTE_COLOR);
158 
159         g2.draw(this.graphicsObject);
160     }
161 }