图形截取代码

来源:互联网 发布:vmware fusion mac 编辑:程序博客网 时间:2024/06/05 22:30

图形截取代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import javax.swing.text.html.StyleSheet;
//Note : the extension is used for the
//String to Color conversion remove this reference
//and the functions it depends on,to create an applet
//that runs on older browsers without Java 1.2 installed.


/**
 *@author 让痛苦痛苦
 *@ 说明: ImageDisplay这个applet非常简单:完成初试化后,立即在applet标记内搜索参数,然后加载图片
 *  (图片必需与applet存放在同一服务器上),将其显示在一个图形窗筐中,最后在图形窗格上显示相关的文字。
 *下面就是代码
 *
 */
public class ImageDisplay extends Applet{
 
 Image myImg;
 MediaTracker mt;
 String strDisplayOverlay="Copyright 2008 Todd Cook";
  int iOverlayXPosition =20;
  int iOverlayYPosition =20;
  int iFontSize=14;
  Color cOverlayColor =Color.white;
 /**
  *Construct the applet
  */
 
  public ImageDisplay()
  {
   
  }
 
  /***
   *Initialize the applet
   *
   *
   */
 
  public void init()
  {
   try {
    jbInit();
    
      }
      catch (Exception ex) {
       System.out.println (ex.getMessage());
       ex.printStackTrace();
      }
  }
  /**
   *Componen initialization
   */
  
  
   private void jbInit() throws Exception
   {
    mt=new MediaTracker(this);
    //The applet viewers on some IDEs merely take
    //the raw class file ,instead of loading the
    //HTML file for parameters,hence the coding
    //style of testing for nulls ,or otherwise
    //assiging default values.
    //Also one may use the applet viewer
    //in the JDK ,e.g. on Windows
    
    if(getParameter("DisplayImage")!=null)
    {
     myImg =getImage(getDocumentBase(),getParameter("DisplayImage"));
    }
    else
    {
     myImg =getImage(getDocumentBase(),"11.jpg");
     
    }
    
    if(getParameter("DisplayOverlay")!=null)
    {
     strDisplayOverlay=getParameter("DisplayOverlay");
    }
    
    if(getParameter("OverlayXPosition")!=null)
    {
     iOverlayXPosition =Integer.parseInt(getParameter("OverlayXPosition"));
    }
    
    if(getParameter("OverlayYPosition")!=null)
    {
     iOverlayYPosition=Integer.parseInt(getParameter("OverlayYPosition"));
    }
    
    if(getParameter("FontSize")!=null)
    {
     iFontSize=Integer.parseInt(getParameter("FontSize"));
    }
    
    if(getParameter("OverlayColor")!=null)
    {
     StyleSheet s=new StyleSheet();
     cOverlayColor =s.stringToColor(getParameter("OverlayColor"));
    }
    mt.addImage(myImg,1);
    
    try {
     mt.waitForAll();
     //stos program execution
     //until the image(s) are loaded
    }
    catch (Exception ex) {
     ex.printStackTrace();
    }
   }
   public void caint(Graphics g)
   {
    g.drawImage(myImg,0,0,this);
    g.setColor(cOverlayColor);
    g.setFont(new Font("Arial",Font.PLAIN,iFontSize));
    g.drawString(strDisplayOverlay,iOverlayXPosition,iOverlayYPosition);
   }
   /**
    *Get Applet information
    *
    */
   
    public String getAppletInfo()
    {
     return "Display a simple banner display over an image";
     
    }
   
    /**
     *Get parameter info
     */
    
     public String[][] getParameterInfo()
     {
      return null;
     }
    
     /***
      *
      *Main method
      */
      public static void main(String args[])
      {
       ImageDisplay applet=new ImageDisplay();
       Frame frame;
       frame=new Frame()
       {
        protected void processWindowEvent(WindowEvent e)
        {
         super.processWindowEvent(e);
         
         if(e.getID()==WindowEvent.WINDOW_CLOSING)
         {
          System.exit(0);
         }
        }
        public synchronized void setTitle(String title)
        {
         super.setTitle(title);
         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        }
       };
       frame.setTitle("Applet Frame");
       frame.add(applet,BorderLayout.CENTER);
       applet.init();
       applet.start();
       frame.setSize(800,600);
       Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
       frame.setLocation((d.width-frame.getSize().width)/2,(d.height-frame.getSize().height)/2);
       frame.setVisible(true);
      } 
}