2

I'm working on some exercises and I've been stuck on this for some hours now (quite new to Java). Anyhow, this is what I'm supposed to do: When I run the program I will have a square in the middle of the screen and when I then click somewhere within that screen another square will be drawn at the place where I clicked and in-between these two points there are supposed to be 10 squares. So wherever I click there should always be 10 squares drawn between.

However, I can't make it to function properly.

This is what I've managed to do so far:

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);


    }

}

I'm pretty sure that I overcomplicated everything since this should be pretty basic.

The end result is supposed to look like this: End result

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

Here's how you move in two directions at once (on a diagonal).

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 に答える