2

我正在做一些练习,现在我已经坚持了几个小时(对 Java 来说很新)。无论如何,这就是我应该做的:当我运行程序时,我将在屏幕中间有一个正方形,然后当我单击该屏幕内的某个位置时,另一个正方形将在我单击的位置绘制并在- 在这两点之间应该有 10 个正方形。所以无论我点击哪里,都应该在它们之间绘制 10 个正方形。

但是,我无法使其正常运行。

到目前为止,这是我设法做到的:

import se.lth.cs.ptdc.window.SimpleWindow;  
import se.lth.cs.ptdc.square.Square;


public class PrintSquares2 {


public static void main(String[] args) {
    SimpleWindow w = new SimpleWindow(600, 600, "PrintSquares2");
    int posX = 300;
    int posY = 300;
    int loop = 0;
    System.out.println("Skriv rotation");
    Square sq1 = new Square(posX,posY,200);
    sq1.draw(w);


            w.waitForMouseClick();
            int destX = w.getMouseX();
            int destY = w.getMouseY();
            System.out.println("Dest X: " + destX + " Dest Y: " + destY);
            System.out.println("Pos X: " + posX + " Pos Y: " + posY);
            SimpleWindow.delay(10);
            //sq1.erase(w);
            int jumpX = (destX - posX) / 10;
            int jumpY = (destY - posY) / 10;
            System.out.println(jumpX);


                while (posX < destX)
                {       
                    posX = posX+10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);       
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);                        
                }

                while (posX > destX)
                {
                    posX = posX-10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }

                while (posY < destY)
                {       
                    posY = posY+10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }

                while (posY > destY)
                {
                    posY = posY-10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }


            SimpleWindow.delay(10);
            sq1.draw(w);

            //SimpleWindow.clear(w);


    }

}

我很确定我把一切都复杂化了,因为这应该是非常基本的。

最终结果应该如下所示: 最终结果

4

3 回答 3

3

这是我解决它的方法:

我不太了解有关文档se.lth.cs.ptdc.square.Square但我假设它会根据左上角的坐标和侧面大小绘制一个正方形。

所以你有你的第一个正方形左上角的坐标和最后一个正方形中心的坐标。有了这个,得到最后一个正方形左上角的坐标并不难:
lastX = centerX - side/2
lastY = centerY - side/2

之后,您会发现起点和终点之间的区别:
diffX = posX - lastX
diffY = posY - lastY

然后再画9个正方形:

for (int i=1; i<10; i++){
    squareX = posX + (diffX/10)*i;
    squareY = posY + (diffY/10)*i;
    Square square = new Square(squareX,squareY,200);         
    square.draw(w);
}

实际上你做的第一部分是对的,只是搞砸了那些不必要的检查。希望能帮助到你。

-
问候,svz。

于 2012-10-11T12:09:08.887 回答
1

同时更新 X 和 Y:

    int jumpX = (destX - posX) / 10;
    int jumpY = (destY - posY) / 10;
    if (posX > destX) {
        int temp = destX;
        destX = posX;
        posX = temp;
    }

    while (posX <= destX)
    {       
            SimpleWindow.delay(100);
            loop++;
            System.out.println("Loop: " + loop);
            System.out.println("Dest X: " + destX + " Dest Y: " + destY);
            System.out.println("Pos X: " + posX + " Pos Y: " + posY);       
            Square sq2 = new Square(posX,posY,200);         
            sq2.draw(w);                        
            posX = posX+jumpX;
            posY = posY+jumpY;
    }    

    SimpleWindow.delay(10);
    sq1.draw(w);
于 2012-10-11T12:10:00.120 回答
1

以下是您如何同时向两个方向移动(在对角线上)。

static final int Steps = 10;

private void test() {
  int x1 = 100;
  int y1 = 100;
  int x2 = 300;
  int y2 = 500;

  double dx = (double)(x2 - x1) / (double) Steps;
  double dy = (double)(y2 - y1) / (double) Steps;

  double x = x1;
  double y = x2;
  for ( int i = 0; i < Steps; i++) {
    // Simulate the drawing of the square.
    System.out.println("("+x+","+y+")");
    x += dx;
    y += dy;
  }
}
于 2012-10-11T12:26:03.770 回答