Class Mosaic

java.lang.Object
  extended byMosaic

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.) David Eck (eck@hws.edu), 3 February 2000. Modified October 2005 to use Javadoc comments and to place the window in the center of the screen when it opens.


Constructor Summary
Mosaic()
           
 
Method Summary
static void close()
          Closes the Mosaic window.
static void delay(int milliseconds)
          Inserts a pause into the excution of the program.
static void fill(java.awt.Color c)
          Set all rectangles in the grid to a specified color.
static void fill(int red, int green, int blue)
          Set all rectangles in the grid to a specified color.
static void fillRandomly()
          Sets each rectangle in the grid to its own randomly selected color.
static int getBlue(int row, int col)
          Get the blue component color of a specified rectangle in the grid.
static java.awt.Color getColor(int row, int col)
          Get the color of a specified rectangle in the grid.
static int getGreen(int row, int col)
          Get the green component color of a specified rectangle in the grid.
static int getRed(int row, int col)
          Get the red component color of a specified rectangle in the grid.
static boolean isOpen()
          Test whether the Mosaic window is currently open
static void open()
          Open a mosaic window with a 20-by-20 grid of squares, where each square is 15 pixels on a side.
static void open(int rows, int columns)
          Open a mosaic window with the specified numbers or rows and columns of squares, where each square is 15 pixels on a side.
static void open(int rows, int columns, int blockWidth, int blockHeight)
          Open a window that shows a mosaic with the specified number of rows and columns of rectangles.
static void setColor(int row, int col, java.awt.Color c)
          Set the color of a specified rectangle in the grid.
static void setColor(int row, int col, int red, int green, int blue)
          Set the color of a specified rectangle in the grid.
static void setTitle(java.lang.String title)
          Change the title displayed in the titlebar of the mosaic window.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Mosaic

public Mosaic()
Method Detail

open

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


open

public static void open(int rows,
                        int columns)
Open a mosaic window with the specified numbers or rows and columns of squares, where each square is 15 pixels on a side.

Parameters:
rows - the number of rows of squares in the grid
columns - the numbe of columns of squares in the grid

open

public static void open(int rows,
                        int columns,
                        int blockWidth,
                        int blockHeight)
Open a window that shows a mosaic with the specified number of rows and columns of rectangles. If a mosaic window is already open, it will be closed before the new window is open.

Parameters:
rows - the number of rows of squares in the grid
columns - the numbe of columns of squares in the grid
blockWidth - the width of each rectangle, in pixels
blockHeight - the height of each rectangle, in pixels

close

public static void close()
Closes the Mosaic window. If the Mosaic window is open, calling this method has no effect.


isOpen

public static boolean isOpen()
Test whether the Mosaic window is currently open

Returns:
returns true if the window is visible on the screen, and returns false if it is not

delay

public static void delay(int milliseconds)
Inserts a pause into the excution of the program. When this method is called, it will return only after a delay of a specified lenght of time. During that time, nothing will happen. It is possible that the actual delay time will be longer than the specified time.

Parameters:
milliseconds - the delay time, in milliseconds. One second is 1000 milliseconds. If the value of this parameter is less than or equal to zero, the method has no effect.

setTitle

public static void setTitle(java.lang.String title)
Change the title displayed in the titlebar of the mosaic window. Calling this method has no effect if the window is not open.

Parameters:
title - the new title for the window. If title is null, then the original title, "Mosaic Window", is restored.

getColor

public static java.awt.Color getColor(int row,
                                      int col)
Get the color of a specified rectangle in the grid.

Parameters:
row - the row number of the rectangle whose color is being tested
col - the column number of the rectangle whose color is being tested
Returns:
the color of the rectangle in the specified row and column. If the row or column number is outside the range of legal values, then the return value will be black. Also, if the Mosaic window is not open, then the return value is black.

getRed

public static int getRed(int row,
                         int col)
Get the red component color of a specified rectangle in the grid. The red component of a color is an ineger in the range 0 to 255.

Parameters:
row - the row number of the rectangle whose color is being tested
col - the column number of the rectangle whose color is being tested
Returns:
the red component of the color of the rectangle in the specified row and column. If the row or column number is outside the range of legal values, then the return value will be 0. Also, if the Mosaic window is not open, then the return value is 0.

getGreen

public static int getGreen(int row,
                           int col)
Get the green component color of a specified rectangle in the grid. The green component of a color is an ineger in the range 0 to 255.

Parameters:
row - the row number of the rectangle whose color is being tested
col - the column number of the rectangle whose color is being tested
Returns:
the green component of the color of the rectangle in the specified row and column. If the row or column number is outside the range of legal values, then the return value will be 0. Also, if the Mosaic window is not open, then the return value is 0.

getBlue

public static int getBlue(int row,
                          int col)
Get the blue component color of a specified rectangle in the grid. The blue component of a color is an ineger in the range 0 to 255.

Parameters:
row - the row number of the rectangle whose color is being tested
col - the column number of the rectangle whose color is being tested
Returns:
the blue component of the color of the rectangle in the specified row and column. If the row or column number is outside the range of legal values, then the return value will be 0. Also, if the Mosaic window is not open, then the return value is 0.

setColor

public static void setColor(int row,
                            int col,
                            java.awt.Color c)
Set the color of a specified rectangle in the grid. If the Mosaic window is not open, this has no effect. Also, if the specified row or column is outside the legal range for the grid, then nothing is done.

Parameters:
row - the row number of the rectangle whose color is being set
col - the column number of the rectangle whose color is being set
c - the new color for the rectangle in the specified row and column. If c is null, then the color will be black.

setColor

public static void setColor(int row,
                            int col,
                            int red,
                            int green,
                            int blue)
Set the color of a specified rectangle in the grid. If the Mosaic window is not open, this has no effect. Also, if the specified row or column is outside the legal range for the grid, then nothing is done.

Parameters:
row - the row number of the rectangle whose color is being set
col - the column number of the rectangle whose color is being set
red - the red component of the color, in the range 0 to 255. The value is clamped to this range; that is, a value less than 0 is replaced by 0 and a value less than 255 is replaced by 255.
green - the green component of the color, in the range 0 to 255. The value is clamped to this range.
blue - the blue component of the color, in the range 0 to 255. The value is clamped to this range.

fill

public static void fill(java.awt.Color c)
Set all rectangles in the grid to a specified color. If the Mosaic window is not open, calling this method has no effect.

Parameters:
c - the color for all the rectangles. If c is null, black is used.

fill

public static void fill(int red,
                        int green,
                        int blue)
Set all rectangles in the grid to a specified color. If the Mosaic window is not open, calling this method has no effect.

Parameters:
red - the red component of the color, in the range 0 to 255. The value is clamped to this range; that is, a value less than 0 is replaced by 0 and a value less than 255 is replaced by 255.
green - the green component of the color, in the range 0 to 255. The value is clamped to this range.
blue - the blue component of the color, in the range 0 to 255. The value is clamped to this range.

fillRandomly

public static void fillRandomly()
Sets each rectangle in the grid to its own randomly selected color. If the Mosaic window is not open, then calling this method has no effect.