Applet学习-1

    技术2025-03-31  7

    1)  在网页上用Applet实现画图的功能。

    HTML内调用程序:

    <applet code=MyApplet width=300 heigth=300 bgColor=red></applet>

    画图程序:

    import java.awt.*;

     import java.applet.*;

     import java.awt.event.*;

    public class  MyApplet extends Applet

    {

         int orgX;

            int orgY;

            int endX;

            int endY;

            public void paint(Graphics g)

           {

                   g.drawString("this is a simple applet",50,50);

            }

            public void init()

           {

                   addMouseListener(new MouseAdapter()

                  {

                          public void mousePressed(MouseEvent e)

                         {

                                orgX=e.getX();//获得初始点的横坐标

                                orgY=e.getY();//获得初始点的纵坐标

                         }

                         public void mouseReleased(MouseEvent e)

                         {

                                endX=e.getX();//获得末点横坐标

                                endY=e.getY();//获得末点纵坐标

                                getGraphics().drawLine(orgX,orgY,endX,endY);

                         }

                   });

            }

    }

    2)  动画小程序

    package applet;

     

    import java.applet.*;

    import java.awt.*;

     

    public class MyApplet extends Applet implements Runnable{

        int index=0;

        String teString=null;

        Image []imgs=new Image[3];

        public void init()

        {

           for (int i = 0; i < 3; i++) {

               imgs[i]=getImage(getCodeBase(),"zhaopin"+(i+1)+".jpg");

           }

           new Thread(this).start();

        }

        public void paint(Graphics g)

        { 

           g.drawImage(imgs[index], 20, 20, this);

           g.setFont(new Font(null,Font.ITALIC|Font.BOLD,20));

           g.drawString(""+index, 10,20);

       

        }

        public void run() {

           while (true) {

               try {

                  Thread.sleep(100);

               } catch (InterruptedException e) {

                  e.printStackTrace();

               }

               index=(index+1)%3;

               repaint();

           }

          

        }

    }

    问题:图片未显示

    3)  通过网页传递给applet信息

    在网页上设置变量:<param name=interval value=100>

    Applet上获得变量: time1=Integer.parseInt(getParameter("interval"));

    4)  网页上用户和Applet的交互

    网页:

    <html>

      <head>

        <title>MyHtml.html</title>

      </head>

     

      <body bgColor="000000">

       <center>

      <applet

        code="MyApplet1.class"

        name="app"

        width=500

        height=300

        >

       <param name=interval value=100>

       </applet>

       </center>

    <script language="javascript">

    function setTest()

    {

           document.app.text=content.value;

           document.app.repaint();

    }

    </script>

    <input type=text name=content>

    <input type=button value="改变文本"

    οnclick="setTest()">

      </body>

    </html>

    Applet程序:

    import java.applet.*;

    import java.awt.*;

     

    public class MyApplet1 extends Applet implements Runnable{

           public String text="";

           int index=0;

           int time1=100;

           Image []imgs=new Image[10];

           public void init()

           {

                         for (int i = 0; i < 10; i++) {

                         imgs[i]=getImage(getCodeBase(),"images//zhaopin"+(i+1)+".bmp");

                  }

               time1=Integer.parseInt(getParameter("interval"));

                  //new Thread(this).start();

           }

           public void paint(Graphics g)

           {

                    g.drawImage(imgs[index],20,20,this);

                  g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));

                  g.drawString(text, 50, 60);

           }

           public void run() {

                  while (true) {

                         try {

                                Thread.sleep(time1);

                         } catch (InterruptedException e) {

                                e.printStackTrace();

                         }

                         index=(index+1)%10;

                         repaint();

                  }

                 

           }

    }

    最新回复(0)