/**
*
* - class JavaPixel
*
- a class representing a pixel in a picture
*
* @author Mark Guzdial
* @version 2
*
* modified by Alyce Brady, 17 October 2005
* in an attempt to speed up calls to getPixel and setColor;
* added the getColor, setColor, setColorFrom, and setAlpha methods
*/
public class JavaPixel {
JavaPicture my_pic;
public int x;
public int y;
// private int red;
// private int green;
// private int blue;
// private int alpha;
// int value;
java.awt.Color pxColor;
/**
*
* @param pic the JavaPicture object for each the JavaPixel is a pixel for
* @param px the x coordinate of the pixel in pic
* @param py the y coordinate of the pixel in pic
*/
public JavaPixel ( JavaPicture pic, int px, int py )
{
my_pic = pic;
x = px;
y = py;
pxColor = new java.awt.Color(my_pic.getBasicPixel(x, y));
// value = my_pic.getBasicPixel(x,y);
// red = (value >> 16) & 0xff;
// green = (value >> 8) & 0xff;
// blue = (value ) & 0xff;
// alpha = (value >> 24) & 0xff;
}
// these are integers [0,255]
/**
* Returns the red value of the pixel.
*
* @return the red value in an integer
*/
public int getRed()
{
// return red;
// return (value >> 16) & 0xff;
return pxColor.getRed();
}
/**
* Returns the green value of the pixel.
*
* @return the green value in an integer
*/
public int getGreen()
{
// return green;
// return (value >> 8) & 0xff;
return pxColor.getGreen();
}
/**
* Returns the blue value of the pixel.
*
* @return the blue value in an integer
*/
public int getBlue()
{
// return blue;
// return (value ) & 0xff;
return pxColor.getBlue();
}
/** Returns the alpha value of the pixel.
*
* @return the alpha value in an integer
*/
public int getAlpha()
{
// return alpha;
// return (value >> 24) & 0xff;
return pxColor.getAlpha();
}
/**
* Returns the color of this pixel.
*
* @return the color
*/
public java.awt.Color getColor()
{
return pxColor;
}
// The set versions
/**
* actually sets the integer (or basic) value of the pixel once the separate
* red, green, blue and alpha values are set
*
*/
/* Commented out by Alyce Brady; replaced with methods below.
public void setPixel()
{
value = (alpha << 24) + (red << 16) + (green << 8) + blue;
my_pic.setBasicPixel(x,y,value);
}
*/
/**
* Sets the color value of the pixel from the given parameters.
*
* Added by Alyce Brady.
*/
public void setColor(int red, int green, int blue, int alpha)
{
// value = (alpha << 24) + (red << 16) + (green << 8) + blue;
setColor(new java.awt.Color(red, green, blue, alpha));
}
/**
* Sets the color value of the pixel from the
* given color value.
*
* Added by Alyce Brady.
*/
public void setColor(java.awt.Color c)
{
// value = c.getRGB();
pxColor = c;
my_pic.setBasicPixel(x,y,c.getRGB());
}
/**
* Sets the color value of this pixel from the color value
* of otherPixel
.
* @arg otherPixel a pixel that may be in the same picture
* or a different picture from this pixel
*
* Added by Alyce Brady.
*/
public void setColorFrom(JavaPixel otherPixel)
{
setColor(otherPixel.getColor());
}
/**
* Sets the red value of the pixel.
*
* @param nuRed the red value as an integer
*/
public void setRed(int nuRed)
{
// red = (int) (nuRed & 0xff);
// this.setPixel();
// int red = (int) (nuRed & 0xff);
// this.setPixel(red, getGreen(), getBlue(), getAlpha());
setColor(nuRed, pxColor.getGreen(),
pxColor.getBlue(), pxColor.getAlpha());
}
/**
* Sets the green value of the pixel.
*
* @param nuGreen the green value as an integer
*/
public void setGreen(int nuGreen)
{
// green = (int) (nuGreen & 0xff);
// this.setPixel();
// int green = (int) (nuGreen & 0xff);
// this.setPixel(getRed(), green, getBlue(), getAlpha());
setColor(pxColor.getRed(), nuGreen,
pxColor.getBlue(), pxColor.getAlpha());
}
/**
* Sets the blue value of the pixel.
*
* @param nuBlue the blue value as an integer
*/
public void setBlue(int nuBlue)
{
// blue = (int) (nuBlue & 0xff);
// this.setPixel();
// int blue = (int) (nuBlue & 0xff);
// this.setPixel(getRed(), getGreen(), blue, getAlpha());
setColor(pxColor.getRed(), pxColor.getGreen(),
nuBlue, pxColor.getAlpha());
}
/**
* Sets the alpha (transparency) value of the pixel.
*
* @param nuAlpha the alpha value as an integer
*/
public void setAlpha(int nuAlpha)
{
setColor(pxColor.getRed(), pxColor.getGreen(),
pxColor.getBlue(), nuAlpha);
}
}