Question
In this problem, I am using java in BlueJ to finish the TestTwoDimArray class. The Circle class and the Canvas class are complete and do
In this problem, I am using java in BlueJ to finish the TestTwoDimArray class. The Circle class and the Canvas class are complete and do not need to be changed. The instructions on how to complete the TestTwoDimArray class are included in the code, I have attempted the problem and would like to compare my work against a solved model.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Circle { private int diameter; private int xPosition; private int yPosition; private String color; private boolean isVisible; /** * Create a new circle at given position with given diameter and default color. */ public Circle(int diameter, int xPosition, int yPosition, String color) { this.diameter = diameter; this.xPosition = xPosition; this.yPosition = yPosition; this.color = color; isVisible = false; }
/** * 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; }
/* * 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); } } }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Canvas { // Note: The implementation of this class (specifically the handling of // shape identity and colors) is slightly more complex than necessary. This // is done on purpose to keep the interface and instance fields of the // shape objects in this project clean and simple for educational purposes.
private static Canvas canvasSingleton;
/** * Factory method to get the canvas singleton object. */ public static Canvas getCanvas() { if(canvasSingleton == null) { canvasSingleton = new Canvas("BlueJ Shapes Demo", 400, 200, Color.white); } canvasSingleton.setVisible(true); return canvasSingleton; }
// ----- instance part -----
private JFrame frame; private CanvasPane canvas; private Graphics2D graphic; private Color backgroundColour; private Image canvasImage; private ArrayList
/** * Set the canvas visibility and brings canvas to the front of screen * when made visible. This method can also be used to bring an already * visible canvas to the front of other windows. * @param visible boolean value representing the desired visibility of * the canvas (true or false) */ public void setVisible(boolean visible) { if(graphic == null) { // first time: instantiate the offscreen image and fill it with // the background colour Dimension size = canvas.getSize(); canvasImage = canvas.createImage(size.width, size.height); graphic = (Graphics2D)canvasImage.getGraphics(); graphic.setColor(backgroundColour); graphic.fillRect(0, 0, size.width, size.height); graphic.setColor(Color.black); } frame.setVisible(visible); }
/** * Draw a given shape onto the canvas. * @param referenceObject an object to define identity for this shape * @param color the color of the shape * @param shape the shape object to be drawn on the canvas */ // Note: this is a slightly backwards way of maintaining the shape // objects. It is carefully designed to keep the visible shape interfaces // in this project clean and simple for educational purposes. public void draw(Object referenceObject, String color, Shape shape) { objects.remove(referenceObject); // just in case it was already there objects.add(referenceObject); // add at the end shapes.put(referenceObject, new ShapeDescription(shape, color)); redraw(); } /** * Erase a given shape's from the screen. * @param referenceObject the shape object to be erased */ public void erase(Object referenceObject) { objects.remove(referenceObject); // just in case it was already there shapes.remove(referenceObject); redraw(); }
/** * Set the foreground colour of the Canvas. * @param newColour the new colour for the foreground of the Canvas */ public void setForegroundColor(String colorString) { if(colorString.equals("red")) graphic.setColor(Color.red); else if(colorString.equals("black")) graphic.setColor(Color.black); else if(colorString.equals("blue")) graphic.setColor(Color.blue); else if(colorString.equals("yellow")) graphic.setColor(Color.yellow); else if(colorString.equals("green")) graphic.setColor(Color.green); else if(colorString.equals("magenta")) graphic.setColor(Color.magenta); else if(colorString.equals("white")) graphic.setColor(Color.white); else graphic.setColor(Color.black); }
/** * Wait for a specified number of milliseconds before finishing. * This provides an easy way to specify a small delay which can be * used when producing animations. * @param milliseconds the number */ public void wait(int milliseconds) { try { Thread.sleep(milliseconds); } catch (Exception e) { // ignoring exception at the moment } }
/** * Redraw ell shapes currently on the Canvas. */ private void redraw() { erase(); for(Iterator i=objects.iterator(); i.hasNext(); ) { ((ShapeDescription)shapes.get(i.next())).draw(graphic); } canvas.repaint(); } /** * Erase the whole canvas. (Does not repaint.) */ private void erase() { Color original = graphic.getColor(); graphic.setColor(backgroundColour); Dimension size = canvas.getSize(); graphic.fill(new Rectangle(0, 0, size.width, size.height)); graphic.setColor(original); }
/************************************************************************ * Inner class CanvasPane - the actual canvas component contained in the * Canvas frame. This is essentially a JPanel with added capability to * refresh the image drawn on it. */ private class CanvasPane extends JPanel { public void paint(Graphics g) { g.drawImage(canvasImage, 0, 0, null); } } /************************************************************************ * Inner class CanvasPane - the actual canvas component contained in the * Canvas frame. This is essentially a JPanel with added capability to * refresh the image drawn on it. */ private class ShapeDescription { private Shape shape; private String colorString;
public ShapeDescription(Shape shape, String color) { this.shape = shape; colorString = color; }
public void draw(Graphics2D graphic) { setForegroundColor(colorString); graphic.fill(shape); } }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class TestTwoDimArray { private final int rows = 10; private final int cols = 20; private final int diameter = 20; /* Add a two-dimensional Circle array field to hold your circles. */
/** * Creates an empty 2-dimensional Circle array. */ /* Write a constructor method that initializes the array. The first dimension should be given size rows, and the second dimension should be given size cols. */
/** * Fill the array with circles. */ /* Write a method called "fillArray" that fills the entire array with blue circles. Give all circles the same diameter using the field defined above. Circles in the same row should have the same yPosition. Circles in the same column should have the same xPosition. The yPosition for row 0 should be 0. The yPosition for row 1 should be diameter. The yPosition for row 2 should be 2*diameter... Continue this pattern for all rows. Use a similar pattern to set the xPosition of each circle in a given column. If you do this correctly, no circle will overlap another circle.
Once you get this working, then fix your code so that it does not add circles to positions in the array that are already occupied by a circle. You can tell whether a position is empty by checking if it contains null. */
/** * Add a random number of circles (between 1 and rows * cols). * Only need one loop here; */ /* Write a method called "addSomeCircles" that tries to add a random number of red circles to the array in a random fashion. The number of circles it tries to add should be between 1 and rows*cols, inclusive. For each circle choose a random row number and a random column number. The x and y positions of the circle should follow the plan used in the "fillArray" method. If the array has no circle at that position, add one, otherwise move on. Your method should return the number of collisions you had while adding circles. Remember, an empty element in the array will be equal (i.e., ==) to null. */
/** * Add a single circle at (row,col). * Make sure you account for arrays having positions from 0 to n and test for entering an invalid position */ /* Write a method called "addSingleCircle" that takes an integer parameter for the row and an integer parameter for the column. If the coordinates specify a valid position in the array, and the array has no circle at that position, then add a green one, otherwise do nothing. The x and y position of the circle should follow the plan used in the "fillArray" method. */ /** * Makes all circles in the array visible. */ /* Write a method called "makeAllCirclesVisible" that makes all circles in the array visible. */
/** * Makes all circles in the array invisible. */ /* Write a method called "makeAllCirclesInvisible" that makes all circles in the array invisible. */
/** * Remove all circles from the array. */ /* Write a method called "emptyArray" that removes all circles from the array (i.e. make them all null.) (Don't forget to make them all invisible first.) */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started