Joined: July 16th, 2007, 11:15 am Posts: 219 Location: Washington
RS Name:Riceay13 RS Status: P2P
Hi guys!
I need help with writing a method. I have three classes, a Circle class (creates a circle object onto a canvas), a Canvas class, and a SuperCircleDrawer class (and arraylist for Circles).
I need to make a method called drawLargestCircle in the SuperCircleDrawer class that takes no arguments and causes only the largest circle in the collection to be drawn. In the case of a tie, it doesn't matter which of the largest circles you draw, but you should only draw one. All other circles should be made invisible.
I can use for each and/or while loops.
Circle Class
import java.awt.*; import java.awt.geom.*;
/** * A circle that can be manipulated and that draws itself on a canvas. * * @author Eric Lund * @version 9/29/11 */
public class Circle { private int diameter; private int xPosition; private int yPosition; private String color; private boolean isVisible;
/** * Create a new circle at default position with default color. */ public Circle() { diameter = 30; xPosition = 20; yPosition = 60; color = "blue"; isVisible = false; }
/** * Create a new circle with specified diameter. */ public Circle(int size) { diameter = size; xPosition = 20; yPosition = 60; color = "blue"; isVisible = false; }
/** * An accessor for returning the diameter. */ public int getDiameter() { return diameter; }
/** * Make this circle visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); }
/** * Make this circle invisible. If it was already invisible, do nothing. */ public void makeInvisible() { erase(); isVisible = false; }
/** * Move the circle a few pixels to the right. */ public void moveRight() { moveHorizontal(20); }
/** * Move the circle a few pixels to the left. */ public void moveLeft() { moveHorizontal(-20); }
/** * Move the circle a few pixels up. */ public void moveUp() { moveVertical(-20); }
/** * Move the circle a few pixels down. */ public void moveDown() { moveVertical(20); }
/** * Move the circle horizontally by 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); }
/** * Move the circle vertically by 'distance' pixels. */ public void moveVertical(int distance) { erase(); yPosition += distance; draw(); }
/** * Slowly move the circle horizontally by 'distance' pixels. */ public void slowMoveHorizontal(int distance) { int delta;
for(int i = 0; i < distance; i++) { yPosition += delta; draw(); } }
/** * Change the size to the new size (in pixels). Size must be >= 0. */ public void changeSize(int newDiameter) { erase(); diameter = newDiameter; draw(); }
/** * Change the color. Valid colors are "red", "yellow", "blue", "green", * "magenta" and "black". */ public void changeColor(String newColor) { color = newColor; draw(); }
/* * Draw the circle with current specifications on screen. */ private void draw() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter)); canvas.wait(10); } }
/* * Erase the circle on screen. */ private void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } } }
SuperCircleDrawer Class
import java.util.ArrayList;
/** * SuperCircleDrawer is an arraylist for circles. In order to add circles into the * list you must create instances of circles and then add them to SuperCircleDrawer * by the storeCircle method. Once circles have been added, one can perform the * following methods. drawCircles, eraseCircles, moveAllHorizontal, MoveAllVetical, * doubleAllDaimeters, drawLargeCircles, and finally drawLargestCircle. * * In depth detail of the methods can be viewed in the comments above the method. * * @author Eric Lund * @version 9/29/11 */ public class SuperCircleDrawer { private ArrayList<Circle> circleCollection; //declares a field called "circleCollection" that references an ArrayList of Circles. private int counter;
/** * Creates a new constructor that creates a new array list of circles. */ public SuperCircleDrawer() { circleCollection = new ArrayList<Circle>(); //makes an empty arraylist to hold the individual Cicrles. }
/** * Add a circle to the ArrayList * * @param Adds the specified circle into the arraylist. */ public void storeCircle (Circle addcircle) { circleCollection.add(addcircle); }
/** * The method causes all of the circles in the list to become visible. */ public void drawCircles() { for (Circle circles : circleCollection) { circles.makeVisible(); } }
/** * The method causes all of the circles in the list to become invisible. */ public void eraseCircles() { for (Circle circles : circleCollection) { circles.makeInvisible(); } }
/** * Moves all of the circles horzontailly by the specified amount in pixles. If * the specified amount is a negitive number it will result in a leftwards * horizontal shift, where as a postivie number will result in rightward horizontal * shift. * * @param Horizontally moves all the circles left or right by the specified number. */ public void moveAllHorizontal(int distance) { for (Circle circles : circleCollection) { circles.moveHorizontal(distance); } }
/** * Moves all of the circles vertically by the specified amount in pixles. If * the specified amount is a negitive number it will result in a upwards * vertical shift, where as a postivie number will result in downwards vertical * shift. * * @param vertically moves all the circles up or down by the specified number. */ public void moveAllVertical(int distance) { for (Circle circles : circleCollection) { circles.moveVertical(distance); } }
/** * doubles the size of all the circles. */ public void doubleAllDiameters() { for (Circle circles : circleCollection) { circles.changeSize(circles.getDiameter()*2); } }
/** * Draws circles with a diameter larger the 30. Circles with a diameter of 30 or less * will become invisivle. */ public void drawLargeCircle() { for (Circle circles : circleCollection) { if(circles.getDiameter() > 30) { circles.makeVisible(); } else { circles.makeInvisible(); } } }
/** * Draws the biggest circle in the arraylist */ public void drawLargestCircle() { }
Joined: September 3rd, 2007, 12:42 am Posts: 564 Location: Anchorage, AK
RS Name:Addiv RS Status: P2P
For what its worth, here's one solution:
Set an integer variable (largestCircle) to hold the largest index found. Init it to zero and overwrite it to any arraylist index you find that has a larger diameter. Loop arraylist If diameter of circle at current index > diameter of circle at largestCircle set largestCircle = current index End loop
If this is a homework assignment try to do this using the pseudo-code above before looking at the code sample below. I've learned more from mistakes than copying someone else's code.
Spoiler for Code:
Code:
// moved the find logic to a separate method to let draw just draw public int getLargestCircle() { int largestCircle = 0; for(int x=1;x<circleCollection.size();x++) { if(circleCollection.get(x).getDiameter() > circleCollection.get(largestCircle).getDiameter()) { // compares next circle object to largest found so far (largestCircle) largestCircle = x; } } return largestCircle; } public void drawLargestCircle() { do whatever with: circleCollection.get(getLargestCircle()).getDiameter(); }
Joined: July 16th, 2007, 11:15 am Posts: 219 Location: Washington
RS Name:Riceay13 RS Status: P2P
Thanks Addiv. Here's what my method looks like.
/** * Draws the largest circle in the arraylist while hiding all of the others. */ public void drawLargestCircle() { int biggestSize; //Holds the largest circle's diameter. biggestSize = 0;
for(Circle circles : circleCollection) { if(circles.getDiameter() > biggestSize) { biggestSize = circles.getDiameter() +1; // in case of a tie, this allows only one circle to be set to biggestSize } }
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum