001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Feb 14, 2003 005 * Time: 8:45:05 PM 006 */ 007 008 package EVolve.util.painters; 009 010 import java.util.*; 011 import EVolve.util.painters.shapes.*; 012 import EVolve.visualization.AutoImage; 013 014 public class RelationshipPainter extends Painter{ 015 private int[][] value; 016 private HashMap[] axis; 017 private int shapeSize; 018 private int threshold; 019 private long entity_type_1, entity_type_2; 020 021 public RelationshipPainter() { 022 axis = new HashMap[2]; 023 } 024 025 public String getName() { 026 return "Relationship Painter"; 027 } 028 029 public void updatePainterParameters(int[][] value, int threshold) { 030 this.value = value; 031 this.threshold = threshold; 032 } 033 034 public void paint(AutoImage image, long x, long y, long z) { 035 /* 036 here x and y stands for the entity type of 037 two axis, z stands for the edge length of the box 038 or diameter of the ball 039 */ 040 clearImage(image); 041 entity_type_1 = x; 042 entity_type_2 = y; 043 axis[0] = new HashMap(); 044 axis[1] = new HashMap(); 045 shapeSize = (int)z; 046 047 int w = value.length, h = value[0].length; 048 049 for (int j=0; j<h; j++) { 050 Shape consumer = null; 051 for (int i=0; i<w; i++) { 052 if (value[i][j]>threshold) { 053 consumer = new Ball(y,j,shapeSize); 054 image.setColor(i,j,consumer); 055 axis[1].put(new Integer(axis[1].size()),consumer); 056 break; 057 } 058 } 059 } 060 061 062 for (int i=0; i<w; i++) { 063 Shape producer = null; 064 for (int j=0; j<h; j++) { 065 if (value[i][j] >= threshold) { 066 Shape consumer = findConsumer(j); 067 producer = new Box(x,i,shapeSize,shapeSize); 068 Iterator it = axis[0].keySet().iterator(); 069 Shape found = null; 070 while (it.hasNext()) { 071 Object key = it.next(); 072 Shape temp = (Shape)axis[0].get(key); 073 if (temp.getEntityID() == i) { 074 found = temp; 075 break; 076 } 077 } 078 if (found == null) { 079 found = producer; 080 image.setColor(i,j,found); 081 axis[0].put(new Integer(axis[0].size()),found); 082 } 083 if (consumer != null) 084 found.addConsumer(consumer,value[i][j]); 085 } 086 } 087 } 088 } 089 090 public Shape getEntityShape(int x, int y) { 091 int totalShapes = axis[0].size() + axis[1].size(); 092 093 int i = 0,producerSize = axis[0].size(); 094 while (i<totalShapes) { 095 Iterator it = axis[i/producerSize].keySet().iterator(); 096 while (it.hasNext()) { 097 Object key = it.next(); 098 Shape object = (Shape)axis[i/producerSize].get(key); 099 if (object.insideShape(x,y)) { 100 return object; 101 } 102 } 103 i += axis[i/producerSize].size(); 104 } 105 return null; 106 } 107 108 public HashMap getEntitiesInBox(int startX, int startY, int endX, int endY) { 109 HashMap result = new HashMap(); 110 for (int i=0; i<axis.length; i++) { 111 ArrayList ids = new ArrayList(); 112 int targetType = -1; 113 for (int j=0; j<axis[i].size(); j++) { 114 Shape object = (Shape)axis[i].get(new Integer(j)); 115 targetType = (int)object.getEntityType(); 116 if (inside(object,startX,startY,endX,endY)) 117 ids.add(new Long(object.getEntityID())); 118 } 119 if (targetType!= -1) result.put(new Integer(targetType),ids); 120 } 121 return result; 122 } 123 124 private Shape findConsumer(int entityId) { 125 Iterator it = axis[1].keySet().iterator(); 126 while (it.hasNext()) { 127 Shape object = (Shape)axis[1].get(it.next()); 128 if (object.getEntityID() == entityId) 129 return object; 130 } 131 return null; 132 } 133 134 private boolean inside(Shape object, int startX, int startY, int endX, int endY) { 135 if ((startX<=object.x)&&(object.x<=endX)&&(startY<=object.y)&&(object.y<=endY)) 136 return true; 137 return false; 138 } 139 140 private void clearImage(AutoImage image) { 141 short[][] data = image.getImageDataArray(); 142 for (int i=0; i<data.length; i++) 143 for (int j=0; j<data[i].length; j++) 144 data[i][j] = 0; 145 } 146 147 public Object clone() { 148 RelationshipPainter o = (RelationshipPainter)super.clone(); 149 o.axis = new HashMap[2]; 150 o.axis[0] = new HashMap(); 151 o.axis[1] = new HashMap(); 152 153 int w = value.length, h = value[0].length; 154 for (int i=0; i<w; i++) { 155 Shape producer = null; 156 for (int j=0; j<h; j++) { 157 if (value[i][j] >= threshold) { 158 Shape consumer = o.findConsumer(j); 159 if (producer == null) { 160 producer = new Box(entity_type_1,i,shapeSize,shapeSize); 161 o.axis[0].put(new Integer(o.axis[0].size()),producer); 162 } 163 if (consumer == null) { 164 consumer = new Ball(entity_type_2,j,shapeSize); 165 o.axis[1].put(new Integer(o.axis[1].size()),consumer); 166 } 167 producer.addConsumer(consumer,value[i][j]); 168 } 169 } 170 } 171 172 return o; 173 } 174 }