监听器代码
package 画图板重绘;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Mouse implements MouseListener,ActionListener{
Graphics g;
String text = "";//按钮上的文字
int x1,x2,y1,y2;
//动作监听:当点击按钮的时候,获取按钮上的文字
public void actionPerformed(ActionEvent e){
text = e.getActionCommand(http://www.amjmh.com);
}
public void mouseClicked(MouseEvent e){
}
//记录按下鼠标时点的坐标
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
}
//记录鼠标释放时点的坐标,并根据按钮上的文字匹配画图方法
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
if("直线".equals(text))
{
g.drawLine(x1, y1, x2, y2);
}
if("矩形".equals(text))
{
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
---------------------