/** A Bitmap class, which represents a rectangle of colored pixels */ import uwcse.graphics.*; import java.awt.Color; public class Bitmap { private Rectangle[][] picture; private int x; // the X coordinate of the upper-left corner of the bitmap private int y; // the Y coordinate of the upper-left corner of the bitmap private int pixelSize; /** Create a new bitmap @param x the X coordinate of the upper-left corner of the bitmap @param y the Y coordinate of the upper-left corner of the bitmap @param width the width of the bitmap @param height the height of the bitmap @param size the size of each pixel @param color the initial color of the bitmap */ public Bitmap(int x, int y, int width, int height, int size, Color color) { // num rows = height // num cols = width this.picture = new Rectangle[height][width]; this.x = x; this.y = y; this.pixelSize = size; // create rectangles for each of the pixels for (int row = 0; row < this.picture.length; row = row + 1) { for (int col = 0; col < this.picture[row].length; col = col + 1) { // x = col, y = row this.picture[row][col] = new Rectangle(x + col*size, y + row*size, size, size, color, true); } } } /** Add the Bitmap to the GWindow */ public void addTo(GWindow w) { for (int row = 0; row < this.picture.length; row = row + 1) { for (int col = 0; col < this.picture[row].length; col = col + 1) { this.picture[row][col].addTo(w); } } } /** Remove the Bitmap from its window */ public void removeFromWindow() { for (int row = 0; row < this.picture.length; row = row + 1) { for (int col = 0; col < this.picture[row].length; col = col + 1) { this.picture[row][col].removeFromWindow(); } } } /** Set the color of one of the pixels of the bitmap. @param x the x coordinate (column) of the pixel in the bitmap @param y the y coordinate (row) of the pixel in the bitmap @param color the color to fill the bitmap with */ public void colorPixel(int x, int y, Color color) { // x = col, y = row Rectangle pixel = picture[y][x]; pixel.setColor(color); } /** Take the x & y coordinates (e.g. from a mouse click), and set the color of the corresponding pixel @param x the X coordinate of the mouse @param y the Y coordinate of the mouse @param color the color to paint the corresponding pixel */ public void colorPixelFromCoordinates(int x, int y, Color color) { int pixelX = (x - this.x) / this.pixelSize; int pixelY = (y - this.y) / this.pixelSize; this.colorPixel(pixelX, pixelY, color); } /** Fill all pixels with the given color. @param color the color to fill the bitmap with */ public void fill(Color color) { for (int row = 0; row < this.picture.length; row = row + 1) { for (int col = 0; col < this.picture[row].length; col = col + 1) { Rectangle pixel = picture[row][col]; pixel.setColor(color); // (we could have used "this.colorPixel(col,row,color);" instead) } } } /** Shift the colors of all pixels left, setting the color of the right border to be the argument color. @param color the color to fill the right border with */ public void shiftLeft(Color color) { for (int row = 0; row < this.picture.length; row = row + 1) { // skip last column int lastCol = this.picture[row].length-1; for (int col = 0; col < lastCol; col = col + 1) { Rectangle thisPixel = picture[row][col]; Rectangle pixelToRight = picture[row][col+1]; thisPixel.setColor(pixelToRight.getColor()); } // set the color of the last column picture[row][lastCol].setColor(color); } } /** Shift the colors of all pixels right, setting the color of the left border to be the argument color. @param color the color to fill the left border with */ public void shiftRight(Color color) { for (int row = 0; row < this.picture.length; row = row + 1) { // go last column to 2nd column, skipping first column for (int col = this.picture[row].length-1; col > 0; col = col - 1) { Rectangle thisPixel = picture[row][col]; Rectangle pixelToLeft = picture[row][col-1]; thisPixel.setColor(pixelToLeft.getColor()); } // set the color of the first column picture[row][0].setColor(color); } } /** Shift the colors of all pixels up, setting the color of the bottom row to be the argument color. @param color the color to fill the bottom border with */ public void shiftUp(Color color) { // assume all rows have the same length! for (int col = 0; col < this.picture[0].length; col = col + 1) { // skip last row int lastRow = this.picture.length-1; for (int row = 0; row < lastRow; row = row + 1) { Rectangle thisPixel = picture[row][col]; Rectangle pixelBelow = picture[row+1][col]; thisPixel.setColor(pixelBelow.getColor()); } // set the color of the last row picture[lastRow][col].setColor(color); } } /** Shift the colors of all pixels down, setting the color of the top row to be the argument color. @param color the color to fill the top border with */ public void shiftDown(Color color) { // assume all rows have the same length! for (int col = 0; col < this.picture[0].length; col = col + 1) { // go last row to second row, skipping first row for (int row = this.picture.length-1; row > 0; row = row - 1) { Rectangle thisPixel = picture[row][col]; Rectangle pixelAbove = picture[row-1][col]; thisPixel.setColor(pixelAbove.getColor()); } // set the color of the first row picture[0][col].setColor(color); } } }