我试图让填充的圆圈缓慢地移动到更大圆圈的中心 使用Thread.sleep(mili seconds)但它似乎无法工作 - 它就像睡眠工作只有一次虽然它在一个循环中。
这是代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MouseListenerUsing {
static int i =0, R1=75,inCircleX=100,inCircleY=100,oldX,oldY, outCircleX=inCircleX , outCircleY=inCircleY, R2=200,dist=1;
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(430, 460);
final Canvas c=new Canvas(shell,SWT.BORDER);
c.setSize(370,380);
c.setLocation(21, 21);
c.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
e.gc.drawOval(outCircleX-((R2-R1)/2), outCircleY-((R2-R1)/2), R2, R2);
e.gc.fillOval(inCircleX, inCircleY, R1, R1);
}
});
c.addMouseMoveListener(new MouseMoveListener(){
@Override
public void mouseMove(MouseEvent e){
if (i==2)
{
if (Math.sqrt(Math.pow((inCircleX+(R1/2))-((outCircleX-((R2-R1)/2)+(R2/2))),2)+Math.pow((inCircleY+(R1/2))-((outCircleY-((R2-R1)/2))+(R2/2)),2))<=R2/2)
{ // distance between the two centers
inCircleX = e.x+oldX;
inCircleY = e.y+oldY;
c.redraw();
}
}
}
});
c.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {System.exit(0);}
public void mouseDown(MouseEvent e)
{
if (Math.sqrt(Math.pow(e.x-inCircleX-R1/2,2)+Math.pow(e.y-inCircleY-R1/2,2))<=R1/2)
{
oldX=inCircleX-e.x;
oldY=inCircleY-e.y;
i=2;
}
}
public void mouseUp(MouseEvent e)
{
i=1;
int flagY=0,flagX=0;
if ((inCircleX+(R1/2))>(outCircleX-((R2-R1)/2)+(R2/2))) flagX=1;
else if ((inCircleX+(R1/2))<(outCircleX-((R2-R1)/2)+(R2/2))) flagX=0;
if ((inCircleY+(R1/2))>(outCircleY-((R2-R1)/2)+(R2/2))) flagY=1;
else if ((inCircleY+(R1/2))<(outCircleY-((R2-R1)/2)+(R2/2))) flagY=0;
double it = (Math.sqrt(Math.pow((inCircleX+(R1/2))-((outCircleX-((R2-R1)/2)+(R2/2))),2)+Math.pow((inCircleY+(R1/2))-((outCircleY-((R2-R1)/2))+(R2/2)),2))); //distance between centers
while (it>0)
{
if (flagX==1)
{ if ((inCircleX - dist+(R1/2))<(outCircleX-((R2-R1)/2)+(R2/2))) flagX=0;
if (flagX ==1) inCircleX = inCircleX - dist;
}
if ( flagX==0)
{
if ((inCircleX + dist+(R1/2))>(outCircleX-((R2-R1)/2)+(R2/2))) flagX=1;
if (flagX ==0) inCircleX = inCircleX + dist;
}
if ( flagY==1)
{
if ((inCircleY - dist+(R1/2))<(outCircleY-((R2-R1)/2)+(R2/2))) flagY=0;
if (flagY ==1) inCircleY = inCircleY - dist;
}
if (flagY==0)
{
if ((inCircleY + dist+(R1/2))>(outCircleY-((R2-R1)/2)+(R2/2))) flagY=1;
if (flagY ==0) inCircleY = inCircleY + dist;
}
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
c.redraw();
it--;
}
inCircleX = outCircleX;
inCircleY = outCircleY;
c.redraw();
}
});
shell.open();
while(!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
display.dispose();
}
}
有人知道这个问题吗?
谢谢
答案 0 :(得分:4)
首先,你不应该在UI线程(UI分派线程)中睡觉。如果你这样做,你就会暂停UI绘图和工作,这很糟糕。
Thread.sleep
将毫秒作为参数,而不是秒。我非常怀疑你能否认识到是否发生了10毫秒的暂停。我想你想要10秒,10000毫秒。
此外,我认为没有理由在那里睡觉。