Applet和AWT

Applet程序中所采用的AWT的绘图机制主要涉及三个方法:paint()方法、update()方法和repaint()方法,update()方法和paint()方法都有一个Graphics类参数。Graphics是画图的关键,它可以支持两种绘图:一种是基本的绘图,如:画线、矩形、圆等;另一种是画图象,主要用于动画制作。

  要进行绘图,首先要找到一个Graphics类的对象。update()方法和paint()方法所传递的参数都是Graphics类的对象,因此主要是通过重载它们来进行绘图,这是在动画程序中经常使用的方法。我们还可以通过getGraphics()方法得到一个Graphics类的对象,这个对象和update()方法和paint()方法中所传递的对象一样,都是该成员所对应的Graphics类的对象。得到了Graphics类的对象,就可使用各种绘图方法。

  Graphics中提供的图形绘制方法有:
  paint( ) //进行绘图的具体操作,必须有程序员重写
  update( ) //用于更新图形,先清除背景、前景,再调用paint()
  repaint( ) /*用于重绘图形,在组件外形发生变化,即大小改变或位置移动时,repaint( )方法立即被系统自动调用,而实际上repaint()方法是自动调用update()方法*/

  下面的方法支持基本的绘图和画图像:
  void drawLine( )
  void drawArc( )
  void drawPolygon( )
  void drawRect( )
  void drawRoundRect( )
  void fill3DRect( )
  void fillOval( )
  java.awt.Graphics类
  
输出文字:
  void drawBytes( )
  void drawChars( )
  void drawString( )

  Applet 的AWT绘制举例如下:
  
例6.10
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  public class ArcTest extends Applet implements WindowListener {
     ArcControls controls;
     pulic void init(){ //Applet的入口方法
       setLayout(new BorderLayout());
       ArcCanvas c=new ArcCanvas();
  

     Add("Center",c);
     add("South",controls=new ArcControls(C));
  }
  public void start(){
     controls.setEnabled(true); //激活controls
  }
  public void stop(){
     controls.setEnabled(false);
  }
  public void windowActivated(WindowEvent e){ }
            //重写WindowListener的方法

  public void windowClosed(WindowEvent e){ }
            //重写WindowListener的方法

  public void windowClosing(WindowEvent e){
            //重写WindowListener的方法

     System.exit(0); }

  public void windowDeactivated(WindowEvent e){}
            //重写WindowListener的方法

  public void windowDeiconified(WindowEvent e){}
            //重写WindowListener的方法

  public void windowIconified(WindowEvent e){ }
            //重写WindowListener的方法

  public void windowOpend(WindowEvent e){ }
            //重写WindowListener的方法

  public static void main(String args[]) {
     Frame f=new Frame("ArcTest"); //构造Frame
     ArcTest arcTest=new ArcTest(); //构造arcTest
     atcTest.init();
     arcTest.start();
     f.add("Center",arcTest);
     f.setSize(300,300);
     f.show();

     f.addWindowListener(arcTest);
     }
  }
  class ArcCanvas extends Canvas{ //类ArcCanvas
     int startAngle=0;
     int endAngle=45;
     boolean filled=false;
     Font font;
     public void paint(Graphics g){
        //paint方法,该方法的作用是在Canvas上画图

      Rectangle r=getBounds();
      int hlines=r.height/10;
      int vlines=r.width/10;
      g.setColor(Color.pink);

  for(int i=1;i<=hlines;i++) {
      g.drawLine(0,i*10,r.width,i*10);
      }
  for(int i=1;i<=vlines;i++) {
      g.drawLine(i*10,0,i*10,r.height);
      }
  g.setColor(Color.red);
  if(filled) {
      g.fillArc(0,0,r.width-1,r.height-1,startAngle,endAngle); }
      else { g.drawArc(0,0,r.width-1,r.height-1,startAngle, endAngle);
  }

  g.setColor(Color.black);
  g.setFont(font);
  g.drawLine(0,r.height/2,r.width,r.height/2);
  g.drawLine(r.width/2,0,r.width/2,r.height);
  g.drawLine(0,,0,r.width,r.height);
  g.drawLine(r.width,0,0,r.height);
  int sx=10;
  int sy=r.height-28;
  g.drawString("S="+startAngle,sx,sy);
  g.drawString("E="+ednAngle,sx,sy+14);
  }

  public void redraw(boolean filled,int start,int end){ //重画方法
        this.filled=filled;
        this.startAngle=start;
        this.endAngle=end;
        repaint();
        //通过调用repaint()方法,从而最终调用paint方法完成重画

        }
  }

  class ArcControls extends Panel implements ActionListener { //ArcControls类
        TextFiled s;
        TextFiled e;
        ArcCanvas canvas;

  public ArcControls(ArcCanvas canvas) {
        Button b=null;
        this.canvas=canvas;
        add(s=new TextField("0",4));
        add(e=new TextField("45",4));
        b=new Button("Fill");
        b.addActionListener(this);
        add(b);
        b=new Button("Draw");
        b.addActionListener(this);
        add(b);
  }

  public void actionPerformed(ActionEvent ev) {
          //实现接口ActionListener的方法

     String label=ev.getActionCommand();
     canvas.redraw(label.equals("Fill"),
     Integer.parseInt(s.getText(),trim()),
     Integer.parserInt(e.getText().trim());
     }
  }

Applet与浏览器间的通信

在Applet类中提供了许多方法,使之可以与浏览器进行通信。下面对这些方法进行简要的介绍:
一个Web页可包含一个以上的小应用程序。一个Web页中的多个小应用程序可以直接通过java.applet包中提供的方法进行通信。

  getDocumentBase( ) //返回当前网页所在的URL
  getCodeBase( ) //返回当前applet所在的URL
  getImage(URL base,String target) //返回网址URL中名为target的图像
  getAudioClip(URL base,String target)
                 //返回网址URL中名为target的声音对象

  getParameter(String target ) //提取HTML文件中名为target的参数的值

  同页Applet间的通信
  在java.applet.Applet类中提供了如下方法得到当前运行页的环境上下文AppletContext对象。
  public AppletContext getAppletContext();

  通过AppletContext对象,可以得到当前小应用程序运行环境的信息。AppletContext是一个接口,其中定义了一些方法可以得到当前页的其它小应用程序,进而实现同页小应用程序之间的通信。

  (1)得到当前运行页的环境上下文AppletContext对象
     public AppletContext getAppletContext();
  (2)取得名为name的Applet对象
     public abstract Applet getApplet(String name);
  (3)得到当前页中所有Applet对象
     public abstract Enumeration getApplets();

例6.11
   import java.applet.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Enumeration;
   public class GetApplets extends Applet implemements ActionListener {
     private TextArea textArea; //声明一个TextArea
     private String newline;
     public void init() {
       Button b=new Button("Click to call getApplets()");
       b.addActionListener(this);
       setLayout(new BorderLayout());

     add("North",b);
     textArea=new TextAred(5,40);
     textArea.setEditable(false);
     add("Center",textArea);
     newline=System.getProperty("line,separator");
               //取得系统当前的换行符

   }
     public void actionPerformed(ActionEvent event) {
              /*Button b点击后的事件处理函数*/

       printApplets();
     }
     public String getAppletInfo() {
       return "GetApplets by Dong.li";
   }


   public void printApplets()}
       Enumeration e=getAppletContext().getApplets();
             /*得到当前网页所有的Applet对象*/

       textArea.append("Results of getApplets():" + newline);
       while(e.hasMoreElements()) {
         Applet applet=(Applet) e.nextElement();
         String info=((Applet)applet).getAppletInfo();
            /*逐个取得当前网页Applet对象的信息*/

         if (info!=null) {
           textArea.append("-"+info+newline);
            /*在textArea中输出网页所有Applet的信息*/

         } else {
           textArea.append("-"+applet.getClass().getName()+newline) ;
         }
         }
           textArea.append("__________"+newline+newline);
       }
   }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章