import java.awt.*;
import java.applet.*;
import java.awt.image.*; // To get the PixelGrabber
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class imagepr2 extends Applet
{
/**
* The entry point for the applet.
*/
Image m_Original, m_Processed, m_Processed2, m_Processed3; // The four image objects to use (last three will be the modified ones)
public void init()
{
//initForm();
//usePageParams();
// TODO: Add any constructor code after initForm call.
MediaTracker Tracker = new MediaTracker(this);
m_Original = getImage(getDocumentBase(), "fotoapplet4.jpg"); // Load the original image
Tracker.addImage(m_Original,0);
try
{
Tracker.waitForID(0);
m_Processed = processImage();
m_Processed2 = processImage2();
m_Processed3 = processImage3();
}
catch(InterruptedException e)
{
}
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the ,
* ,
* and tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
public void paint(Graphics g)
{
// Draws original image
g.drawImage(m_Original, 5, 10, this);
// Draws image on shades of gray
g.drawImage(m_Processed, 5 + m_Original.getWidth(this)+ 2, 10, this);
// Draws brightened image
g.drawImage(m_Processed2, 5, 305, this);
// Draws colored image
g.drawImage(m_Processed3, 5 + m_Original.getWidth(this)+ 2, 305, this);
}
public Image processImage()
{
Image img = null;
int nWidth = m_Original.getWidth(this);
int nHeight = m_Original.getHeight(this);
int nNumPixels = nWidth * nHeight;
int nRawPixels[] = new int[nNumPixels];
PixelGrabber Grabber = new PixelGrabber(m_Original, 0,0,nWidth,nHeight,nRawPixels,0,nWidth);
try
{
Grabber.grabPixels();
ColorModel cm = ColorModel.getRGBdefault(); // Get the color model to create an image from pixel data
MemoryImageSource ImageSource= new MemoryImageSource(nWidth, nHeight, cm, nRawPixels, 0, nWidth);
for(int nPixel=0; nPixel255)
r = 255;
if(g>255)
g = 255;
if(b>255)
b = 255;
nRawPixels[nPixel] = 0xff000000|(r<<16)|(g<<8)|b; // Store color components in the pixel data array
}
img = createImage(ImageSource);
MediaTracker Tracker = new MediaTracker(this);
Tracker.addImage(img, 1);
Tracker.waitForID(1);
}
catch(InterruptedException e)
{
}
return(img);
}
public Image processImage3()
{
Image img = null;
int nWidth = m_Original.getWidth(this);
int nHeight = m_Original.getHeight(this);
int nNumPixels = nWidth * nHeight;
int nRawPixels[] = new int[nNumPixels];
PixelGrabber Grabber = new PixelGrabber(m_Original, 0,0,nWidth,nHeight,nRawPixels,0,nWidth);
try
{
Grabber.grabPixels();
ColorModel cm = ColorModel.getRGBdefault(); // Get the color model to create an image from pixel data
MemoryImageSource ImageSource= new MemoryImageSource(nWidth, nHeight, cm, nRawPixels, 0, nWidth);
for(int nPixel=0; nPixel