如何获取鼠标拖动的轨迹啊??

ideal2004 2005-06-30 03:52:22
如何获取鼠标拖动的轨迹啊??我在画矩形时,按下时的点为开始的点,接着鼠标拖动,最好鼠标松开时为结束点。现在需要显示画的轨迹,就像运行windows 自带的画图程序中,点中画矩形时的一样。改怎么画轨迹呢??
...全文
307 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
regandy 2005-06-30
  • 打赏
  • 举报
回复
注册鼠标监听事件就可以了,你运行下下面这个程序吧,看看是不是你要的东西


import java.awt.*;
import java.awt.event.*;
public class ThreeListener implements MouseMotionListener,MouseListener,WindowListener
{
private Frame f;
private TextField tf;
private static int counter=0;
public static void main(String agr[])
{
ThreeListener two= new ThreeListener();
two.go();
}
public void go()
{
f=new Frame("Three listeners example");
f.add(new Label("Click and drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
String s="Mouse dragging;X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e)
{
String s="Mouse is moving and the position is X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseClicked(MouseEvent e)
{

counter+=1;
String s="The times of click is "+counter;
tf.setText(s);
}
public void mouseEntered(MouseEvent e)
{
String s="The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e)
{
String s="The mouse has left the building";
tf.setText(s);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e)
{
System.out.println("window will be closed");
System.exit(0);

}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e)
{
System.out.println("what is the meaning???");
}
public void windowClosed(WindowEvent e)
{
System.out.println("window has been closed");
}
public void windowActivated(WindowEvent e)
{
System.out.println("window has been Activated");
}
public void windowDeactivated(WindowEvent e)
{
System.out.println("window has been deactivated");
}
public void windowOpened(WindowEvent e)
{
System.out.println("Window has been opened");
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧