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

  1.  
  2. // David Eck, eck@hws.edu, August 1996
  3.  
  4. import java.awt.*;
  5.  
  6. public class MosaicCanvas extends Canvas {
  7.  
  8.    protected int rows;
  9.    protected int columns;
  10.    private Color[][] color;
  11.    private int height = 0;
  12.    private int width = 0;
  13.    private int squareHeight;
  14.    private int squareWidth;
  15.    private int hOffset;
  16.    private int vOffset;
  17.  
  18.    MosaicCanvas() {
  19.       this(20,20);
  20.    }
  21.    
  22.    MosaicCanvas(int ROWS, int COLUMNS) {
  23.       rows = ROWS;
  24.       columns = COLUMNS;
  25.       setBackground(Color.white);
  26.       setForeground(Color.black);
  27.       color = new Color[rows][columns];
  28.       for (int i=0; i<rows; i++)
  29.          for (int j=0; j<columns; j++)
  30.             color[i][j] = null;
  31.    }
  32.    
  33.    void setSize(int w, int h) {
  34.       height = h;
  35.       width = w;
  36.       squareHeight = height/rows;
  37.       squareWidth = width/columns;
  38.       vOffset = (height - rows*squareHeight) / 2;
  39.       hOffset = (width - columns*squareWidth) / 2;
  40.    }
  41.    
  42.    public synchronized void paint(Graphics g) {
  43.        Dimension d = size();
  44.        if (d.height != height || d.width !=width)
  45.           setSize(d.width,d.height);
  46.        for (int c=0; c<columns; c++)
  47.           for (int r=0; r<rows; r++) {
  48.              if (color[r][c] == null)
  49.                 g.setColor(getBackground());
  50.              else
  51.                 g.setColor(color[r][c]);
  52.              g.fillRect(hOffset + c*squareWidth, vOffset + r*squareHeight, squareWidth, squareHeight);
  53.           }  
  54.    }
  55.    
  56.    public void update(Graphics g) {
  57.       paint(g);
  58.    }
  59.    
  60.    public void clear() {
  61.       for (int i=0; i<rows; i++)
  62.          for (int j=0; j<columns; j++)
  63.             color[i][j] = null;
  64.       repaint();
  65.    }
  66.    
  67.    public synchronized Color getColor(int row, int column) {
  68.       if (color[row][column] == null)
  69.          return color[row][column];
  70.       else
  71.          return getBackground();
  72.    }
  73.    
  74.    public synchronized double getRed(int row, int column) {
  75.       Color c = getColor(row,column);
  76.       return c.getRed();
  77.    }
  78.    
  79.    public synchronized double getGreen(int row, int column) {
  80.       Color c = getColor(row,column);
  81.       return c.getGreen();
  82.    }
  83.    
  84.    public synchronized double getBlue(int row, int column) {
  85.       Color c = getColor(row,column);
  86.       return c.getBlue();
  87.    }
  88.    
  89.    public synchronized void setColor(int row, int column, Color c) {
  90.       if (row < 0 || row >= rows || column < 0 || column >= columns)
  91.          return;
  92.       Dimension d = size();
  93.       if (d.height != height || d.width !=width)
  94.           setSize(d.width,d.height);
  95.       color[row][column] = c;
  96.       Graphics g=getGraphics();
  97.       g.setColor(c);
  98.       g.fillRect(hOffset + column*squareWidth, vOffset + row*squareHeight, squareWidth, squareHeight);
  99.       g.dispose();
  100.    }
  101.    
  102.    public synchronized void setColor(int row, int column, double red, double green, double blue) {
  103.       Dimension d = size();
  104.       if (d.height != height || d.width !=width)
  105.          setSize(d.width,d.height);
  106.       if (red < 0.0)
  107.          red = 0.0;
  108.       else if (red > 1.0)
  109.          red = 1.0;
  110.       if (green < 0.0)
  111.          green = 0.0;
  112.       else if (green > 1.0)
  113.          green = 1.0;
  114.       if (blue < 0.0)
  115.          blue = 0.0;
  116.       else if (blue > 1.0)
  117.          blue = 1.0;
  118.       Color c = new Color((float)red, (float)green, (float)blue);
  119.       setColor(row,column,c);
  120.    }
  121.    
  122. }  // end of class MosaicWindow