001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Feb 27, 2003 005 * Time: 1:52:09 PM 006 */ 007 008 package EVolve.visualization; 009 010 import java.awt.Color; 011 import java.awt.Graphics2D; 012 import java.awt.image.BufferedImage; 013 import java.util.ArrayList; 014 import java.util.HashMap; 015 import java.util.Iterator; 016 017 import EVolve.exceptions.NoDataPlotException; 018 import EVolve.util.painters.shapes.Shape; 019 020 public class AutoShapeImage extends AutoImage{ 021 private Shape defaultShape; 022 023 public AutoShapeImage() { 024 w = h = 0; 025 wMax = hMax = 200; 026 image = new short[wMax][hMax]; 027 for (int i = 0; i < wMax; i++) { 028 for (int j = 0; j < hMax; j++) { 029 image[i][j] = 0; 030 } 031 } 032 defaultShape = null; 033 } 034 035 public AutoShapeImage(Shape defaultShape) { 036 this(); 037 this.defaultShape = defaultShape; 038 } 039 040 private void drawShapes(Graphics2D g2, int x, int y) { 041 if (defaultShape == null) { 042 ArrayList value = (ArrayList)Int2Object.get(new Integer(image[x][y])); 043 for (int k=0; k<value.size(); k++) 044 ((Shape)value.get(k)).draw(g2); 045 } else { //when there is active default shape, image array contains color 046 int width = defaultShape.getWidth(), height = defaultShape.getHeight(); 047 defaultShape.setColor((Color)Int2Object.get(new Integer(image[x][y]))); 048 defaultShape.move(x*width,y*height); 049 defaultShape.draw(g2); 050 } 051 } 052 053 public void setColor(int x, int y, Object shape) { 054 055 if ((x >= wMax) || (y >= hMax)) { 056 while (x >= wMax) { 057 wMax += 200; 058 } 059 while (y >= hMax) { 060 hMax += 200; 061 } 062 063 short[][] newImage = new short[wMax][hMax]; 064 for (int i = 0; i < wMax; i++) { 065 for (int j = 0; j < hMax; j++) { 066 newImage[i][j] = 0; 067 } 068 } 069 for (int i = 0; i < w; i++) { 070 for (int j = 0; j < h; j++) { 071 newImage[i][j] = image[i][j]; 072 } 073 } 074 image = newImage; 075 } 076 077 if (defaultShape == null) { 078 Shape aShape = (Shape)shape; 079 if (image[x][y] == 0) { 080 short size = (short)object2Int.size(); 081 ArrayList value = new ArrayList(); 082 object2Int.put(value, new Integer(size)); 083 Int2Object.put(new Integer(size),value); 084 image[x][y] = size; 085 } 086 ((ArrayList)Int2Object.get(new Integer(image[x][y]))).add(aShape); 087 } else { 088 if (object2Int.containsKey(shape)) { 089 image[x][y] = ((Integer)object2Int.get(shape)).shortValue(); //in this case, shape contains a color object 090 } else { 091 short size = (short)object2Int.size(); 092 object2Int.put(shape,new Integer(size)); 093 Int2Object.put(new Integer(size),shape); 094 image[x][y] = size; 095 } 096 } 097 098 if (x >= w) { 099 w = x + 1; 100 } 101 if (y >= h) { 102 h = y + 1; 103 } 104 } 105 106 public BufferedImage getImage() throws NoDataPlotException{ 107 BufferedImage returnVal = null; 108 109 if (defaultShape != null) { 110 imageWidth = defaultShape.getWidth() * w; 111 imageHeight = defaultShape.getHeight() * h; 112 } 113 114 if (imageWidth*imageHeight == 0) throw new NoDataPlotException(); 115 116 returnVal = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB); 117 returnVal.getGraphics().setColor(Color.white); 118 returnVal.getGraphics().fillRect(0, 0, imageWidth, imageHeight); 119 Graphics2D g2 = (Graphics2D)returnVal.getGraphics(); 120 121 for (int i = 0; i < w; i++) { 122 for (int j = 0; j < h; j++) { 123 if (image[i][j] != 0) 124 drawShapes(g2,i,j); 125 } 126 } 127 128 return returnVal; 129 } 130 131 public Object getColor(int x, int y) { 132 if (((image!=null) && ((x < 0) || (y < 0) || (x >= image.length) || (y >= image[0].length))) ||(image[x][y]==0)) { 133 return null; 134 } else { 135 if (defaultShape == null) 136 return Int2Object.get(new Integer(image[x][y])); 137 else 138 return (Color)Int2Object.get(new Integer(image[x][y])); 139 } 140 } 141 142 public Object getSortedColor(ReferenceDimension xAxis, ReferenceDimension yAxis,int x,int y) { 143 int xMappedId, yMappedId; 144 xMappedId = (xAxis == null) ? x : xAxis.getOriginMappedId(x); 145 yMappedId = (yAxis == null) ? y : yAxis.getOriginMappedId(y); 146 147 if ((xMappedId >= wMax) || (yMappedId >= hMax) || (xMappedId < 0) || (yMappedId < 0)) 148 return null; 149 else 150 if (defaultShape == null) 151 return Int2Object.get(new Integer(image[xMappedId][yMappedId])); 152 else 153 return (Color)Int2Object.get(new Integer(image[x][y])); 154 } 155 156 public Shape getEntityShapes(int x,int y) { 157 for (int i=0; i<w; i++) { 158 for (int j=0; j<h; j++) { 159 ArrayList data = (ArrayList)Int2Object.get(new Integer(image[i][j])); 160 if (data != null) { 161 for (int k=0; k<data.size(); k++) { 162 Shape aShape = (Shape)data.get(k); 163 if (aShape.insideShape(x,y)) return aShape; 164 } 165 } 166 } 167 } 168 return null; 169 } 170 171 public AutoImage getSortedImage(ReferenceDimension xAxis, ReferenceDimension yAxis) { 172 AutoShapeImage returnVal = new AutoShapeImage(defaultShape); 173 for (int i = 0; i < w; i++) { 174 for (int j = 0; j < h; j++) { 175 int x, y; 176 if (xAxis == null) { 177 x = i; 178 } else { 179 x = xAxis.getSortedIndex(i); 180 } 181 if (yAxis == null) { 182 y = j; 183 } else { 184 y = yAxis.getSortedIndex(j); 185 } 186 if ((x != -1) && (y != -1) && (image[i][j]!=0)) { 187 if (defaultShape == null) { 188 ArrayList value = (ArrayList)Int2Object.get(new Integer(image[i][j])); 189 for (int k=0; k< value.size(); k++) { 190 Shape aShape = (Shape)(value.get(k)); 191 192 aShape.move(x*(xAxis == null ? 1 : aShape.getWidth()), 193 y*(yAxis == null ? 1 : aShape.getHeight())); 194 195 returnVal.setColor(x, y, aShape); 196 } 197 } else { 198 returnVal.setColor(x,y,Int2Object.get(new Integer(image[i][j]))); 199 } 200 } 201 } 202 } 203 returnVal.setImageSize(imageWidth,imageHeight); 204 return returnVal; 205 } 206 207 public void setImageSize(int width, int height) { 208 imageWidth = width; 209 imageHeight = height; 210 } 211 212 public void reArrange(int width, int boxSize) { 213 int newWidth = width/boxSize; 214 int newHeight = ((w*h)%newWidth == 0) ? w*h/newWidth : w*h/newWidth + 1; 215 short newImage[][] = new short[newWidth][newHeight]; 216 for (int i=0; i<w*h; i++) { 217 newImage[i%newWidth][i/newWidth] = image[i%w][i/w]; 218 } 219 220 w = newWidth; 221 h = newHeight; 222 image = null; 223 image = newImage; 224 defaultShape.setSize(boxSize, boxSize); 225 } 226 227 public Shape getDefaultShape() { 228 return defaultShape; 229 } 230 231 public Object clone() { 232 AutoShapeImage o = new AutoShapeImage(); 233 234 o.image = new short[wMax][hMax]; 235 o.imageWidth = imageWidth; 236 o.imageHeight = imageHeight; 237 o.Int2Object = new HashMap(); 238 o.object2Int = new HashMap(); 239 o.w = w; 240 o.h = h; 241 o.wMax = wMax; 242 o.hMax = hMax; 243 244 if (defaultShape == null) { 245 o.object2Int.put(null,new Integer(0)); 246 o.Int2Object.put(new Integer(0),null); 247 for (int i=0; i<image.length; i++) { 248 for (int j=0; j<image[i].length; j++) { 249 o.image[i][j] = image[i][j]; 250 if (image[i][j] != 0) { 251 ArrayList value = (ArrayList)Int2Object.get(new Integer(image[i][j])); 252 ArrayList newValue = new ArrayList(); 253 o.Int2Object.put(new Integer(image[i][j]),newValue); 254 o.object2Int.put(newValue, new Integer(image[i][j])); 255 for (int k=0; k<value.size(); k++) { 256 newValue.add(((Shape)value.get(k)).clone()); 257 } 258 } else 259 o.image[i][j] = 0; 260 } 261 } 262 } else { 263 o.defaultShape = (Shape)defaultShape.clone(); 264 for (int i=0; i<image.length; i++) { 265 for (int j=0; j<image[i].length; j++) 266 o.image[i][j] = image[i][j]; 267 } 268 269 Iterator it = object2Int.keySet().iterator(); 270 while (it.hasNext()) { 271 Object key = it.next(); 272 o.object2Int.put(key, object2Int.get(key)); 273 } 274 it = Int2Object.keySet().iterator(); 275 while (it.hasNext()) { 276 Object key = it.next(); 277 o.Int2Object.put(key, Int2Object.get(key)); 278 } 279 } 280 281 return o; 282 } 283 }