📄 celebritypainter.java
字号:
optSmall = new Checkbox("Small", grpBrushSize, true);
gblControls.setConstraints(optSmall, gbcControls);
add(optSmall);
optMedium = new Checkbox("Medium", grpBrushSize, false);
gblControls.setConstraints(optMedium, gbcControls);
add(optMedium);
optLarge = new Checkbox("Large", grpBrushSize, false);
gblControls.setConstraints(optLarge, gbcControls);
add(optLarge);
}
//
// fillCombo:
// Fills the combo box of celebrity brushes with names
// given as parameters through the html.
// The number of brushes is unlimited.
//
public void fillCombo(Choice cboList)
{
String sListItem; // String to add as a combo choice
int iNum; // Current parameter to get
// Look first for parameter brush1.
iNum = 1;
while(true)
{
sListItem = appParent.getParameter("brush" + iNum);
// Stop looping when there are no more brush parameters.
if(sListItem == null) break;
cboList.addItem(sListItem);
// Look for the next brush parameter incrementally.
iNum++;
}
}
//
// action:
// Act on the afftected control panel component.
//
public boolean action(Event e, Object arg)
{
int iSelectedLen; // Length of selected brush name in combo box
// Used for erasing.
// Process celebrity name combo box event.
if(e.target instanceof Choice)
{
// Play sound for changing celebrities.
appParent.sndChange.play();
// Tell user that the brush is being loaded.
appParent.screenGC.setColor(Color.white);
appParent.screenGC.fillRect(0, 180, 160, 200);
appParent.screenGC.setColor(Color.black);
appParent.screenGC.drawString("Loading Brush.....", 20, 195);
// Turn off the eraser, in case it was on.
optEraser.setState(false);
// Set the thumbnail celebrity image to one matching the selected name.
bcBrushPic.setImage(convertImageString((String)arg, "BR"));
sSelected = convertImageString((String)arg, "");
bcBrushPic.repaint();
// Load the selected name's celebrity image into the applet's brush.
appParent.bshCeleb.setImage(sSelected);
// Erase the loading message by redrawing the applet's screen.
appParent.screenGC.drawImage(appParent.drawnPic, 0, 0, appParent);
}
// Process option event.
if(e.target instanceof Checkbox)
{
// Process eraser event
if(e.target == optEraser)
{
// Play sound for changing celebrities
appParent.sndChange.play();
// Load the eraser
if(optEraser.getState() == true)
{
// Tell user that the eraser is being loaded.
appParent.screenGC.setColor(Color.white);
appParent.screenGC.fillRect(0, 180, 160, 200);
appParent.screenGC.setColor(Color.black);
appParent.screenGC.drawString("Loading Eraser.....", 20, 195);
// Set the thumbnail to image of background.
bcBrushPic.setImage("ERASERBR.jpg");
bcBrushPic.repaint();
// Load the eraser image into the applet's brush.
appParent.bshCeleb.setImage("ERASER.jpg");
// Erase the loading message by redrawing the applet's screen.
appParent.screenGC.drawImage(appParent.drawnPic, 0, 0, appParent);
}
// Reload last selected brush.
else
{
// Tell user that the brush is being loaded.
appParent.screenGC.setColor(Color.white);
appParent.screenGC.fillRect(0, 180, 160, 200);
appParent.screenGC.setColor(Color.black);
appParent.screenGC.drawString("Loading Brush.....", 20, 195);
// Get length of string holding name of selected celeb's image file.
iSelectedLen = sSelected.length();
// Set the thumbnail celebrity image to one matching the selected name.
bcBrushPic.setImage(convertImageString(sSelected.substring(0, iSelectedLen - 4), "BR"));
bcBrushPic.repaint();
// Load the selected name's celebrity image into the applet's brush.
appParent.bshCeleb.setImage(sSelected);
// Erase the loading message by redrawing the applet's screen.
appParent.screenGC.drawImage(appParent.drawnPic, 0, 0, appParent);
}
}
// Process brush size event
else
{
// Play sound for changing brush size
appParent.sndBrush.play();
// Set the brush size to correspond to the option button set to true.
if(optSmall.getState() == true) iBrushSize = SMALL;
else if(optMedium.getState() == true) iBrushSize = MEDIUM;
else if(optLarge.getState() == true) iBrushSize = LARGE;
}
}
// Process clear button.
if(e.target instanceof Button)
{
// Clear the applet's screen, setting it back to a blank face.
appParent.clear();
appParent.sndClear.play();
}
return true;
}
//
// convertImageString:
// Convert a celebrity name from the combo box into the name
// of a jpg file to be loaded. Do this by stripping spaces, adding
// any extra characters, and adding the ".jpg" extension.
//
public String convertImageString(String sImageString, String sExt)
{
String sImageName = null; // String to hold the final image name
int iSpace = 0; // Location of current space
int iLastSpace = (-1); // Location of last space
int iNameLength = 0; // Length of given string
iNameLength = sImageString.length();
// Keep stripping spaces until there are no more.
while(iSpace != (-1))
{
iSpace = sImageString.indexOf(" ", iLastSpace + 1);
// If a space was found, add the next string segment to the file name.
if(iSpace != (-1))
{
if(sImageName == null) sImageName = sImageString.substring(iLastSpace + 1, iSpace);
else sImageName += sImageString.substring(iLastSpace + 1, iSpace);
iLastSpace = iSpace;
}
} // end of while
// Add the string segment found after the last space (or no space).
if(sImageName == null) sImageName =
sImageString.substring(iLastSpace + 1, iNameLength) + sExt + ".jpg";
else sImageName += sImageString.substring(iLastSpace + 1, iNameLength) + sExt + ".jpg";
return sImageName;
}
} // end of ControlPanel
//
// class BrushCanvas:
// Canvas to paint thumbnail of currently selected celebrity on.
//
class BrushCanvas extends Canvas
{
// MEMBERS /////////////////////////////////////////////
Applet appParent;
Brush bshCelebPic; // Brush used to hold current celebrity thumbnail
Image imgCanvasImage;
Graphics canvasImageGC;
public BrushCanvas(String sImageName, Applet ap)
{
appParent = ap;
// Create the canvas' image.
imgCanvasImage = appParent.createImage(40, 50);
canvasImageGC = imgCanvasImage.getGraphics();
// Create a new brush from the given image.
bshCelebPic = new Brush(sImageName, appParent);
// Call set image to copy the image to the canvas.
setImage(sImageName);
}
//
// setImage:
// Copies specified image to canvas by setting it in
// the canvas' brush, and copying the pixles.
// This process is slow, but produces a better quality image
// than simply using getImage() and drawImage().
//
public void setImage(String sImageName)
{
// Set the specified image in the canvas' brush.
bshCelebPic.setImage(sImageName);
// Copy pixels of the canvas' brush to the canvas.
for(int x=0; x<40; x++) {
for(int y=0; y<50; y++) {
canvasImageGC.setColor(new Color(bshCelebPic.iImgPixels[x][y]));
canvasImageGC.drawLine(x, y, x, y);
}
}
}
//
// update:
// Call paint without clearing first.
//
public void update(Graphics g) {
paint(g);
}
//
// paint:
// Copy the canvas to the screen.
//
public void paint(Graphics g)
{
g.drawImage(imgCanvasImage, 0, 0, appParent);
}
} // end of BrushCanvas
//
// class Brush:
// Creates an array of pixel values from a given Image.
//
class Brush implements ImageConsumer
{
// MEMBERS /////////////////////////////////////////////
Applet appParent;
boolean bImgReady; // Flag set when image has been consumed
int iImgXSize, iImgYSize;
int iImgPixels[][]; // Actual pixel values of image
//
// Brush:
// Constructor calls setImage to extract pixels frmo the given image.
//
Brush(String sImageName, Applet ap) {
appParent = ap;
// Extract pixels from given image.
setImage(sImageName);
}
//
// setImage:
// Extracts pixels from a given image.
//
public void setImage(String sImageName)
{
int iTicks;
Image imgCopy;
imgCopy = appParent.getImage(appParent.getDocumentBase(), sImageName);
bImgReady = false;
// Get pixles, but give up after 5 minutes.
imgCopy.getSource().startProduction(this);
iTicks = 5*60*1000; // 5 minutes
while(iTicks>0) {
try {
Thread.currentThread().sleep(100);
} catch (Exception e) {}
if(this.bImgReady)
break;
iTicks -= 100;
}
}
// Function required for Image Consumer.
public void setProperties(java.util.Hashtable dummy) {
}
// Function required for Image Consumer.
public void setColorModel(ColorModel model) {
}
// Function required for Image Consumer.
public void setHints(int dummy) {
}
//
// imageComplete:
// Notifies brush that an image has been consumed by
// setting the image ready flag to true.
//
public void imageComplete(int dummy) {
bImgReady = true;
}
//
// setDimensions:
// Sets variables for dimensions of given image,
// and creates an array to hold the pixels.
//
public void setDimensions(int x, int y) {
iImgXSize = x;
iImgYSize = y;
iImgPixels = new int[x][y];
}
//
// setPixels:
// Take pixels from the one dimensional array and put their
// RGB lookup values into the two dimensional array of pixels.
//
public void setPixels(int x1, int y1, int w, int h,
ColorModel cmModel, byte byPixels[], int iOff, int iScansize) {
int x, y, x2, y2, sx, sy;
x1 = x2 = iOff = 0;
x2 = x1+w;
y2 = y1+h;
sy = iOff;
for(y=y1; y<y2; y++) {
sx = sy;
for(x=x1; x<x2; x++)
iImgPixels[x][y] = cmModel.getRGB(byPixels[sx++]);
sy += iScansize;
}
}
//
// setPixels:
// Take pixels from the one dimensional array and put their
// RGB lookup values into the two dimensional array of pixels.
//
public void setPixels(int x1, int y1, int w, int h,
ColorModel cmModel, int iPixels[], int iOff, int iScansize) {
int x, y, x2, y2, sx, sy;
x1 = x2 = iOff = 0;
x2 = x1+w;
y2 = y1+h;
sy = iOff;
for(y=y1; y<y2; y++) {
sx = sy;
for(x=x1; x<x2; x++)
iImgPixels[x][y] = cmModel.getRGB(iPixels[sx++]);
sy += iScansize;
}
}
} // end of Brush
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -