我正在做一些练习,现在我已经坚持了几个小时(对 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);
}
}
我很确定我把一切都复杂化了,因为这应该是非常基本的。
最终结果应该如下所示: 最终结果