Reputation: 303
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
Upvotes: 2
Views: 810
Reputation: 65813
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;
}
}
Upvotes: 1
Reputation: 13713
Update both X and Y at the SAME time :
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);
Upvotes: 1
Reputation: 4588
This is the way I'd have solved it:
I didn't quite understand the documentation on se.lth.cs.ptdc.square.Square
but I'll assume it draws a square given the coordinates of its top-left corner and a side size.
So you have the coodinates of your first square's left-top corner and the coordinates of the last square's center. Having that it's not difficult to get the coords of the last square's top-left corner:
lastX = centerX - side/2
lastY = centerY - side/2
After you have that you find the difference between the starting and ending points:
diffX = posX - lastX
diffY = posY - lastY
and after that just draw 9 more squares:
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);
}
Actually you did the first part right, just messed up with those unnecessary checks. Hope it helps.
--
Regards, svz.
Upvotes: 3