001 /* EVolve - an Extensible Software Visualization Framework 002 * Copyright (C) 2001-2002 Qin Wang 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Library General Public 006 * License as published by the Free Software Foundation; either 007 * version 2 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Library General Public License for more details. 013 * 014 * You should have received a copy of the GNU Library General Public 015 * License along with this library; if not, write to the 016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 017 * Boston, MA 02111-1307, USA. 018 */ 019 020 /* 021 * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/ 022 */ 023 024 package EVolve.visualization; 025 026 import java.awt.*; 027 import java.awt.image.*; 028 029 /** 030 * Automatic expanding image. 031 */ 032 public class AutoImage { 033 private Color[][] image; // the image 034 private int w, h; // width and height of the content 035 private int wMax, hMax; // width and height of the image 036 037 /** 038 * Creates an auto-image. 039 */ 040 public AutoImage() { 041 w = 0; 042 h = 0; 043 wMax = 200; 044 hMax = 200; 045 image = new Color[wMax][hMax]; 046 for (int i = 0; i < wMax; i++) { 047 for (int j = 0; j < hMax; j++) { 048 image[i][j] = null; 049 } 050 } 051 } 052 053 /** 054 * Sets the color of a pixel. 055 * 056 * @param x position on X-axis 057 * @param y position on Y-axis 058 * @param color the color 059 */ 060 public void setColor(int x, int y, Color color) { 061 if ((x >= wMax) || (y >= hMax)) { 062 while (x >= wMax) { 063 wMax += 200; 064 } 065 while (y >= hMax) { 066 hMax += 200; 067 } 068 069 Color[][] newImage = new Color[wMax][hMax]; 070 for (int i = 0; i < wMax; i++) { 071 for (int j = 0; j < hMax; j++) { 072 newImage[i][j] = null; 073 } 074 } 075 for (int i = 0; i < w; i++) { 076 for (int j = 0; j < h; j++) { 077 newImage[i][j] = image[i][j]; 078 } 079 } 080 image = newImage; 081 } 082 image[x][y] = color; 083 084 if (x >= w) { 085 w = x + 1; 086 } 087 if (y >= h) { 088 h = y + 1; 089 } 090 } 091 092 /** 093 * Gets the color of a pixel. 094 * 095 * @param x position on X-axis 096 * @param y position on Y-axis 097 * @return the color 098 */ 099 public Color getColor(int x, int y) { 100 if ((x >= wMax) || (y >= hMax)) { 101 return null; 102 } else { 103 return image[x][y]; 104 } 105 } 106 107 public Color getSortedColor(ReferenceDimension xAxis, ReferenceDimension yAxis,int x,int y) { 108 int sortedX,sortedY; 109 sortedX = (xAxis == null) ? x : xAxis.getSourceIndex(x); 110 sortedY = (yAxis == null) ? y : yAxis.getSourceIndex(y); 111 112 if ((sortedX >= wMax) || (sortedY >= hMax) || (sortedX < 0) || (sortedY < 0)) 113 return null; 114 else 115 return image[sortedX][sortedY]; 116 } 117 118 /** 119 * Gets the buffered-image. 120 * 121 * @return buffered-image 122 */ 123 public BufferedImage getImage() { 124 BufferedImage returnVal = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 125 returnVal.getGraphics().setColor(Color.white); 126 returnVal.getGraphics().fillRect(0, 0, w, h); 127 128 for (int i = 0; i < w; i++) { 129 for (int j = 0; j < h; j++) { 130 if (image[i][j] != null) { 131 returnVal.setRGB(i, j, image[i][j].getRGB()); 132 } 133 } 134 } 135 136 return returnVal; 137 } 138 139 /** 140 * Sorts the image. 141 * 142 * @param xAxis dimension that represents the X-axis, null if doesn't need to be sorted 143 * @param yAxis dimension that represents the Y-axis, null if doesn't need to be sorted 144 * @return sorted image 145 */ 146 public AutoImage getSortedImage(ReferenceDimension xAxis, ReferenceDimension yAxis) { 147 AutoImage returnVal = new AutoImage(); 148 for (int i = 0; i < w; i++) { 149 for (int j = 0; j < h; j++) { 150 int x, y; 151 if (xAxis == null) { 152 x = i; 153 } else { 154 x = xAxis.getSortedIndex(i); 155 } 156 if (yAxis == null) { 157 y = j; 158 } else { 159 y = yAxis.getSortedIndex(j); 160 } 161 if ((x != -1) && (y != -1)) { 162 returnVal.setColor(x, y, image[i][j]); 163 } 164 } 165 } 166 return returnVal; 167 } 168 169 public int getDimension(int dim) { 170 if (dim == -1) return image.length; 171 172 return image[dim].length; 173 } 174 175 public int getW() { 176 return w; 177 } 178 179 public int getH() { 180 return h; 181 } 182 183 }