Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Window Manager java program. What to Do Open Eclipse and create a project named

ID: 3727406 • Letter: W

Question

Window Manager java program.

What to Do

Open Eclipse and create a project named Assignment05. You'll need StdDraw for this project, so make sure you can import it. You'll need to create implement three classes:

Window

WindowDisplay

WindowDriver

The details of each class are given below:

Window

The Window class represents a single window. It should contain the following:

Instance variables:

Two private variables of type double for the x and y-coordinates of the center of the window.

Two privates variable of type double for the half-width and half-height of the window (remember, StdDraw works with half-dimensions).

A private variable of type Color for the color of the window.

Methods:

A constructor that takes the x and y-coordinates, width, height, and color of the window. It should set the instance variable using these arguments.

A draw() method that draws the window using StdDraw.

A contains(double x, double y) method that takes two arguments of type double: an x and y-coordinate representing a point. This method should return true if the point is inside the window, and false if it is not.

WindowDisplay

The WindowDisplay class stores a list of windows, and updates the list when the mouse is clicked on a specific window.

Instance variables:

A private ArrayList of windows.

Two private int variables that store the width and height of the StdDraw canvas.

Methods:

A constructor that takes two arguments: the width and height of the StdDraw canvas. The constructor should initialize the width and height instance variables and create an empty list. It should also initialize StdDraw with the given width and height (use these for both the scale and size of the canvas).

An addWindow(Window w) method that has one parameter of type Window. This method should add this Window to its list of windows.

A run() method that contains our (infinite) animation loop. This loop is responsible for:

Displaying the canvas.

Capturing mouse clicks. (Hint: StdDraw has StdDraw.isMousePressed(), StdDraw.mouseX(), and StdDraw.mouseY())

Rearranging the windows to move windows that were clicked on to the top.

Create any additional private methods to divide up the work and make your code easier to read. For example, you might have a method for finding which Window was clicked, or a method to rearrange the list of windows to move a given Window to the top.

WindowDriver

The WindowDriver class should contain a main method that runs the simulation. Here is the code I used to create the demonstration video (https://videomanager.du.edu/private/5349999f1abd6):

This code creates a 200 x 200 canvas and then adds four windows to the display. It then starts the simulation.

Extra Credit

For (up to) 10 extra points, make the windows "draggable," so you can move them around with the mouse.

What to Submit

When you are finished, submit a zip of your project to Canvas. This should at least include:

Window.java

WindowDisplay.java

WindowDriver.java

Explanation / Answer

WindowsDriver.java

import edu.princeton.cs.introcs.StdDraw;

/**
* Driver method to create and run a Window Display
*/
public class WindowDriver {
    public static void main(String[] args) {
        WindowDisplay w = new WindowDisplay(200, 200);
        Window w1 = new Window(50, 50, 30, 40, StdDraw.BLUE);
        w.addWindow(w1);
        Window w2 = new Window(100, 130, 40, 40, StdDraw.RED);
        w.addWindow(w2);
        Window w3 = new Window(80, 80, 30, 40, StdDraw.GREEN);
        w.addWindow(w3);
        Window w4 = new Window(120, 60, 50, 40, StdDraw.BLACK);
        w.addWindow(w4);
        w.run();
    }
}

WindowDisplay.java

import edu.princeton.cs.introcs.StdDraw;

import java.util.ArrayList;

class WindowDisplay {
    /**
     * List of all windows to be displayed on the screen
     */
    private ArrayList<Window> windows;
    /**
     * Height and width of StdDraw Canvas
     */
    private int width, height;

    /**
     * Default constructor, creates canvas and arrayList of windows
     *
     * @param width The width of the canvas
     * @param height THe height of the canvas
     */
    WindowDisplay(int width, int height) {
        // initializing values of height, width, and arrayList object
        //NOTE: I personally would probably just pass width and height directly to StdDraw, but the assignment calls for
        // private variables to store this data in the class.
        this.width = width;
        this.height = height;
        this.windows = new ArrayList<>();

        // Setting up canvas
        StdDraw.setCanvasSize(width, height);
        StdDraw.setXscale(0, width);
        StdDraw.setYscale(0, height);
        StdDraw.enableDoubleBuffering();
    }

    /**
     * A method to add a window to the current arraylist of windows
     *
     * @param w The window to add
     */
    void addWindow(Window w) {
        windows.add(w);
    }

    /**
     * A method to run the simulation, drawing and animating all of the windows in the correct stack order
     */
    void run() {

        for (; ; ) {

            // drawing all of the windows in correct order to display "top" window last
            StdDraw.clear();
            for (int i = windows.size() - 1; i >= 0; i--) {
                windows.get(i).draw();
            }

            // mouse pressed actions
            if (StdDraw.mousePressed()) {
                // setting "temp" variables for movement loop
                double x = StdDraw.mouseX();
                double y = StdDraw.mouseY();
                int indexOfWindow = whichWindow();

                // If the "index" of the window the mouse clicked on is -1, the window does not exist (white space)
                if (indexOfWindow >= 0) {
                    // move selected window to the top of the window stack
                    moveToTop(indexOfWindow);

                    Window currentlySelected = windows.get(whichWindow());
                    // while loop to handle movement
                    while (StdDraw.mousePressed()) {
                        if (x != StdDraw.mouseX()) {
                            currentlySelected.move(StdDraw.mouseX() - x, 0);
                            x = StdDraw.mouseX();
                        }
                        if (y != StdDraw.mouseY()) {
                            currentlySelected.move(0, StdDraw.mouseY() - y);
                            y = StdDraw.mouseY();
                        }

                        StdDraw.clear();
                        for (int i = windows.size() - 1; i >= 0; i--) {
                            windows.get(i).draw();
                        }
                        StdDraw.show();
                    }
                }

            }
            StdDraw.show();
        }
    }

    /**
     * A method which determines which window was clicked on
     *
     * @return The index of the window that was clicked on
     */
    private int whichWindow() {
        for (Window w : windows) {
            if (w.contains(StdDraw.mouseX(), StdDraw.mouseY())) {
                return windows.indexOf(w);
            }
        }
        // Returns -1 if the mouse was clicked on a location that isn't a window
        return -1;
    }

    /**
     * A method which moves a window to the top of the window stack
     *
     * @param index The index of the window to move
     */
    private void moveToTop(int index) {
        Window temp = windows.get(index);
        windows.remove(index);
        windows.add(0, temp);
    }

}


Window.java

import edu.princeton.cs.introcs.StdDraw;

import java.awt.*;

class Window {
    /**
     * X and Y coordinates for the center of the window
     */
    private double x, y;
    /**
     * Half width and half heights of the window
     */
    private double width, height;
    /**
     * Color of the window
     */
    private Color color;

    /**
     * Default constructor, initializes values
     *
     * @param x      The X coordinate of the window
     * @param y      The Y coordinate of the window
     * @param width The half width of the window
     * @param height The half height of the window
     * @param color The color of the window
     */
    Window(double x, double y, double width, double height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    /**
     * Method which draws the window in the correct color at the correct location
     */
    void draw() {
        StdDraw.setPenColor(color);
        StdDraw.filledRectangle(x, y, width, height);
    }

    /**
     * A method which determines if a point is inside the window or not
     *
     * @param x The x coordinate of the point
     * @param y The y coordinate of the point
     * @return Whether the point is inside the window or not
     */
    boolean contains(double x, double y) {
        return (x >= this.x - width && x <= this.x + width) &&
                (y >= this.y - height && y <= this.y + height);
    }

    /**
     * A method which moves the window by an amount
     *
     * @param x The amount to move in the x direction
     * @param y The amount to move in the y direction
     */
    void move(double x, double y) {
        this.x += x;
        this.y += y;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote