我的思路是这样的:对panel进行鼠标事件侦听,当鼠标发生坐标变化的时候将鼠标坐标值传递给panel,矩形的位置发生变化

解决方案 »

  1.   

    代码比较乱,没有整理。不过最好你还是继承JComponent做一个自己的控件,在paintComponent方法里绘制图形,然后靠改变它的大小(size)来完成,这样界面的刷新就有系统来完成。
    package test;import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;public class Applet1 extends Applet {
      boolean isStandalone = false;
      int xx = 60;
      int yy = 80;
      //Get a parameter value
      private Image image1;
      boolean isPressed = false;
      XYLayout xYLayout1 = new XYLayout();
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public Applet1() {
      }
      //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception {
        image1=getImage(getCodeBase(),"fan.jpg");
        this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
          public void mouseMoved(MouseEvent e) {
            this_mouseMoved(e);
          }
          public void mouseDragged(MouseEvent e) {
            this_mouseDragged(e);
          }
        });
        this.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            this_mousePressed(e);
          }
          public void mouseReleased(MouseEvent e) {
            this_mouseReleased(e);
          }
        });
        this.setLayout(xYLayout1);
      }
      //Start the applet
      public void start() {
      }
      //Stop the applet
      public void stop() {
      }
      //Destroy the applet
      public void destroy() {
      }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }  public void paint(Graphics g)
     {
         g.clearRect(0,0,this.xx+1,this.yy+1);
         Graphics2D gg = (Graphics2D)g;
         //System.out.println(image1.getHeight(this));
         //g.drawImage(image1,0,0,this);
    //g.drawString("你好!",5,120);
         gg.setStroke(new BasicStroke(2.0F,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
         paintRec(gg,xx,yy);
    }  //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }
      
      void this_mousePressed(MouseEvent e) {
        isPressed = true;
        System.out.println("press");
      }  void this_mouseMoved(MouseEvent e) {
        this.xx = e.getX();
        this.yy = e.getY();
        System.out.println(xx);
      }  void this_mouseReleased(MouseEvent e) {
        isPressed = false;
        this.setCursor(Cursor.getDefaultCursor());
        //System.out.println("Release");
      }  void this_mouseDragged(MouseEvent e) {
        if(isPressed)
        {
          if(Math.abs(e.getX()-xx)<3&&Math.abs(e.getY()-yy)<3)//在矩形最坐下角六个像素内
          {
            this.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));        this.repaint(9,9,this.xx+1,this.yy+1);
            System.out.println(xx);
          }    }
      }
      //Main method
      public static void main(String[] args) {
        Applet1 applet = new Applet1();
        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);
      }  void paintRec(Graphics2D gg,int xx,int yy) {
        gg.drawRect(10,10,xx,yy);
      }}
      

  2.   

    监听鼠标事件
    先记住大矩形原来的位置(old_x,old_y,old_width,old_height)
    鼠标按下时,判断鼠标位置,如果在小矩形内,大矩形可拉伸,否则不能
    在小矩形内时,得到鼠标位置(x,y)在鼠标的移动事件中
    以鼠标位置(x,y)和(old_x,old_y)为对角点不断地画大矩形,并不断擦除刚画的矩形,只留下最后画的
    当然比较麻烦的是大矩形的位置和大小计算,并且画完后你还要重新设置小矩形的位置