CPW 143 Spring 2017 Programming Assignment 6: ArrayLists

This assignment is primarily about using the methods of the Java container class named ArrayList.

Main Idea:

Write a method named moveToTop for the class named OverLappedRectangles that models a list of possibly overlapping rectangular two-dimensional window regions, like the windows for the programs open on your computer. The order of the rectangles in the list implies the order in which they would display on the screen (sometimes called the “z-order”), from 0 on the bottom to size() 1 on the top.


You are to complete the class OverLappedRectangles.java.You are provided classes named WindowsTester.java, Rectangle143.java, Rectangle.java, Point.java, and code for OverLappedRectangles.java that is complete except for one method, moveToTop, and any helper method you want to use with moveToTop.

WindowsTester contains the method main and instantiates a JFrame to show what is happening with your code. Rectangle143 is a class derived from Rectangle and extends it by adding a Color field for the GUI display. Your OverLappedRectangles class will have a field consisting of an ArrayList of Rectangle143s.

The class named Point is as we have used it before. See page 560 of textbook for description of Point class if you need further documentation.

The JFrame is used to display the rectangles graphically. The provided code handles all interactions with the drawing panel, so you don’t have to do any GUI code at all. When the drawing panel is open and you click the mouse in it, the coordinates of the mouse location appear in the console window and the Point is sent to your moveToTop method. Your moveToTop method will use that Point to determine which Rectangle143 to move.

Your rectangle list class, OverLappedRectangles, will keep an ArrayList of Rectangle143s to implement the model of possibly overlapping rectangular two-dimensional window regions.

Your rectangle list class will have a method, moveToTop, that takes a Point as a parameter and moves the topmost rectangle touching that Point to the front of the list, leaving the order of the rest of the list unchanged. If the Point clicked is not on any of the rectangles, nothing is moved. For example, if we had the configuration you see on the left below and clicked the second from the bottom rectangle, we would get the configuration seen on the right.

To make your moveToTop method work correctly, you will need to determine which Rectangle143 needs to be moved to the top by examining the coordinates of the Point passed to your method. Note that moving a Rectangle143 from its present location to the top can be accomplished by deleting it from the ArrayList and adding it back in again since the ArrayList’s add method always adds the new item to the last index, which corresponds to the rectangle that appears on top of all the others. So, you can use OverLappedRectangles’s addRect and deleteRect methods to accomplish this.

Submit to Canvas: OverLappedRectangles.java

Note: How to tell if a point is in a rectangle:

If Point p is in Rectangle143 r, then:

r.getX() < p.getX() && p.getX() < r.getX() + r.getWidth()

&& r.getY() < p.getY() && p.getY() < r.getY() + r.getHeight()

Leave a Reply

Your email address will not be published. Required fields are marked *

  • File Format: Java