我正在做一些练习,而且我已经坚持了几个小时了(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);
}
}
我很确定我过于复杂,因为这应该是非常基本的。
最终结果应该如下所示: End result
答案 0 :(得分:2)
这就是我解决它的方式:
我对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。
答案 1 :(得分: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);
答案 2 :(得分: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;
}
}