Class Mosaic

java.lang.Object
  extended by Mosaic

public class Mosaic
extends java.lang.Object

The class Mosaic makes available a window made up of a grid of colored rectangles. Routines are provided for opening and closing the window and for setting and testing the color of rectangles in the grid.

Each rectangle in the grid has a color. The color can be specified by red, green, and blue amounts in the range from 0 to 255. It can also be given as an object belonging to the class Color.

(Technical note: Some of the methods in this class are declared to be "synchronized". Synchronization is sometimes required when there are several threads running at the same time. In this case, there can be both the thread that runs the main routine of a program and the thread that manages the GUI. Synchronization can be necessary if the window is closed by the GUI while it is being used by the main thread.)

Updated October 17, 2011, to add the getRows, getCols, and saveImage methods.


Constructor Summary
Mosaic()
           
 
Method Summary
static void close()
          Close the mosaic window, if one is open.
static void delay(int milliseconds)
          Inserts a delay in the program (to regulate the speed at which the colors are changed, for example).

Precondition: milliseconds is a positive integer.

Postcondition: The program has paused for at least the specified number of milliseconds, where one second is equal to 1000 milliseconds.
static void fill(java.awt.Color c)
          Fills the entire mosaic with a specified color.
static void fill(int red, int green, int blue)
          Fills the entire mosaic with a color that is specified by giving its red, green, and blue components (numbers in the range 0 to 255).
static void fillRandomly()
          Fill the entire mosaic window with random colors by setting the color of each rectangle to a randomly selected red/blue/green values.
static int getBlue(int row, int col)
          Like getRed, but returns the blue component of the color.
static java.awt.Color getColor(int row, int col)
          Gets the color of one of the rectangles in the mosaic.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The color of the specified rectangle is returned as object of type color.
static int getCols()
          Returns the number of columns in the mosaic, if the mosaic is open.
static int getGreen(int row, int col)
          Like getRed, but returns the green component of the color.
static int getRed(int row, int col)
          Gets the red component of the color of one of the rectangles.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The red component of the color of the specified rectangle is returned as an integer in the range 0 to 255 inclusive.
static int getRows()
          Returns the number of rows in the mosaic, if the mosaic is open.
static boolean isOpen()
          Tests whether the mosaic window is currently open.

Precondition: None.

Postcondition: The return value is true if the window is open when this function is called, and it is false if the window is closed.
static void open()
          Open a mosaic window with a 20-by-20 grid of squares, where each square is 15 pixel on a side.
static void open(int rows, int columns)
          Opens a mosaic window containing a specified number of rows and a specified number of columns of square.
static void open(int rows, int columns, int blockWidth, int blockHeight)
          Opens a "mosaic" window on the screen.
static void saveImage(java.lang.String filename)
          Saves a copy of the image in the mosaic window to the file with the specified name.
static void saveImageToUserSelectedFile()
          Lets the user select an output file using a GUI file selection box.
static void setColor(int row, int col, java.awt.Color c)
          Sets the color of one of the rectangles in the window.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The color of the rectangle in row number row and column number col has been set to the color specified by c.
static void setColor(int row, int col, int red, int green, int blue)
          Sets the color of one of the rectangles in the window.

Precondition: row and col are in the valid range of row and column numbers, and r, g, and b are in the range 0 to 255, inclusive.

Postcondition: The color of the rectangle in row number row and column number col has been set to the color specified by r, g, and b.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Mosaic

public Mosaic()
Method Detail

close

public static void close()
Close the mosaic window, if one is open.


delay

public static void delay(int milliseconds)
Inserts a delay in the program (to regulate the speed at which the colors are changed, for example).

Precondition: milliseconds is a positive integer.

Postcondition: The program has paused for at least the specified number of milliseconds, where one second is equal to 1000 milliseconds.


fill

public static void fill(java.awt.Color c)
Fills the entire mosaic with a specified color. If c is null, the mosaic is filled with black.


fill

public static void fill(int red,
                        int green,
                        int blue)
Fills the entire mosaic with a color that is specified by giving its red, green, and blue components (numbers in the range 0 to 255).


fillRandomly

public static void fillRandomly()
Fill the entire mosaic window with random colors by setting the color of each rectangle to a randomly selected red/blue/green values.


getBlue

public static int getBlue(int row,
                          int col)
Like getRed, but returns the blue component of the color.


getColor

public static java.awt.Color getColor(int row,
                                      int col)
Gets the color of one of the rectangles in the mosaic.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The color of the specified rectangle is returned as object of type color.


getCols

public static int getCols()
Returns the number of columns in the mosaic, if the mosaic is open. If the mosaic is not open, throws an IllegalStateException.


getGreen

public static int getGreen(int row,
                           int col)
Like getRed, but returns the green component of the color.


getRed

public static int getRed(int row,
                         int col)
Gets the red component of the color of one of the rectangles.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The red component of the color of the specified rectangle is returned as an integer in the range 0 to 255 inclusive.


getRows

public static int getRows()
Returns the number of rows in the mosaic, if the mosaic is open. If the mosaic is not open, throws an IllegalStateException.


isOpen

public static boolean isOpen()
Tests whether the mosaic window is currently open.

Precondition: None.

Postcondition: The return value is true if the window is open when this function is called, and it is false if the window is closed.


open

public static void open()
Open a mosaic window with a 20-by-20 grid of squares, where each square is 15 pixel on a side.


open

public static void open(int rows,
                        int columns)
Opens a mosaic window containing a specified number of rows and a specified number of columns of square. Each square is 15 pixels on a side.


open

public static void open(int rows,
                        int columns,
                        int blockWidth,
                        int blockHeight)
Opens a "mosaic" window on the screen. If another mosaic window was already open, that one is closed and a new one is created.

Precondition: The parameters rows, cols, w, and h are positive integers.

Postcondition: A window is open on the screen that can display rows and columns of colored rectangles. Each rectangle is w pixels wide and h pixels high. The number of rows is given by the first parameter and the number of columns by the second. Initially, all rectangles are black.

Note: The rows are numbered from 0 to rows - 1, and the columns are numbered from 0 to cols - 1.


saveImage

public static void saveImage(java.lang.String filename)
Saves a copy of the image in the mosaic window to the file with the specified name. The image is saved in PNG format.

Parameters:
filename - the name of the file, given as an absolute path or a path relative to the current directory. Note that if the file name does not already end with ".png" (case insensitive), then .png is appended to the name.
Throws:
java.lang.IllegalStateException - if the mosaic is not open
java.lang.IllegalArgumentException - if the mosaic is open but the specified file already exists, or if it is not possible to write to create the file.

saveImageToUserSelectedFile

public static void saveImageToUserSelectedFile()
Lets the user select an output file using a GUI file selection box. The mosaic image is saved to that file. The image is saved in PNG format. If the user's file does not end with .png (case insensitive), then .png is added to the file name. If the user cancels the dialog, no file is saved.

Throws:
java.lang.IllegalStateException - if the mosaic is not open
IllegalArguementException - if it an error occurs while trying to write to the file.

setColor

public static void setColor(int row,
                            int col,
                            java.awt.Color c)
Sets the color of one of the rectangles in the window.

Precondition: row and col are in the valid range of row and column numbers.

Postcondition: The color of the rectangle in row number row and column number col has been set to the color specified by c. If c is null, the color of the rectangle is set to black.


setColor

public static void setColor(int row,
                            int col,
                            int red,
                            int green,
                            int blue)
Sets the color of one of the rectangles in the window.

Precondition: row and col are in the valid range of row and column numbers, and r, g, and b are in the range 0 to 255, inclusive.

Postcondition: The color of the rectangle in row number row and column number col has been set to the color specified by r, g, and b. r gives the amount of red in the color with 0 representing no red and 255 representing the maximum possible amount of red. The larger the value of r, the more red in the color. g and b work similarly for the green and blue color components.