我再次在需要的时候来找你。hehehe 我做了一点 SWT,它可以制作线条、圆形、正方形和三角形。我通过绘制所有这些图形的起点和终点来做到这一点。一旦绘制,我想在 x 和 y 轴上移动它。生病把我遇到麻烦的核心放在这里。我通过在画布内安装鼠标监听器来管理线条,并在我单击时听到并放开单击。//这是监听器
MasX.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event event) {
SX += 1;
EX += 1;
canvas.redraw();
//gc.drawLine(SX, SY, EX, EY);
//crearLinea(SX,SY,EX,EY,gc);
}
});
//这是画线方法
public void crearLinea(int SX, int SY, int EX, int EY, GC gc){
int x1 = SX;
int x2 = EX;
int y1 = SY;
int y2 = EY;
System.out.println(SX+" "+SY+" "+EX+" "+EY);
int cont = 0;
float dx, dy, m, y, x;
if (x1>x2){
int ax = x2;
int ay = y2;
x2 = x1;
x1 = ax;
y2 = y1;
y1 = ay;
}
dx = x2 - x1;
dy = y2 - y1;
m = dy/dx;
if (m>=-1&&m<=1){
y = y1;
//System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
for (x = x1 ; x <= x2;x=x+5){
//if (x>=dot&&x<=dot+10||x>=dot*2&&x<=dot*2+10||x>=dot*3&&x<=dot*3+10){
gc.drawLine((int)x, (int)Math.round(y), (int)x, (int)Math.round(y));
y+=m*5;
//}
}
}
if(m>1){
x = x1;
//System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
for (y = y1 ; y <= y2;y=y+3){
if (cont<=5){
//System.out.println(cont);
gc.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
cont++;
}
if(cont>=10){
//System.out.println(cont);
cont=0;
}
if(cont>5){
//System.out.println(cont);
cont++;
}
x+=(1/m)*3;
}
}
if(m<-1){
x = x1;
//System.out.println(m+" "+x1+" "+y1+"....."+x2+" "+y2);
for (y = y1 ; y >= y2;y--){
if(y%10==0||y%8==0||y%12==0){
gc.drawLine((int)Math.round(x), (int)y, (int)Math.round(x), (int)y);
}
x-=(1/m);
}
}
}
它给我的错误是空指针异常并指向 gc.drawline()
希望有人可以帮助我:D