Question
Write a simulator that allows the user to move and resize a java.awt.Rectangle object. To move a rectangle object, you can use its setLocation method;
Write a simulator that allows the user to move and resize a java.awt.Rectangle object. To move a rectangle object, you can use its setLocation method; and to resize it, simply use the setSize method. You may hardcode a set of actions that the user might do with the simulator. Please see the attached Simulator.java to get an idea. After every action, the simulator should print the description of the action and the state of the rectangle object, for example:
--Move the rectangle to (5, 10): java.awt.Rectangle[x=5,y=10,width=0,height=0]
This simulator must support multi-level undo operations. That is, the simulator must keep track of every undoable action (i.e., move and resize) so that it will be able to undo these actions properly (see the example below). Use the Command Pattern and the Command interface to implement the required features above:
// You must use this interface to implement the Command Pattern
// Do not change this interface.
public interface Command {
public void execute();
public void undo();
}
Note: A real application might draw rectangles on a panel and allow the user to click-and-drag a rectangle to move it or change its size, but the approach to solving this problem is essentially the same.
Example of a simulation:
Simulation
--Create a default rectangle1: java.awt.Rectangle[x=0,y=0,width=0,height=0]
--Create a default rectangle2: java.awt.Rectangle[x=0,y=0,width=0,height=0]
--Move rectangle1 to (5, 10): java.awt.Rectangle[x=5,y=10,width=0,height=0]
--Undo, rectangle1 back to: java.awt.Rectangle[x=0,y=0,width=0,height=0]
--Resize rectangle1 to 5 by 10: java.awt.Rectangle[x=0,y=0,width=5,height=10]
--Resize rectangle2 to 8 by 12: java.awt.Rectangle[x=0,y=0,width=8,height=12]
--Move rectangle2 to (10, 20): java.awt.Rectangle[x=10,y=20,width=8,height=12]
--Undo, rectangle2 back to: java.awt.Rectangle[x=0,y=0,width=8,height=12]
--Undo, rectangle2 back to: java.awt.Rectangle[x=0,y=0,width=0,height=0]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started