home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / mframe.jav < prev    next >
Text File  |  1996-12-20  |  9KB  |  238 lines

  1.  
  2. /*
  3.  
  4.    The class MosaicFrame represents a window subdivided into a grid of
  5.    rectangles, where the color of each rectangle can be set individually.
  6.    The user can close the window by clicking on it's close box.  The
  7.    window can also be closed by calling the method close().  Once a
  8.    window is closed, commands for setting rectangle colors will be ignored.
  9.  
  10.    David Eck, eck@hws.edu, September 1996
  11.  
  12. */
  13.  
  14. import java.awt.*;
  15.  
  16. public class MosaicFrame extends Frame {
  17.  
  18.    public MosaicFrame() {
  19.           // default constructor just provides values to main constructor
  20.       this("Mosaic",20,20,200,200);
  21.    }
  22.    
  23.    public MosaicFrame(String title, int ROWS, int COLUMNS) {
  24.           // constructor call main constructor, providing default
  25.           // values for WIDTH and HEIGHT that will give colored
  26.           // squares that are 10-by-10 pixels big
  27.       this(title,ROWS,COLUMNS,ROWS*10,COLUMNS*10);
  28.    }
  29.    
  30.    public MosaicFrame(String title, int ROWS, int COLUMNS, int HEIGHT, int WIDTH) {
  31.           // The main constructor for the class opens a window with the given
  32.           // title.  The window displays a grid of colored rectangles.  The number
  33.           // of rows and columns in the grid are specified by the parameters
  34.           // ROWS and COLUMNS.  WIDTH and HEIGHT give the desired size of the
  35.           // window.  WIDTH should be a multiple of COLUMNS, and HEIGHT should
  36.           // be a multiple of ROWS (if not, they will be adjusted).
  37.       super(title);
  38.       if (WIDTH > 400)   // sanity check for values of WIDTH and HEIGHT
  39.          WIDTH = 400;
  40.       else if (WIDTH < COLUMNS)
  41.          WIDTH = COLUMNS;
  42.       if (HEIGHT > 400)
  43.          HEIGHT = 400;
  44.       else if (HEIGHT < ROWS)
  45.          HEIGHT = ROWS;
  46.       rows = ROWS;
  47.       columns = COLUMNS;
  48.       rectWidth = WIDTH / columns;
  49.       width = rectWidth * columns;  // actual width is a multiple of rectWidth
  50.       rectHeight = HEIGHT / rows;
  51.       height = rectHeight * rows;   // actual height is a multiple of rectHeight
  52.       resize(width,height);
  53.       setResizable(false);  // window cannot be resized
  54.       setBackground(Color.black);
  55.       color = new Color[rows][columns];
  56.       for (int i=0; i<rows; i++)
  57.          for (int j=0; j<columns; j++)
  58.             color[i][j] = null;
  59.       show();
  60.       g = getGraphics();
  61.    }
  62.    
  63.    public boolean hasClosed() {
  64.          // test whether window has been closed, either by user or by the close() method
  65.       return closed;
  66.    }
  67.    
  68.    public void close() {
  69.          // close the window
  70.       if (!closed) {
  71.          dispose();
  72.          closed = true;
  73.       }
  74.    }
  75.    
  76.    public static void delay(int milliseconds) {
  77.          // wait, doing nothing, for the given number of milliseconds
  78.       if (milliseconds > 0) {
  79.          try { Thread.sleep(milliseconds); }
  80.          catch (InterruptedException e) { }
  81.       }
  82.       else
  83.          Thread.yield();
  84.    }
  85.       
  86.    public void clear() {
  87.          // set all the squares equal to the background color (which is black, unless
  88.          // it has been changed by a call to setBackground()
  89.       if (closed)
  90.          return;
  91.       for (int i=0; i<rows; i++)
  92.          for (int j=0; j<columns; j++)
  93.             color[i][j] = null;
  94.       Color c = getBackground();
  95.       g.setColor(c);
  96.       g.fillRect(0,0,width,height);
  97.    }
  98.    
  99.    public void setColor(int row, int column, Color c) {
  100.           // set the color of the rectangle in given row and column
  101.       if (closed || row < 0 || row >= rows || column < 0 || column >= columns)
  102.          return;
  103.       color[row][column] = c;
  104.       g.setColor(c);
  105.       g.fillRect(column*rectWidth,row*rectHeight, rectWidth, rectHeight);
  106.    }
  107.    
  108.    public Color getColor(int row, int column) {
  109.           // read the color of the rectangle in the given row and column
  110.       if (closed)
  111.          return null;
  112.       if (color[row][column] != null)
  113.          return color[row][column];
  114.       else
  115.          return getBackground();
  116.    }
  117.    
  118.    public double getRed(int row, int column) {
  119.           // Get the red component of the color in given row and column.
  120.           // The value returned is between 0.0 and 1.0, with 0.0 indicating
  121.           // that the color contains no red at all, and 1.0 indicating that
  122.           // it contains the maximum possible amount of red.
  123.       if (closed)
  124.          return 0;
  125.       Color c = getColor(row,column);
  126.       return c.getRed() / 255.0;
  127.    }
  128.    
  129.    public double getGreen(int row, int column) {
  130.           // Get the green component of the color in given row and column.
  131.           // The value returned is between 0.0 and 1.0, with 0.0 indicating
  132.           // that the color contains no green at all, and 1.0 indicating that
  133.           // it contains the maximum possible amount of green.
  134.       if (closed)
  135.          return 0;
  136.       Color c = getColor(row,column);
  137.       return c.getGreen() / 255.0;
  138.    }
  139.    
  140.    public double getBlue(int row, int column) {
  141.           // Get the blue component of the color in given row and column.
  142.           // The value returned is between 0.0 and 1.0, with 0.0 indicating
  143.           // that the color contains no blue at all, and 1.0 indicating that
  144.           // it contains the maximum possible amount of blue.
  145.       if (closed)
  146.          return 0;
  147.       Color c = getColor(row,column);
  148.       return c.getBlue() / 255.0;
  149.    }
  150.    
  151.    public void setColor(int row, int column, double red, double green, double blue) {
  152.           // Set the color of the rectangle in the given row and column.
  153.           // The parameters "red", "blue", and "green" should be between
  154.           // 0.0 and 1.0 (if not, they will be adjusted to lie in that range).
  155.           // They specify the level of red, blue, and green in the color.
  156.       if (closed)
  157.          return;
  158.       if (red < 0.0)
  159.          red = 0.0;
  160.       else if (red > 1.0)
  161.          red = 1.0;
  162.       if (green < 0.0)
  163.          green = 0.0;
  164.       else if (green > 1.0)
  165.          green = 1.0;
  166.       if (blue < 0.0)
  167.          blue = 0.0;
  168.       else if (blue > 1.0)
  169.          blue = 1.0;
  170.       Color c = new Color((float)red, (float)green, (float)blue);
  171.       setColor(row,column,c);
  172.    }
  173.    
  174.    public void setAll(double red, double green, double blue) {
  175.           // Set the color of all the squares in the window to the same value.
  176.           // The parameters "red", "blue", and "green" should be between
  177.           // 0.0 and 1.0 (if not, they will be adjusted to lie in that range).
  178.           // They specify the level of red, blue, and green in the color.
  179.       if (closed)
  180.          return;
  181.       if (red < 0.0)
  182.          red = 0.0;
  183.       else if (red > 1.0)
  184.          red = 1.0;
  185.       if (green < 0.0)
  186.          green = 0.0;
  187.       else if (green > 1.0)
  188.          green = 1.0;
  189.       if (blue < 0.0)
  190.          blue = 0.0;
  191.       else if (blue > 1.0)
  192.          blue = 1.0;
  193.       Color c = new Color((float)red, (float)green, (float)blue);
  194.       for (int row = 0; row < rows; row++)
  195.          for (int col = 0; col < columns; col++)
  196.             color[row][col] = c;
  197.       g.setColor(c);
  198.       g.fillRect(0,0,width,height);
  199.    }
  200.  
  201.    public void paint(Graphics g) {
  202.           // This is called by the system when the window needs to be
  203.           // repainted.  It is not meant to be called by a program.
  204.       if (color == null)
  205.          g.clearRect(0,0,size().width,size().height);
  206.       else {
  207.          for (int col = 0; col < columns; col++)
  208.             for (int row = 0; row < rows; row++)
  209.                if (color[row][col] != null) {
  210.                   g.setColor(color[row][col]);
  211.                   g.fillRect(col*rectWidth,row*rectHeight, rectWidth, rectHeight);
  212.                }
  213.       }
  214.    }
  215.    
  216.    public boolean handleEvent(Event evt) {
  217.           // This is called by the system when some event occurs.
  218.           // It is not meant to be called by a program.
  219.       if (evt.id == Event.WINDOW_DESTROY) {
  220.          closed = true;  // user has clicked window's close box
  221.          dispose();
  222.          return true;
  223.       }
  224.       else
  225.          return super.handleEvent(evt);
  226.    }
  227.       
  228.    private int rows;                 // number of rows of squares
  229.    private int columns;              // number of columns
  230.    private  Color[][] color;         // color of each rectangl