Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:52:19+00:00 2026-06-12T15:52:19+00:00

I’m working on some exercises and I’ve been stuck on this for some hours

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:52:20+00:00Added an answer on June 12, 2026 at 3:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.