绘制红线的延长和收回
为红线设置三个状态:0摇摆,1延长,2收回
利用switch语句,当case为0时使用前文编写的摇摆代码,为1时(添加鼠标左键e点击事件,点击后红线状态为1)延长,并返回状态为2,使红线延长后可自动返回
线类:
public class Line {
//起点坐标
int x=380;
int y=180;
//终点坐标
int endx=500;
int endy=500;
//线长
double length=100;//根据线长和角度计算endx和endy
double n=0;
//方向
int dir=1;
//状态 0 摇摆 1 向下抓取 2 收回
int state;
void lines(Graphics g){//红线绘制
endx = (int)(x + length * Math.cos(n*Math.PI));//jiaodu是double,endx是int,因此需要强制转化
endy = (int)(y + length * Math.sin (n*Math.PI));
g.setColor(Color.red);//添加颜色
g.drawLine(x,y,endx,endy);//绘制线条
}
void paintSelf(Graphics g){
switch (state){
case 0://摆动
if(n<0.1){dir=1;}//n的取值应该为0-1,若n<0(转圈到上面了),让线顺时针旋转,dir=