⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 celebritypainter.java

📁 java编程的一些Applets例子。值得深入研究一下。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************
 * CelebrityPainter:                      *
 * Applet lets users paint a blank face   *
 * using facial features of celebrities.  *
 *                                        *
 * Created & Programmed by:               *
 * Jeff Orkin                             *
 *                                        *
 * Copyright 1996                         *
 ******************************************/

//
//  To run this applet, you need to create an html page that
//  links in the applet, and provides parameter lines for celebrity 
//  brushes to include.
//  The html to link in the applet should look like this:
//     <APPLET CODE=CelebrityPainter.class WIDTH=300 HEIGHT=200>
//     <PARAM NAME=brush1 VALUE="Drew Barrymore">
//     <PARAM NAME=brush2 VALUE="Jim Carrey">
//     <PARAM NAME=brush3 VALUE="Bill Clinton">
//     </APPLET>
//
//  You can add as many celebrities as you want.  
//  To add celebrities:
//     1. Add a parameter line to the applet tag in the html
//        that has the next sequential brush number, and 
//        gives a name string.
//     2. Provide a 160x200 jpeg of the celebrity, with a
//        filename that matches the name string without spaces.
//     3. Provide a 40x50 jpeg of the celebrity, with a 
//        filename that matches the name string without spaces
//        plus the characters "BR".
//
//  For example, to add a David Letterman brush:
//     1. Add the parameter line 
//        <PARAM NAME=brush4 VALUE="David Letterman">
//     2. Provide a 160x200 jpeg called DavidLetterman.jpg
//     3. Provide a 40x50 jpeg called DavidLettermanBR.jpg
//

 
// I M P O R T S ///////////////////////////////////////////////////////
import java.applet.*;
import java.awt.*;
import java.awt.image.*;


// C L A S S E S /////////////////////////////////////////////////////

//
//  class CelebrityPainter:
//  Main applet class for the applet
//
public class CelebrityPainter extends Applet 
{
   // CONSTANTS //////////////////////////////////////////////////////
   static final int SMALL = 1;
   static final int MEDIUM = 2;
   static final int LARGE = 3;

   // MEMBERS /////////////////////////////////////////////
   ControlPanel cpControls;		// Applet's control panel
   Image drawnPic;			// Double buffer to draw on
   Graphics drawnPicGC;
   Graphics screenGC;
   Brush bshCeleb;			// Brush to paint with
   AudioClip sndClear;
   AudioClip sndChange;
   AudioClip sndBrush;

   //
   //  init:
   //  Set up the applet by setting the background to white and creating
   //  the control panel, brush, and double buffer.
   // 
   public void init()
   {
      this.setBackground(Color.white);

      setLayout(new BorderLayout());

      cpControls = new ControlPanel(this);
      add("East", cpControls);

      // Load event sounds
      sndClear = getAudioClip(getDocumentBase(), "clear.au");
      sndChange = getAudioClip(getDocumentBase(), "change.au");
      sndBrush = getAudioClip(getDocumentBase(), "brush.au");

      // Set brush to the first Celebrity in the combo box of names.
      bshCeleb = new Brush(cpControls.sSelected, this);

      // Create a graphics context for the screen so that 
      // functions other than paint() and update() can draw
      // to the screen.
      screenGC = this.getGraphics();

      // Create a double buffer same size as applet to draw on.
      drawnPic = createImage(size().width, size().height);
      drawnPicGC = drawnPic.getGraphics();

      // Paint the blank face background.
      clear();
   }

   //
   //  clear:
   //  Paint a blank face over whatever's in the buffer,
   //  and repaint the applet.
   //
   public void clear()
   {
      // Draw blue background.
      drawnPicGC.setColor(Color.blue);
      drawnPicGC.fillRect(0, 0, 160, 200);

      // Draw pink oval as blank face.
      drawnPicGC.setColor(Color.pink);
      drawnPicGC.fillOval(26, 12, 108, 176);

      // Copy double buffer to the screen.
      repaint();
   }

   // 
   //  paint:
   //  Copy the double buffer to the screen.
   //
   public void paint(Graphics g)
   {
      g.drawImage(drawnPic, 0, 0, this);
   }

   //
   //  mouseDrag:
   //  This is where most of the action takes place.
   //  Copy the pixels surrounding the mouse from the current
   //  celebrity picture, held in the Brush, to the double buffer.
   //  Finally, copy the modified double buffer to the screen.
   //  The number of pixels affected depends on the current
   //  brush size held in the control panel.
   //
   public boolean mouseDrag(Event e, int x, int y) {
      int i, j; 
      Color pixColor;

      // Ignore mouse draging in the control panel.
      if(x >= 160) return true;

      // Number of pixels affected depends on the brush size.
      switch (cpControls.iBrushSize)
      {
         // small brush
         case SMALL:
            // Copy pixels surrounding mouse from the celebrity brush
            // to the double buffer.

            for(i=x-1; i<=x+1; i++)
            {
               for(j=y-3; j<=y+3; j++) {
                  drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                  drawnPicGC.drawLine(i, j, i, j);
               }
            }

            i=x-3;
            for(j=y-1; j<=y+1; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x-2;
            for(j=y-2; j<=y+2; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x+3;
            for(j=y-1; j<=y+1; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j); 
            }        

            i=x+2;
            for(j=y-2; j<=y+2; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            break;

         // medium brush
         case MEDIUM:
            // Copy pixels surrounding mouse from the celebrity brush
            // to the double buffer.

            for(i=x-2; i<=x+2; i++)
            {
               for(j=y-4; j<=y+4; j++) {
                  drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                  drawnPicGC.drawLine(i, j, i, j);
               }
            }

            i=x-4;
            for(j=y-2; j<=y+2; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x-3;
            for(j=y-3; j<=y+3; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x+4;
            for(j=y-2; j<=y+2; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j); 
            }        

            i=x+3;
            for(j=y-3; j<=y+3; j++) {
               drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
               drawnPicGC.drawLine(i, j, i, j);         
            }

            break;

         // large brush
         case LARGE:
            // Copy pixels surrounding mouse from the celebrity brush
            // to the double buffer.

            for(i=x-3; i<=x+3; i++)
            {
               for(j=y-5; j<=y+5; j++) {
                   drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                   drawnPicGC.drawLine(i, j, i, j);
               }
            }

            i=x-5;
            for(j=y-3; j<=y+3; j++) {
                drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x-4;
            for(j=y-4; j<=y+4; j++) {
                drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                drawnPicGC.drawLine(i, j, i, j);         
            }

            i=x+5;
            for(j=y-3; j<=y+3; j++) {
                drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                drawnPicGC.drawLine(i, j, i, j); 
            }        

            i=x+4;
            for(j=y-4; j<=y+4; j++) {
                drawnPicGC.setColor(new Color(bshCeleb.iImgPixels[i][j]));
                drawnPicGC.drawLine(i, j, i, j);         
            }

            break;
      } // end of switch

      // Copy double buffer to the screen.
      // Done locally rather than calling repaint() for speed. 
      screenGC.drawImage(drawnPic, 0, 0,  this);

      return true;
   }
}  // end of CelebrityPainter


//
//  class ControlPanel:
//  Class responsible for creating and managing
//  the applets GUI.
//
class ControlPanel extends Panel
{
   // CONSTANTS //////////////////////////////////////////////////////
   static final int SMALL = 1;
   static final int MEDIUM = 2;
   static final int LARGE = 3;

   // MEMBERS /////////////////////////////////////////////
   BrushCanvas bcBrushPic;		// canvas to paint thumbnail image of brush on
   Checkbox optSmall;
   Checkbox optMedium;
   Checkbox optLarge;
   Checkbox optEraser;
   int iBrushSize;			// currently selected brush size
   String sSelected;			// currently selected celebrity brush
   CelebrityPainter appParent;

   //
   //  ControlPanel:
   //  Constructor for the Control Panel.
   //  Creates and places components of the GUI.
   //
   public ControlPanel(CelebrityPainter ap)
   {
      Choice cboBrush;
      Button btnClear;
      CheckboxGroup grpBrushSize;
      Label lblBrushSize;
      GridBagLayout gblControls = new GridBagLayout();
      GridBagConstraints gbcControls = new GridBagConstraints();

      appParent = ap;
      
      setFont(new Font("Helvetica", Font.PLAIN, 14));
     
      setLayout(gblControls);

      // Set spacing for controls.
      gbcControls.insets = new Insets(0, 3, 0, 0);

      // cboBrush ///////////////////////////////////////////////////////////////////

      // cboBrush is the last control on row of grid. 
      gbcControls.gridwidth = GridBagConstraints.REMAINDER;

      cboBrush = new Choice();

      gblControls.setConstraints(cboBrush, gbcControls);

      // Fill combo box with names given as parameters to the applet in the html.
      fillCombo(cboBrush);

      // Set selected to start out holding the filename of the jpg of the first
      // celebrity listed in the combo box.
      sSelected = convertImageString(cboBrush.getItem(0), "");
     
      add(cboBrush);


      // bcBrushPic ///////////////////////////////////////////////////////////////////
  
      // Set grid constraints to allow the bush pic and clear button to share a row.
      gbcControls.weightx = 1.0;
      gbcControls.gridwidth = 1;

      // Create the canvas initially showing the thumbnail of the first celebrity in the list.
      bcBrushPic = new BrushCanvas(convertImageString(cboBrush.getItem(0), "BR"), appParent);
      bcBrushPic.resize(40, 50);

      gblControls.setConstraints(bcBrushPic, gbcControls);

      add(bcBrushPic);  


      // btnClear ///////////////////////////////////////////////////////////////////

      // Set grid constraints to make btnClear the last control on the row,
      // and weight it so that it takes up less than half of the row. 
      gbcControls.gridwidth = GridBagConstraints.REMAINDER;
      gbcControls.fill = GridBagConstraints.VERTICAL;
      gbcControls.weightx = 0.4;


      btnClear = new Button("CLEAR");



      gblControls.setConstraints(btnClear, gbcControls);



      add(btnClear);



      // Eraser Option Button  ///////////////////////////////////////////////////////////////////

      // Set grid constraints that let each control from here on takes up a 
      // whole line, which makes them left align. 
      gbcControls.fill = GridBagConstraints.BOTH;

      optEraser = new Checkbox("Eraser");
   

      gblControls.setConstraints(optEraser, gbcControls);



      add(optEraser);



      // Brush Size Option Buttons ///////////////////////////////////////////////////////////////////

      
      lblBrushSize = new Label("Brush Size:");

      gblControls.setConstraints(lblBrushSize, gbcControls);

      add(lblBrushSize);


      grpBrushSize = new CheckboxGroup();

 
      // Set initial brush size to small.
      iBrushSize = SMALL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -