package trackerModule.sim.map;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.text.DecimalFormat;
import trackerModule.core.datastructure.TDB;
import trackerModule.core.datastructure.Unit;
import trackerModule.sim.map.draw.UCanvas;
import trackerModule.sim.map.draw.UShape;
/**
* The Class GEntity.
* This is a special shape for an entity of simulation
*/
public class GEntity extends UShape implements MouseMotionListener{
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -364119376352390841L;
/** The ellipse. */
protected Ellipse2D ellipse;
/** The t unit. */
public Unit tUnit = null;
/** The show info. */
boolean showInfo = false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Instantiates a new g entity.
*
* @param c the canvas
* @param t the unit
* @param x the x
* @param y the y
*/
public GEntity(UCanvas c, Unit t, int x, int y){
super(c, null, x, y, 110, 110);
setUseSelection(false);
setName(t.getName());
setLabel(t.getName());
tUnit = t;
InitShape();
}
/**
* Initialize the shape.
*/
public void InitShape()
{
ellipse = new Ellipse2D.Double(GAP*5,GAP*5,getWidth()-1-GAP*2*5,getHeight()-1-GAP*2*5);
//set back color
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+tUnit.getName());
if( ship != null ){
String type = ship.get("Type").getData();
if( type.equalsIgnoreCase("FishingShip" ) ||
type.equalsIgnoreCase("BombingFishingShip" ) ||
type.equalsIgnoreCase("IllicitCargoFishingShip" ) )
setBackColor(Color.YELLOW);
else
if( type.equalsIgnoreCase("NavyShip" ))
setBackColor(Color.BLUE);
else
if( type.equalsIgnoreCase("MerchantShip" ) ||
type.equalsIgnoreCase("BombingMerchantShip" ) ||
type.equalsIgnoreCase("IllicitCargoMerchantShip" ) )
setBackColor(Color.GREEN);
else
setBackColor(Color.YELLOW);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Sets the show info.
*
* @param b the new show info
*/
public void setShowInfo( boolean b ){
showInfo = b;
}
/**
* Sets the position.
*
* @param x the x
* @param y the y
*/
public void setPosition(int x, int y){
setLocation(x - getWidth()/2, y - getHeight()/2);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Gets the show info.
*
* @return the show info
*/
public boolean getShowInfo(){
return showInfo;
}
/* (non-Javadoc)
* @see trackerModule.sim.map.draw.UShape#contain(double, double)
*/
public boolean contain(double x, double y){
return ellipse.contains((double)(x-getGlobalX()), (double)(y-getGlobalY()));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Draw
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* (non-Javadoc)
* @see trackerModule.sim.map.draw.UShape#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
InitShape();
Graphics2D g2 = (Graphics2D) g;
Unit u = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+tUnit.getName());
if(u == null)
return;
String strState = u.get("State").getData();
if( strState.indexOf("waiting") >=0 || strState.equalsIgnoreCase("end"))
return;
//text
g2.setColor(new Color(0,0,255,100));
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+tUnit.getName() +".Type").getData();
if( str.equals("Navy Ship"))
g2.drawOval(GAP*1,GAP*1,getWidth()-1-GAP*2*1,getHeight()-1-GAP*2*1);
g2.drawLine(getWidth()/2, getHeight()/2, getWidth()/2+10, getHeight()/2-10);
g2.drawLine(getWidth()/2+10, getHeight()/2-10, getWidth()/2+55, getHeight()/2-10);
if( getShowInfo() == true ){
DecimalFormat formatter = new DecimalFormat("0.00");
double d = Math.random();
if( d > 0.9 )
g2.setColor(new Color(255,0,0,100));
else
if( d > 0.5 )
g2.setColor(new Color(255,100,0,100));
else
g2.setColor(new Color(0,0,255,100));
g2.drawString("p=" + formatter.format(d), getWidth()/2+13, getHeight()/2);
}
g2.drawString(tUnit.getName(), getWidth()/2+13, getHeight()/2-13);
g2.setColor(new Color(0,0,255,100));
//circle
g2.setPaint( new GradientPaint( getWidth()/2, getHeight(), getBackColor(), getWidth()/2, 0,Color.white, false));
g2.fill(ellipse);
g2.setColor(getDrawColor());
g2.draw(ellipse);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Draw
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* (non-Javadoc)
* @see trackerModule.sim.map.draw.UShape#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent arg0){
super.mousePressed(arg0);
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS." +tUnit.getName());
MapFrame.This().setCurEntity(ship);
}
}