Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/zazof/jteg/gui/CountryCanvas.java


1   /*
2    *  Class CountryCanvas
3    *
4    *  This class contains the GUI information about a country.
5    *
6    *  Classauthors:
7    *      Yves Vandewoude  (yves@stcham.org)
8    */
9   
10  package org.zazof.jteg.gui;
11  
12  import org.zazof.jteg.*;
13  import java.awt.*;
14  import java.awt.image.*;
15  import java.awt.event.*;
16  
17  public class CountryCanvas extends Canvas{
18  
19  /**
20    *  Constructor that creates a CountryCanvas from a given country
21    *
22    *  @param The country that is used as the Domain-object for this Country 
23    */
24    
25    public CountryCanvas(Country country)
26    {
27      super();
28            $country = country;
29    }
30  
31  
32  
33  /**************************************
34   *
35   *  INSPECTOREN
36   *
37   **************************************/
38  
39  /**
40    *  Returns the name of the country associated with this CountryCanvas
41    *
42    *  @return The name of the country (e.g. Argentina)
43    *
44    */
45    public String getCountryName(){
46      return $country.getCountryName();
47    }
48  
49  
50  /**
51    *  Returns the number of the Country associated with this CountryCanvas
52    *
53    *  @return Number of the country (e.g. 1)
54    */  
55  
56    public int getID(){
57      return $country.getID();
58    }
59  
60  /**
61    *  Returns the player that owns the Country associated with this CountryCanvas
62    *
63    *  @return Owner of the country
64    */  
65  
66    public Player getOwner(){
67            return $country.getOwner();
68    }
69  
70  
71  /**
72    *  Returns the imagebitmap that is associated with this country
73    *
74    *  @return Image of the country
75    */  
76  
77    public Image getImage(){
78            return $country.getImage();
79    }
80  
81  
82  /**
83    *  Returns the x-coordinate of the absolute location of this country
84    *
85    *  @return Xcoordinate of the country
86    */  
87    
88    public int getAbsoluteXCoord(){
89      return $country.getAbsoluteXCoord();
90    }
91  
92  
93  /**
94    *  Returns the y-coordinate of the absolute location of this country
95    *
96    *  @return YCoordinate of the country
97    */  
98      public int getAbsoluteYCoord(){
99      return $country.getAbsoluteYCoord();
100   }
101 
102 
103 /**
104   *  Returns the number of armies currently present in this country
105   *
106   *  @return Number of armies in this country
107   */  
108 
109   public int getNumberOfArmies(){
110     return $country.getNumberOfArmies();
111   }
112 
113 
114 /**
115   *  Returns the country associated with this countrycanvas
116   *
117   *  @return The country associated with this canvas
118   */
119   public Country getCountry()
120     {
121       return $country;
122     }
123 
124 
125 /**
126   *  Returns the squared distance between position of the armies and point (x,y)
127   *
128   *  @return The squared distance between position of the armies and point (x,y)
129   */
130 
131   public int getDistanceTo(int x, int y){
132           int distance;
133     // army_x and army_y are the relative positions in respect tot the centre of the country
134     int centerX = ($country.getImage().getWidth(this))/2;
135           int centerY = ($country.getImage().getHeight(this))/2;
136     // absolute positions where they armies numbers are placed
137     int armiesX = $country.getAbsoluteXCoord()+centerX+($country.getArmiesX());
138     int armiesY = $country.getAbsoluteYCoord()+centerY+($country.getArmiesY());
139 
140     distance = (armiesX - x) * (armiesX - x) + (armiesY - y) * (armiesY - y);
141     return distance;
142   }
143 
144 
145 
146 // *****************************
147 // *  MUTATORS
148 // *****************************
149 
150 
151 /**
152   *  Sets the new owner of the country associated with this CountryCanvas
153   *
154   *  @param The player to be associated as owner of this country
155   */
156   public void setOwner(Player owner){
157     $country.setOwner(owner);
158   }
159 
160 
161 /**
162   *  Sets the number of armies present in the country to the new value
163   *
164   *  @param The number of armies to be placed in this country
165   */
166 
167   public void setNumberOfArmies(int armies){
168           $country.setNumberOfArmies(armies);
169   }
170 
171 
172 /**
173   *  The draw method from the Canvas Interface
174   *  Draws itself and its armies in the provided graphics2D environment
175   *  
176   *  @param The Graphics2D environment in which the country and its information will be drawn
177   */
178 
179   public void draw (Graphics2D g)
180   {
181   // This is just a standard draw of the countryImage
182     g.drawImage($country.getImage(),$country.getAbsoluteXCoord(),$country.getAbsoluteYCoord(),this);
183 
184   // First we determine the color of the circle
185         Player owner = $country.getOwner();
186           if (owner == null){
187             g.setColor(Color.gray);
188           }else{
189       if (DEBUG) System.out.println("CountryCanvas: " + owner.toString());
190             g.setColor(owner.getColor());
191     }
192 
193 
194     int nrOfArmies = $country.getNumberOfArmies();
195     
196     // If the nrOfArmies == 0 the game has not started yet, so we do not draw them
197     if (nrOfArmies != 0){
198       // Then we define the size of the Circle
199 
200       int circleSize = 12;
201             if (nrOfArmies > 9) circleSize = 20;
202             if (nrOfArmies > 99) circleSize = 22;
203 
204             // army_x and army_y are the relative positions in respect tot the centre of the country
205       int centerX = ($country.getImage().getWidth(this))/2;
206             int centerY = ($country.getImage().getHeight(this))/2;
207 
208       // draw the circle!
209             g.fillOval($country.getAbsoluteXCoord()+centerX+($country.getArmiesX()/2),
210                       $country.getAbsoluteYCoord()+centerY+($country.getArmiesY()/2), circleSize, circleSize);
211 
212       // draw the number of armies
213       // the font color is function of the player color: only black and white are accepted as font color
214       if (owner != null){
215         if (DEBUG) System.out.println("CountryCanvas owner: " + owner.toString() + " color: " + owner.getColor().toString());
216         Color oc = owner.getColor();
217         if (oc.equals(Color.black) || oc.equals(Color.blue) || oc.equals(Color.red)) g.setColor(Color.white); else g.setColor(Color.black); 
218       }else{
219         g.setColor(Color.black);
220       }
221         g.drawString(Integer.toString($country.getNumberOfArmies()), 
222                $country.getAbsoluteXCoord()+centerX+($country.getArmiesX()/2)+circleSize/4, 
223                $country.getAbsoluteYCoord()+centerY+($country.getArmiesY()/2)+circleSize*7/8); 
224 
225             // If the country is selected, we draw a white border, otherwise a black border
226             if ($country.isSelected())
227                  g.setColor(Color.white);
228             else
229               g.setColor(Color.black);
230 
231             g.drawOval($country.getAbsoluteXCoord()+centerX+($country.getArmiesX()/2),
232                         $country.getAbsoluteYCoord()+centerY+($country.getArmiesY()/2), circleSize, circleSize);
233     }
234   }
235 
236   
237 
238 /**
239   *  Changes the selectionvariable. 
240   */
241 
242   public void changeSelected(){
243     $country.changeSelected();
244   }   
245 
246   public void select(){
247           $country.select();
248   }
249 
250   public void unselect(){
251           $country.unselect();
252   }
253 
254 /*
255  *
256  *  Private Variables
257  *
258  *
259  */
260 
261   private Country $country;
262 
263   private static final boolean DEBUG = false;
264 }
265