+ private int ch = ' ';
+
+ /**
+ * The display width of this cell.
+ */
+ private Width width = Width.SINGLE;
+
+ /**
+ * The image at this cell.
+ */
+ private BufferedImage image = null;
+
+ /**
+ * The image at this cell, inverted.
+ */
+ private BufferedImage invertedImage = null;
+
+ /**
+ * The background color used for the area the image portion might not
+ * cover.
+ */
+ private Color background = Color.BLACK;
+
+ /**
+ * hashCode() needs to call image.hashCode(), which can get quite
+ * expensive.
+ */
+ private int imageHashCode = 0;
+
+ /**
+ * hashCode() needs to call background.hashCode(), which can get quite
+ * expensive.
+ */
+ private int backgroundHashCode = 0;
+
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Public constructor sets default values of the cell to blank.
+ *
+ * @see #isBlank()
+ * @see #reset()
+ */
+ public Cell() {
+ // NOP
+ }
+
+ /**
+ * Public constructor sets the character. Attributes are the same as
+ * default.
+ *
+ * @param ch character to set to
+ * @see #reset()
+ */
+ public Cell(final int ch) {
+ this.ch = ch;
+ }
+
+ /**
+ * Public constructor sets the attributes.
+ *
+ * @param attr attributes to use
+ */
+ public Cell(final CellAttributes attr) {
+ super(attr);
+ }
+
+ /**
+ * Public constructor sets the character and attributes.
+ *
+ * @param ch character to set to
+ * @param attr attributes to use
+ */
+ public Cell(final int ch, final CellAttributes attr) {
+ super(attr);
+ this.ch = ch;
+ }
+
+ /**
+ * Public constructor creates a duplicate.
+ *
+ * @param cell the instance to copy
+ */
+ public Cell(final Cell cell) {
+ setTo(cell);
+ }
+
+ // ------------------------------------------------------------------------
+ // Cell -------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Set the image data for this cell.
+ *
+ * @param image the image for this cell
+ */
+ public void setImage(final BufferedImage image) {
+ this.image = image;
+ imageHashCode = image.hashCode();
+ width = Width.SINGLE;
+ }
+
+ /**
+ * Get the image data for this cell.
+ *
+ * @return the image for this cell
+ */
+ public BufferedImage getImage() {
+ if (invertedImage != null) {
+ return invertedImage;
+ }
+ return image;
+ }
+
+ /**
+ * Get the bitmap image background color for this cell.
+ *
+ * @return the bitmap image background color
+ */
+ public Color getBackground() {
+ return background;
+ }
+
+ /**
+ * If true, this cell has image data.
+ *
+ * @return true if this cell is an image rather than a character with
+ * attributes
+ */
+ public boolean isImage() {
+ if (image != null) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Restore the image in this cell to its normal version, if it has one.
+ */
+ public void restoreImage() {
+ invertedImage = null;
+ }
+
+ /**
+ * If true, this cell has image data, and that data is inverted.
+ *
+ * @return true if this cell is an image rather than a character with
+ * attributes, and the data is inverted
+ */
+ public boolean isInvertedImage() {
+ if ((image != null) && (invertedImage != null)) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Invert the image in this cell, if it has one.
+ */
+ public void invertImage() {
+ if (image == null) {
+ return;
+ }
+ if (invertedImage == null) {
+ invertedImage = new BufferedImage(image.getWidth(),
+ image.getHeight(), BufferedImage.TYPE_INT_ARGB);
+
+ int [] rgbArray = image.getRGB(0, 0,
+ image.getWidth(), image.getHeight(), null, 0, image.getWidth());
+
+ for (int i = 0; i < rgbArray.length; i++) {
+ // Set the colors to fully inverted.
+ if (rgbArray[i] != 0x00FFFFFF) {
+ rgbArray[i] ^= 0x00FFFFFF;
+ }
+ // Also set alpha to non-transparent.
+ rgbArray[i] |= 0xFF000000;
+ }
+ invertedImage.setRGB(0, 0, image.getWidth(), image.getHeight(),
+ rgbArray, 0, image.getWidth());
+ }
+ }