简单写的一个大部分用JBuilder生成的,呵呵
package test.org.brunt.applet;import java.awt.*;
import java.awt.event.*;
import java.applet.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 1998</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */public class DrawApplet extends Applet {
    private boolean isStandalone = false;
    private Label l = new Label();
    private int x1=0,y1=0,x2=0,y2=0;
    //Get a parameter value
    public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
            (getParameter(key) != null ? getParameter(key) : def);
    }    //Construct the applet
    public DrawApplet() {
    }
    //Initialize the applet
    public void init() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    //Component initialization
    private void jbInit() throws Exception {        this.addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                System.out.println("x2="+x2 +";y2="+y2);
                DrawApplet.this.repaint();
            }
        });
        this.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e)
            {
                x1=e.getX();
                y1=e.getY();
                System.out.println("x1="+x1 +";y1="+y1);
            }
            public void mouseReleased(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                System.out.println("x2="+x2 +";y2="+y2);
            }
        });
       // this.draw
    }    public void paint(Graphics g)
    {
        super.paintComponents(g);
        //super.repaint(g);
        g.setColor(Color.yellow);
        g.drawRect(x1,y1,x2-x1,y2-y1);
        g.drawOval(x1,y1,x2-x1,y2-y1);
    }    //Get Applet information
    public String getAppletInfo() {
        return "Applet Information";
    }
    //Get parameter info
    public String[][] getParameterInfo() {
        return null;
    }
    //Main method
    public static void main(String[] args) {
        DrawApplet applet = new DrawApplet();
        applet.isStandalone = true;
        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(400,320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
    }
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class Test extends Frame
    {
       public static void main(String[] args)
       {
          new Test();
       }
       public Test()
       {
          this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){dispose();System.exit(0);}});
          this.setSize(300,300);
          this.setLocation(300,0);
          this.add(new Draw());
          this.show();
       }
       protected void processWindowEvent(WindowEvent event)
       {
          super.processWindowEvent(event);
          if(event.getID() == WindowEvent.WINDOW_CLOSING)
          {
             System.exit(0);
          }
       }
    }
    class Draw extends Canvas   //由Canvas
    {
       int x1=-1;
       int y1=-1;
       int x2=-1;
       int y2=-1;
       public Draw()
       {
          this.setBackground(Color.white);
          this.addMouseListener(new MyMouseListener(this));
          this.addMouseMotionListener(new MyMouseMotionListener(this));
       }
       public void paint(Graphics g)
       {
          if(x2==-1 || y2==-1)
          {
             return;
          }
          
          g.drawOval(x1,y1,x2,y2);
         
       }
       public void update(Graphics g)
       {
          this.paint(g);
       }
    }
    class MyMouseListener implements MouseListener
    {
       Draw draw;
       public MyMouseListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseClicked(MouseEvent e)
       {
         
       }
       public void mouseEntered(MouseEvent e)
       {
         
       }
       public void mouseExited(MouseEvent e)
       {
         
       }
       public void mousePressed(MouseEvent e)
       {
        
          draw.x1=e.getX();
          draw.y1=e.getY();
          
          
          
       }
       public void mouseReleased(MouseEvent e)
       {
        //  draw.x2 = e.getX();
        //  draw.y2 = e.getY();
          draw.repaint();
          
       }
    }
    class MyMouseMotionListener implements MouseMotionListener
    {
       Draw draw;
       public MyMouseMotionListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseDragged(MouseEvent e)
       {
          draw.x2 = e.getX();
          draw.y2 = e.getY();
     
       
       }
       public void mouseMoved(MouseEvent e)
       {
          
       }
    }
      

  2.   

    Test.html<HTML>
    <HEAD>
    </HEAD>
    <BODY BGCOLOR="000000">
    <CENTER>
    <APPLET
    code = "Test.class"
    width = "500"
    height = "300"
    >
    </APPLET>
    </CENTER>
    </BODY>
    </HTML>
      

  3.   

    //一楼的好些,没时间了,自己看看吧
    Test.javaimport java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class Test extends Applet {
        private boolean isStandalone = false;
        private Label l = new Label();
        private int x1=0,y1=0,x2=0,y2=0;
      
        public String getParameter(String key, String def) {
            return isStandalone ? System.getProperty(key, def) :
                (getParameter(key) != null ? getParameter(key) : def);
        }
           public Test() {
        }
      
        public void init() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        } 
        private void jbInit() throws Exception {        this.addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e)
                {
                    x2=e.getX();
                    y2=e.getY();
                    System.out.println("x2="+x2 +";y2="+y2);
                    Test.this.repaint();
                }
            });
     
            this.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e)
                {
                    x1=e.getX();
                    y1=e.getY();
                    System.out.println("x1="+x1 +";y1="+y1);
                }
      
                  public void mouseReleased(MouseEvent e)
                {
                    x2=e.getX();
                    y2=e.getY();
                    System.out.println("x2="+x2 +";y2="+y2);
                }
            });    }
     
          public void paint(Graphics g)
        {
            super.paintComponents(g);
            g.setColor(Color.yellow);
            g.drawRect(x1,y1,x2-x1,y2-y1);
            g.drawOval(x1,y1,x2-x1,y2-y1);
        }
     
        public String getAppletInfo() {
            return "Applet Information";
        }    public String[][] getParameterInfo() {
            return null;
        }    public static void main(String[] args) {
            Test applet = new Test();
            applet.isStandalone = true;
            Frame frame;
            frame = new Frame() {
                protected void processWindowEvent(WindowEvent e) {
                    super.processWindowEvent(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(400,320);
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
            frame.setVisible(true);
        }
    }