home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
LANGUAGE
/
JAVA
/
NOTES
/
SOURCE
/
mcanvas.jav
< prev
next >
Wrap
Text File
|
1996-12-20
|
3KB
|
122 lines
// David Eck, eck@hws.edu, August 1996
import java.awt.*;
public class MosaicCanvas extends Canvas {
protected int rows;
protected int columns;
private Color[][] color;
private int height = 0;
private int width = 0;
private int squareHeight;
private int squareWidth;
private int hOffset;
private int vOffset;
MosaicCanvas() {
this(20,20);
}
MosaicCanvas(int ROWS, int COLUMNS) {
rows = ROWS;
columns = COLUMNS;
setBackground(Color.white);
setForeground(Color.black);
color = new Color[rows][columns];
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
color[i][j] = null;
}
void setSize(int w, int h) {
height = h;
width = w;
squareHeight = height/rows;
squareWidth = width/columns;
vOffset = (height - rows*squareHeight) / 2;
hOffset = (width - columns*squareWidth) / 2;
}
public synchronized void paint(Graphics g) {
Dimension d = size();
if (d.height != height || d.width !=width)
setSize(d.width,d.height);
for (int c=0; c<columns; c++)
for (int r=0; r<rows; r++) {
if (color[r][c] == null)
g.setColor(getBackground());
else
g.setColor(color[r][c]);
g.fillRect(hOffset + c*squareWidth, vOffset + r*squareHeight, squareWidth, squareHeight);
}
}
public void update(Graphics g) {
paint(g);
}
public void clear() {
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
color[i][j] = null;
repaint();
}
public synchronized Color getColor(int row, int column) {
if (color[row][column] == null)
return color[row][column];
else
return getBackground();
}
public synchronized double getRed(int row, int column) {
Color c = getColor(row,column);
return c.getRed();
}
public synchronized double getGreen(int row, int column) {
Color c = getColor(row,column);
return c.getGreen();
}
public synchronized double getBlue(int row, int column) {
Color c = getColor(row,column);
return c.getBlue();
}
public synchronized void setColor(int row, int column, Color c) {
if (row < 0 || row >= rows || column < 0 || column >= columns)
return;
Dimension d = size();
if (d.height != height || d.width !=width)
setSize(d.width,d.height);
color[row][column] = c;
Graphics g=getGraphics();
g.setColor(c);
g.fillRect(hOffset + column*squareWidth, vOffset + row*squareHeight, squareWidth, squareHeight);
g.dispose();
}
public synchronized void setColor(int row, int column, double red, double green, double blue) {
Dimension d = size();
if (d.height != height || d.width !=width)
setSize(d.width,d.height);
if (red < 0.0)
red = 0.0;
else if (red > 1.0)
red = 1.0;
if (green < 0.0)
green = 0.0;
else if (green > 1.0)
green = 1.0;
if (blue < 0.0)
blue = 0.0;
else if (blue > 1.0)
blue = 1.0;
Color c = new Color((float)red, (float)green, (float)blue);
setColor(row,column,c);
}
} // end of class MosaicWindow