import   java.applet.*;   
  import   java.awt.*;   
  import   java.awt.event.*;   
  public   class   Canvas   extends   Applet     implements   MouseMotionListener   
    
  {int   x=-1,y=-1;
   int   width=160,height=30;
    public   void   init()   
    {   
        //setBackground(Color.green);           
          addMouseMotionListener(this);   
    }   
    public   void   paint(Graphics   g)   
    {   
        if   (x!=-1   &&y!=-1)   
              
        {   
            g.setColor(Color.red);   
            g.drawRect(x,y,width,height);  
g.fillRect(x,y,width,height);
        }   
    }   
    public   void   mouseDragged(MouseEvent   e)   
    {   
        x=(int)e.getX();   
        y=(int)e.getY();   
        repaint();   
    
    }   
    public   void   mouseMoved(MouseEvent   e)   
    {}   
    public   void   update(Graphics   g)   
    {   
        paint(g);   
    }   
  } // <applet code=Canvas.class width=200 height=200></applet>这是我实现的一个程序,只能在鼠标点击处拖动鼠标来画矩形,我需要的效果是在以有的矩形基础上向左拖动,已有的矩形消失,向右拖动,矩形接着已有的延伸.不知怎么做,还有怎么让矩形沿直线画,谢谢!!