Question: Files from P02 Dorm Designer (needed for P03 Dorm Designer 3000): **Link for P02 Dorm Designer description : http://cs300-www.cs.wisc.edu/wp/index.php/2018/01/11/p02-dorm-designer-2000/ 1.DormDesigner.jar : http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2018/01/DormDesigner.jar 2.backgrounf.png : http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2018/01/background.png
Files from P02 Dorm Designer (needed for P03 Dorm Designer 3000):
**Link for P02 Dorm Designer description : http://cs300-www.cs.wisc.edu/wp/index.php/2018/01/11/p02-dorm-designer-2000/
1.DormDesigner.jar : http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2018/01/DormDesigner.jar
2.backgrounf.png : http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2018/01/background.png
3.bed.png : http://cs300-www.cs.wisc.edu/wp/wp-content/uploads/2018/01/bed.png
Code for P02 Dorm Designer 2000:
/////////////////////////////// 80 COLUMNS WIDE ///////////////////////////////
public class Main {
public static void main(String[] args) {
Utility.startApplication();
}
public static void setup(Data data) {
data.backgroundImage = data.processing.loadImage("images/background.png");
data.bedImage = data.processing.loadImage("images/bed.png");
data.bedPositions = new float [6][];
data.dragBedIndex = -1;
}
public static void update(Data data){
data.processing.background(100,150,250);
data.processing.image(data.backgroundImage, data.processing.width/2, data.processing.height/2);
int i = 0;
if(data.dragBedIndex != -1){
data.bedPositions [data.dragBedIndex][0] = data.processing.mouseX;
data.bedPositions [data.dragBedIndex][1] = data.processing.mouseY;
}
while(i
if(data.bedPositions[i] == null){
i++;
continue;
}
else{
data.processing.image(data.bedImage, data.bedPositions[i][0], data.bedPositions[i][1]);
i++;
}
}
}
public static void mouseDown(Data data) {
for(int index = 0; index if(data.bedPositions[index] == null){ return; } if(data.processing.mouseX >= data.bedPositions[index][0] - data.bedImage.width/2 && data.processing.mouseX = data.bedPositions[index][1] - data.bedImage.height/2){ data.dragBedIndex = index; break; } else{ continue; } } } public static void mouseUp(Data data) { data.dragBedIndex = -1; } public static void keyPressed(Data data) { if(data.processing.key == 'B' || data.processing.key == 'b'){ for(int i = 0; i if(data.bedPositions[i] == null){ data.bedPositions[i] = new float[2]; data.bedPositions[i][0] = data.processing.width/2; data.bedPositions[i][1] = data.processing.height/2; break; } else{ continue; } } } else{ return; } } } Current assignement: http://cs300-www.cs.wisc.edu/wp/index.php/2018/01/27/p03-dorm-designer-3000/ (same as below) This assignment involves refactoring the code from your P02 Dorm Designer 2000 program into a more object-oriented organization. Within this reorganized code, youll implement the ability to add different kinds of furniture by clicking on buttons, and youll add the ability to rotate or remove furniture using the keyboard. We will continue adding features to this program through the next few weekly assignments, so be sure to keep your code clean, clear, and well commented. Heres an example of what this program might look like when it is done. GETTING STARTED If its not already there, open the project containing your P02 Dorm Designer 2000 code in Eclipse. Make a copy of this project and name it whatever youd like, although P03 Dorm Designer 3000 would be a descriptive choice. Update (Feb 8): The old DormDesigner.jar was not giving very helpful feedback when an exception was thrown from Mains constructor. It was also crashing on a few peoples Macs. Fixes for both of these problems have been implemented can be used by replacing your old file with a fresh download of DormDesigner.jar. OBJECT ORIENTING MAIN: INSTANCE FIELDS AND METHODS Rather than leaving Main full of static methods that make use of a data parameter, well start this project by re-organizing the code within Main into an instantiable class. For all of the static methods, except for the main method, change them from static methods into instance methods and remove the Data parameter that is passed into each of these different methods. Instead, well include the variables that need to be shared across these methods as instantiable fields within the Main class. Youll need to go through and update the references to these instance fields by removing the old data. portion of each reference. Here is a list of the fields from the Data object that your Main class will need: 1 2 3 4 5 private PApplet processing; private PImage backgroundImage; private PImage bedImage; private float[][] bedPositions; private int dragBedIndex; Instead of using your setup() method to initialize these fields, create a constructor for the Main class to do this initialization. Since the processing field is one that you cannot create on your own, well leave this as a parameter that the Utility class will pass into the constructor of your Main class: 1 public Main(PApplet processing) {} Now when you call Utility.startApplication() from main(), the Utility class will create a Main object using this constructor, and will then call the appropriate instance methods on it (instead of those old static methods). Its worth noting that a program could now create as many Main objects as it needs, and that each of these objects can keep track of their own layout, including bedPositions, images, dragBedIndexes, etc. THE BED CLASS Wed like to organize the data related to each bed into their own instantiable objects as well. Create a new Bed class for this purpose. This bed class must utilize the following fields: 1 2 3 4 private PApplet processing; private PImage image; private float[] position; private boolean isDragging; Implement the following constructor and methods using these fields within the Bed class: 1 2 3 4 5 6 7 8 9 10 // initializes the fields of a new bed object positioned in the center of the display public Bed(PApplet processing) { } // draws this bed at its current position public void update() { } // used to start dragging the bed, when the mouse is over this bed when it is pressed public void mouseDown() { } // used to indicate that the bed is no longer being dragged public void mouseUp() { } // helper method to determine whether the mouse is currently over this bed public boolean isMouseOver() { } After implementing these methods, refactor your Main class to begin using these Bed objects. Replace the bedImage, bedPositions, and dragBedIndex fields in Main with a single array of Bed references that begins with 6 null values. When the B key is pressed, a new Bed object should be created, and its reference should replace a null reference within this Bed array. When the array becomes full, no more new Bed object references will be able to be added to it. Also make sure that your Main class calls each Bed objects update(), mouseDown(), and mouseUp() methods at the appropriate times. REMOVING AND ROTATING BEDS When the user holds their mouse over a bed and presses the D key, wed like to delete any bed that the mouse is over from the room. We can do this by replacing the reference in our beds array with null, so that bed is no longer updated, dragged, etc. with the others. Eventually Javas garbage collector will get around to freeing up the memory used by that object. It is up to you to decide whether pressing the D key while the mouse is over several beds results in either removing all of those beds that the mouse is over, vs removing only one of them. When the user holds their mouse over a bed and presses the R key, wed like to rotate that bed which the mouse is over by 90 degrees clockwise. To track and update these rotations, well add a new field and method to our Bed class: 1 2 private int rotations; public void rotate() { } The rotations field will keep track of the number of 90 degree rotations that have been applied to a bed. This number will go up by one each time the rotate() method is called while the mouse is over this bed. In order to see the effect of these rotations, well need to apply these rotations when drawing this bed to the screen, and well also need to account for these rotations when checking whether the mouse is over that bed. For drawing, you can use this overloaded image() method (provided by us, so you will not find documentation about it in the processing library): 1 processing.image(image, position[0], position[1], rotations*PApplet.PI/2); For the isMouseOver() method, an odd number of rotations will lead to a width and height on the screen corresponding to the original images height and width respectively. Your isMouseOver() method should correctly report when the mouse is over a bed, independent of its orientation (number of rotations). CREATING A BUTTON Lets implement a more elegant way to create new bed objects: with a Create Bed button. Create a new class called CreateBedButton with the following instance fields and methods: 1 2 3 4 5 6 7 8 9 10 11 private static final int WIDTH = 96; private static final int HEIGHT = 32; private PApplet processing; private float[] position; private String label; public CreateBedButton(float x, float y, PApplet processing) {} public void update() { } public Bed mouseDown() { } // After step 10, this method will instead return Furniture public boolean isMouseOver() { } This functionality will be quite similar to what you have already written in the Bed class. The two main differences that are further described in the following steps include 1) youll be drawing a rectange with a text label rather than an image, and 2) the mouseDown() method will create a new Bed object, when the mouse is over that button while this method is called. Add a private CreateBedButton instance field to your Main class, and initialize it from Mains constructor to be located at position (50,24), like in the picture at the top of this write-up. The processing method for drawing a rectangle is called rect(), and it takes four paramters: 1) the x-position of the upper left corner, 2) the y-position of the upper left corner, 3) the x-position of the lower right corner, 4) the y-position of the lower right corner. These corner positions must be calculated so that the button appears centered around its position field with the appropriate WIDTH and HEIGHT. Before calling rect(), you should call the fill method to set the color that this rectangle is filled with. When the mouse is over this button set fill(100) for a dark gray, and when the mouse is not over this button set fill(200) for a light gray. After drawing this button, you should draw the label Create Bed. To do so, first call fill(0) to set the text drawing color to black, and then call the processing method text() which takes three parameters: 1) the string of text to draw, 2) the x-position that this text should be centered around, and 3) the y-position that this text should be centered around. Remember that youll need to call the buttons update() method from Mains update method in order for this code to run, and for the button to be displayed as a result. Note that this text() method can also serve as a useful debugging tool, similar to println(). The mouseDown() method should create and return the reference to a new Bed object, when the mouse is over this button. Otherwise, this method should return null. When this buttons mouseDown() method is called from the mouseDown() method in Main, youll need to check its return value to determine whether a new Bed has been created or not. When a new bed has been created, you should add a reference to that bed to your Bed array, by replacing a reference that was formerly null within that array (similar to how pressing B has worked in the past). Once this is working, you should remove the code that causes a bed to be created as a result of pressing the B key. ADDING MORE FURNITURE You might notice while looking through the Bed class that there is nothing terribly bed-specific other than the image that is loaded for this bed. Lets generalize this class so that it can be used for any kind of furniture. Start by right-clicking on the Bed.java file in the Project Explorer and choosing Refactor > Rename. Rename this class from Bed to Furniture. This should update all reference from Bed to Furniture throughout your project. Then add a String type parameter to the Furniture classs constructor to help differentiate between bed furniture and sofa, desk, dresser or any other kind of furniture in the future. The type string that is passed into this constructor should be stored in an instance field within the furniture object, and should also be used to load the correct image: images/ + type + .png. Heres a sofa image that you can add to your images folder to test this out. You should also rename your Furniture[] field in Main from beds to furniture. The Refactor > Rename command in Eclipse can be used to help you update all references to this new identifier at once. There will only be one array for all furniture, so the total number of beds and sofas in your room should remain capped at a maximum of six for the time being. The last step in this assignment is to create one more class: a CreateSofaButton class. This class will function in a manner that is extremely similar to the CreateBedButton. It should have all of the same fields and methods, but will create a Furniture object with the sofa type instead of the bed type when it is clicked. After defining this class, add a private CreateSofaButton instance field to Main, which is initialized to position (150,24). Make sure that the update() and mouseDown() methods are correctly called so that the user can add both sofas and beds to their dorm room. Both types of furniture should still be rotate-able, and delete-able via keyboard commands.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
