Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java, write a program that emulates a broom that can sweep an object around the screen. The broom will be represented by a small

In Java, write a program that emulates a broom that can sweep an object around the screen. The broom will be represented by a small ball with diameter 10 that appears when the mouse is pressed and disappears when the mouse is released.

While the mouse is depressed, the broom (i.e. ball) is dragged along by the mouse (the upperleft corner of the broom should always be at the mouse location). In the image below, the broom is partly hidden underneath the cursor arrow. The dirt you are sweeping will be represented by a filled square (30 pixels to a side) shown on the screen. If the broom bumps into the square while being dragged (as shown in the image above), then the square will move the same distance (and in the same direction) as the broom, as it would if you had a real broom.

image text in transcribed

Notice that we are asking you to have the broom push the square, rather than have the mouse drag the square. Thus if the mouse is depressed when it is not over the square, but then is dragged toward the square, the square should be pushed ahead of the mouse only when the circle that represents the broom is moved into a position where it would overlap with the square. Once this happens, the square will be pushed in the same direction and for the same distance as the mouse was just moved. As a result, when the mouse (and the circle) stop moving, the mouse and the circle will still not be on top of the square.

When the mouse leaves the window, you should reset the box back to its original position.

The reference available here (Links to an external site.)will almost certainly be useful in completing this project. The onMouseDrag method will (almost certainly) be useful in this project. There are other methods that can simplify this project; I leave it to you to find them.

As you write your program, you should not worry about the possibility that the mouse is pointing at the square when it is first depressed. If you do this, we expect that the code you write will result in behavior that looks more like dragging than pushing if someone does try depressing the mouse while pointing at the square. That is, in this case, the circular broom will appear in a position overlapping the square and the two shapes will move around the screen together as you drag the mouse. One warning: If you move the broom diagonally while pushing the square, the square will have a tendency to slide off of the broom. This is because movements parallel to the side of the square will not affect the square (and the computer is so fast that some of your diagonal moves will end up parallel to the square). This is a feature not a bug (this is what happens with real brooms), so dont worry about it as long as the square generally is moved along with the broom.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions