static void removeFromLayers(Shape shape) { if (!layerOf.containsKey(shape)) return; int oldLayer = layerOf.get(shape); layerContents.get(oldLayer).remove(shape); if (layerContents.get(oldLayer).isEmpty()) { layerContents.remove(oldLayer); layers.remove((Integer) oldLayer); } layerOf.remove(shape); }
/** * Set the layer that a given shape will be displayed in. Since the shapes in this game are two * dimensional, it must be decided which will appear "on top" when two shapes overlap. The shape * in the higher layer will appear on top. * * <p>Setting a shape's layer will only affect how it is displayed; a shape's layer has no effect * on how it interacts with other shapes. (For example, two shapes can touch even if they are in * different layers. See {@link Shape#isTouching(Shape)}.) * * @param shape the shape whose layer is being set. * @param layer the layer into which this shape will be moved. */ static void setLayer(Shape shape, int layer) { removeFromLayers(shape); // add new stuff if (!layerContents.containsKey(layer)) { layerContents.put(layer, new CopyOnWriteArrayList<Shape>()); int insertionPoint = ~Collections.binarySearch(layers, layer); layers.add(insertionPoint, layer); } layerContents.get(layer).add(shape); layerOf.put(shape, layer); }
/** * Get the layer that the given shape is in. See {@link #setLayer} for more information about * layers. * * @param shape the shape whose layer index will be returned. * @return the index of the layer that <code>shape</code> is contained in. * @see #setLayer */ static int getLayerOf(Shape shape) { return layerOf.get(shape); }