//鼠标绘制点横纵坐标
int x = -1;
int y = -1;
boolean rubber = false;//橡皮
/*
* 构造方法
*/
public DrawPictureFRame() {
setResizable(false);
setBounds( 500, 100, 574, 460);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("画图程序");
init();
addListener();
}
private void addListener() {
//画板添加鼠标移动事件监听
canvas.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(final MouseEvent e){//拖拽鼠标
if(x > 0 && y > 0){
if(rubber){
g.setColor(backgroundColor);
g.fillRect(x, y, 10, 10);
}else{//鼠标划过的位置画直线
g.drawLine(x, y, e.getX(), e.getY());
}
}
x = e.getX();
y = e.getY();
canvas.repaint();//更新画布
}
});
canvas.addMouseListener(new MouseAdapter(){
public void mouseReleased(final MouseEvent arg0){
x = -1;
y = -1;
}
});
}
转载自原文链接, 如需删除请联系管理员。
原文链接:添加鼠标画笔功能,转载请注明来源!