001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Feb 14, 2003 005 * Time: 9:34:32 PM 006 */ 007 008 package EVolve.util.painters.shapes; 009 010 import java.awt.Color; 011 import java.awt.Graphics2D; 012 import java.awt.Rectangle; 013 import java.util.HashMap; 014 import java.util.Iterator; 015 016 public abstract class Shape implements Cloneable{ 017 public int x, y; 018 protected long entity_id; 019 protected long entity_type; 020 protected HashMap consumerMap; 021 protected Color color; 022 protected Color defaultColor = Color.black; 023 024 public Shape(long entity_type, long entity_id) { 025 this.entity_id = entity_id; 026 this.entity_type = entity_type; 027 consumerMap = new HashMap(); 028 color = null; 029 } 030 031 public long getEntityID() { 032 return entity_id; 033 } 034 035 public Rectangle getPosition() { 036 return new Rectangle(x,y,1,1); 037 } 038 039 public void move(int x, int y) { 040 this.x = x<0 ? 0:x; 041 this.y = y<0 ? 0:y; 042 } 043 044 public void addConsumer(Shape consumer, int gravity) { 045 consumerMap.put(consumer,new Integer(gravity)); 046 } 047 048 public HashMap getConsumers() { 049 return consumerMap; 050 } 051 052 public long getEntityType() { 053 return entity_type; 054 } 055 056 public void setColor(Color color) { 057 this.color = color; 058 } 059 060 public Color getColor() { 061 return color; 062 } 063 064 protected void drawArrows(Graphics2D g2) { 065 Iterator it2 = consumerMap.keySet().iterator(); 066 while (it2.hasNext()) { 067 Shape consumer = (Shape)it2.next(); 068 int gravity = ((Integer)consumerMap.get(consumer)).intValue(); 069 // draw arrows 070 int x1 = x+getWidth()/2, y1 = y+getHeight()/2; 071 int x2 = consumer.x + consumer.getWidth()/2, y2 = consumer.y + consumer.getHeight()/2; 072 g2.drawLine(x1,y1,x2,y2); 073 double len = java.lang.Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); 074 double angle = java.lang.Math.asin(java.lang.Math.abs(y1-y2)/len); 075 if (x1>x2) { 076 if (y1 < y2) 077 angle = 2*java.lang.Math.PI - angle; 078 } else { 079 if (y1 > y2) 080 angle = java.lang.Math.PI - angle; 081 else 082 angle = java.lang.Math.PI + angle; 083 } 084 g2.drawLine(x2,y2,(int)(x2+10*java.lang.Math.cos(angle-0.5236)),(int)(y2+10*java.lang.Math.sin(angle-0.5236))); 085 g2.drawLine(x2,y2,(int)(x2+10*java.lang.Math.cos(angle+0.5236)),(int)(y2+10*java.lang.Math.sin(angle+0.5236))); 086 g2.drawString(""+gravity+"",(x1+(x2-x1)/5),(y1-(y1-y2)/5)); 087 } 088 } 089 090 public Object clone() { 091 Shape o = null; 092 try { 093 o = (Shape)super.clone(); 094 } catch (CloneNotSupportedException e) { 095 e.printStackTrace(); 096 return null; 097 } 098 o.consumerMap = new HashMap(); 099 return o; 100 } 101 102 public abstract void draw(Graphics2D g); 103 104 public abstract void fill(Graphics2D g); 105 106 public abstract String getName(); 107 108 public abstract boolean insideShape(int x, int y); 109 110 public abstract int getWidth(); 111 112 public abstract int getHeight(); 113 114 public abstract void setSize(int width, int height); 115 }