import  java.awt.*;  
import  javax.swing.*;  
import  java.awt.event.*;  
import  java.awt.image.*;  
public  class  TMouseMotion  extends  JApplet  implements  MouseListener  
{  
        int  x,y;  
        int  o_x,o_y;  
        int  flag;  
            int  s_x;  
        int  s_y;  
        int  width;  
        int  height;  
        public  void  init()  
        {  
                CustomListener  ct=new  CustomListener(this);  
                this.addMouseMotionListener(ct);  
                addMouseListener(this);//You  missed  this  line  
        }  
        public  void  update(Graphics  g)  
        {  
                paint(g);  
        }  
        public  void  paint(Graphics  gdraw)  
        {  
     BufferedImage  bufimg=new  BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);  
     Graphics  g=bufimg.getGraphics();  
                g.setColor(this.getBackground());  
                g.fillRect(0,0,getWidth(),getHeight());  
               g.setColor(Color.blue);  
                g.drawString("drag/move  mouse......",5,20);  
                g.setColor(Color.red);  
                if(flag==1)  
                {  
                        g.drawString("don't  move!drag  the  mouse",5,85);  
                        g.drawString("cursor  coordinates:"+x+","+y,5,95);  
                }  
                else  if(flag==2)  
                {  
                        g.drawString("don't  drag!move  the  mouse",5,85);  
                        g.drawString("cursor  coordinates:"+x+","+y,5,95);  
                }  
                g.setColor(Color.blue);  
                g.drawRect(s_x,s_y,width,height);  
                gdraw.drawImage(bufimg,0,0,null);  
        }  
  
        public  void  mousePressed(MouseEvent  e)  
        {  
                o_x=e.getX();  
                o_y=e.getY();  
        }  
        public  void  mouseReleased(MouseEvent  e)  
        {  
                o_x=0;  
                o_y=0;  
        }  
        public  void  mouseClicked(MouseEvent  e)  
        {  
        }  
        public  void  mouseEntered(MouseEvent  e)  
        {  
        }  
        public  void  mouseExited(MouseEvent  e)  
        {  
        }  
}  
class  CustomListener  implements  MouseMotionListener  
{  
        TMouseMotion  tm;  
        public  CustomListener(TMouseMotion  tm)  
        {  
                this.tm=tm;  
        }  
        public  void  mouseMoved(MouseEvent  e)  
        {  
                tm.flag=2;  
                tm.x=e.getX();  
                tm.y=e.getY();  
                tm.repaint();  
        }  
        public  void  mouseDragged(MouseEvent  e)  
        {  
                tm.flag=1;  
                tm.x=e.getX();  
                tm.y=e.getY();  
                if(tm.x>  =tm.o_x  &  &    tm.y<  =tm.o_y)  
                {  
                        tm.s_x=tm.o_x;  
                        tm.s_y=tm.y;  
                        tm.width=tm.x-tm.o_x;  
                        tm.height=tm.o_y-tm.y;  
                }  
                if(tm.x>  =tm.o_x  &  &    tm.y>  =tm.o_y)  
                {  
                        tm.s_x=tm.o_x;  
                        tm.s_y=tm.o_y;  
                        tm.width=tm.x-tm.o_x;  
                        tm.height=tm.y-tm.o_y;  
                }  
                if(tm.x<  =tm.o_x  &  &    tm.y>  =tm.o_y)  
                {  
                        tm.s_x=tm.x;  
                        tm.s_y=tm.o_y;  
                        tm.width=tm.o_x-tm.x;  
                        tm.height=tm.y-tm.o_y;  
                }  
                if(tm.x<  =tm.o_x  &  &    tm.y<  =tm.o_y)  
                {  
                        tm.s_x=tm.x;  
                        tm.s_y=tm.y;  
                        tm.width=tm.o_x-tm.x;  
                        tm.height=tm.o_y-tm.y;  
                }  
  
                tm.repaint();  
  
        }  
  
}

解决方案 »

  1.   

    呵呵,思路?行!
    定义矩形的起使位置,长和宽4个成员变量
    paint()方法中如果不使用双缓存,可以只有一句:g.drawRect(x,y,width,height);
    因为x,y,width,height这4个变量都是在鼠标监听器里面改变的
    当鼠标按下,x,y被重新赋值,width,height有点复杂,应为你可以向相对起使点的4个不同方向拖动鼠标,思想并不复杂,你应该能想通~
    当鼠标拖动的时候,用条件判断确定起使点和长,宽,确定后调用repaint()
    当鼠标释放,再次确定一下x,y,width,height就可以了
    我都昏了,呵呵,如果你不想看我的代码,又被我说晕了的话,把你的代码帖出来吧
      

  2.   

    在mousedown中fillrect,move中做图
      

  3.   

    wes109(我要飞) (  ) 信誉:94 
    你的做法偶认为有缺陷,因为paint函数是系统自己调用的,它将重绘界面,同时也把你绘制的擦掉, 所以, 绘制的代码应该在paint函数里面
    这样不管是你调用repaint还是系统自己调用repaint, 那个矩形始终都在 !
      

  4.   

    楼上的说法当然是大错而特错罗在MOVE中作图,鼠标的一次微小拖动的过程中,你做了几次图?
      

  5.   

    to xioyoo:你为什么说在paint中绘制?我是在paintComponent里绘制,jdk1.3里好像不推荐在paint()里绘制图形。
    这倒不是关键,现在我就是想在画当前矩形时,能把以前的擦掉,可是先用repaint(),再用drawRectangle(),它就连后来的都擦掉了,主要是repaint()只是发请求调用paintComponent(),并不是马上执行paintComponent()。我知道我的做法肯定少了什么,可就是想不出来。不过我先看看你的方法先。
      

  6.   

    我感觉你就直接在Paint()方法中直接画图就可以了
    然后在鼠标的什么事件中只需Repaint()就可以了
      

  7.   

    private void onSingleClick( Point p ){
       repaint() ;   //这句有没有就情况两样,都不对
       for(int i = 0;i <myTable.getCellList().size(); i ++){
           Cell cell = (Cell)myTable.getCellList().get(i) ;
           if( cell.contains(p) ){
           Graphics g = getGraphics() ;
           Color oldColor = g.getColor() ;
           g.setColor(Color.red);
           g.drawRect(cell.x,cell.y,cell.width,cell.height );
           g.setColor(oldColor);
           g.dispose();
        }
       }
    }
      

  8.   

    to 小刀:我就是你这想法,但就是不对,我不用repaint(),而是直接调用paint()就对了,但是刷新太慢。
      

  9.   

    看下面这个例子,我已经运行了
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class DrawP extends JFrame {
      DrawPicture drawArea = new DrawPicture();
      Point point;
      WindowClose a = new WindowClose();
      public DrawP(String title) {
          super(title);
      }  public static void main(String[] args) {
        DrawP drawP1 = new DrawP("Draw");
        drawP1.init();
        drawP1.setSize(400,400);
        drawP1.setVisible(true);
        drawP1.addWindowListener(drawP1.a);
      }
      public void init(){    Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(drawArea);
        drawArea.addMouseListener(drawL);
      }
      MouseAdapter drawL = new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                point = new Point(e.getX(),e.getY());
                repaint();
            }
        };
      class WindowClose extends WindowAdapter{
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        }
      class DrawPicture extends JTextArea{
            DrawPicture(){
                super("Draw circles",10,20);
                repaint();
            }
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.blue);
                if(point!=null){
                    int x=point.x;
                    int y=point.y;
                    g.drawOval(x,y,50,50);
                }
            }
        }
    }
      

  10.   

    你这个成,我知道为什么了,我没用Point来保存e.getPoint();我每次画的是当前的e.getPoint();而你画的是Point里保存的那个,所以就永远是当前的。
    可我还有一事不明白,为什么先repaint(),再drawRect就不行呢?至少看上去可以。分我会给你们的。
      

  11.   

    to 小刀:
    不如直接用Rectangle,这样还能动态决定矩形大小,对吧?
      

  12.   

    to javalearner:
    不知你运行过和好生看过偶第一次给你的代码没有,你后面说的问题,偶第一次给你的代码就已经解决了啊,我给你的代码的效果是动态的,就想即时战略游戏鼠标框选作战单位的效果偶好心寒啊~~~~~~~呵呵
      

  13.   

    to xioyoo:真不好意思,我哪有心情看那么多,现在我看看,分是小问题,关键咱们交流了,不是么?
      

  14.   

    to xioyoo:哥们,能不能把代码发给我,上面写的空行太多,头晕
    [email protected]
      

  15.   

    JDK文档中说明:public void repaint()
    Repaints this component. 
    This method causes a call to this component's update method as soon as possible注意中间的 as soon as possible,即调用repaint不能保证立即重绘,其他如validate等都是同样的。
    保证立即重绘的可以直接调用paint或update。
      

  16.   

    to coinicon:谢了,兄弟,我知道是因为这问题,可我不知道能用paint(),我试试。
      

  17.   

    偶FAINT!
    偶怎么用成GF的帐号了,呵呵,不好意思
      

  18.   

    我也有类似的问题
    代码如下:package myprojects.model;import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;public class Model extends JFrame implements KeyListener
    {
    DrawLinePane pane = new DrawLinePane();

    public Model() {
    super("This is Cartoon");

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }
    });

    setSize(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    addKeyListener(this);

    getContentPane().add(pane);

    show(true);
    }

    public void keyTyped(KeyEvent e)
    {
    int i = 1;
    }

      public void keyReleased(KeyEvent e)
      {
        int i =1;
      } 

    public void keyPressed(KeyEvent e)
    {
    if (e.getKeyCode() == KeyEvent.VK_UP){
    pane.up();
    }else if (e.getKeyCode() == KeyEvent.VK_DOWN){
    pane.down();
    }else if (e.getKeyCode() == KeyEvent.VK_LEFT){
    pane.left();
    }else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
    pane.right();
    }

    }

    public static void main(String args[]) {
    System.out.println("Starting Model...");
    DrawLine line = new DrawLine();
    }
    }
    class DrawLinePane extends JPanel implements Runnable
    {
    private Thread threadRunner = null;

    double xPos = 0;
    double yPos = 0;
    double xMove = 1;
    double yMove = 1;

    DrawLinePane()
    {
    Thread threadRunner = new Thread(this);
    threadRunner.start();
    }
    public void run()
    {

    while(true)
    {
    xPos += xMove;
    yPos += yMove;
    if(xPos > 300 || xPos < 0 )
    {
    xMove *= -1;
    }
    if(yPos > 300 || yPos < 0)
    {
    yMove *= -1;
    }
    repaint();
    try{
    Thread.sleep(100);
    }catch(Exception e){}
    }
    }


    public void paint(Graphics g)
    {
    Graphics2D g2D = (Graphics2D)g;
    Rectangle2D.Double rect2D = new Rectangle2D.Double (xPos, yPos, 10, 10);
    g2D.setColor(Color.black);
    g2D.fill(rect2D);
    }

    void up()
    {
    yMove -= 2;

    }

    void down()
    {
    yMove +=2;
    }

    void left()
    {
    xMove -= 2;
    }

    void right()
    {
    xMove += 2;
    }

    }
      

  19.   

    TO ggyy_csdn(开心) 
    偶已经回复了你的那个帖子了啊,解决地不满意吗?
      

  20.   

    to xioyoo:呵呵,你行,佩服。我的qq在公司不能用,封了。你还是发文本文件给我就成。
      

  21.   

    真不好意思,没有,[email protected]