public class drawPanel extends JPanel implements MouseListener,MouseMotionListener{
Point start=new Point();
int r=20; //设定擦除精度
public void mousePressed(MouseEvent e) {
start.setLocation(e.getX(), e.getY()); //获取点击时的位置
}
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics(); //获取组件绘图环境
if(!e.isMetaDown()){ //判断是否是鼠标左键
g.setColor(Color.BLACK);
g.drawLine(start.x, start.y, e.getX(), e.getY()); //绘线
}
else
{
g.setColor(getBackground());
g.fillOval(e.getX()-r, e.getY()-r, 2*r, 2*r); //擦除
}
start.setLocation(e.getX(), e.getY());
g.dispose();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
利用鼠标实现画图功能 java
最新推荐文章于 2024-06-08 09:54:05 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)