📄 rectcollection.java
字号:
import objectdraw.*;// maintains a collection of FilledRectspublic class RectCollection { // the array used to hold the rectangles private FilledRect[] theRects; // the number of rectangles currently in the array private int curCount = 0; // Create a collection that can hold 'size' rectangles public RectCollection( int size){ theRects = new FilledRect[size]; } // add a new rectangle to the collection (if there is room) public void add(FilledRect newRect){ if ( curCount < theRects.length){ theRects[ curCount ] = newRect; curCount++; } else { System.out.println("Too many rects in collection"); } } // return a rectangle in the collection that overlaps with the ball // or return null public FilledRect findFirstOverlap(FilledOval ball){ for ( int pos = 0; pos < curCount; pos++) { if ( ball.overlaps(theRects[pos])) { return theRects[pos]; } } return null; } // the following methods provide the means to step through the // collection of rectangles processing all of its members // in sequence // position of next rectangle to process private int curRect; // start processing with the first rectangle in the collection public void goToFirst() { curRect = 0; } // return true if there are more rectangles left to process public boolean rectsLeft(){ return curRect < curCount; } // return the next rectangle to process public FilledRect getNextRect() { if ( rectsLeft() ) { curRect++; return theRects[curRect-1]; } else { return null; } } // ---------- end of methods for processing rects sequentially // return a rectangle that overlaps the ball and remove it from the // collection, otherwise return null public FilledRect removeFirstOverlap(FilledOval ball){ for ( int pos = 0; pos < curCount; pos++) { if ( ball.overlaps(theRects[pos])) { FilledRect result = theRects[pos]; removeRect(pos); return result; } } return null; } public void removeRect(int pos){ theRects[pos] = theRects[curCount-1]; curCount--; } /* alternate way to remove rectangle from the collection for (int target = pos; target < curCount-1; target++){ theRects[target] = theRects[target+1]; } */ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -