This small project builds off of the Circle class implemented in the previous classwork. After completing both exercises, the interface of the Circle class should be as follows (note: the parameter names you chose do not have to match the names given here exactly):
Constructor: public Circle(double radius, double x, double y, double xVel, double yVel) Methods: double getArea() double getCircumference() double getRadius() void setRadius(double newRadius) double getXPos() double getYPos() void setXPos(double newX) void setYPos(double newY) double getXVel() double getYVel() void setXVel() void setYVel() void move()If your class does not conform to the interface given above, continue working on it. Be sure to write Javadoc comments for your class. When you are done, submit the current version of the class as
Circles1, and then begin working on the following exercises.
- Drawing the circle: As a first step towards animating the circle, we are going to draw it in the center of the screen. Start by snarfing the starter code
Circles_Animation. There are two classes,CirclesMain, which contains the main method you run to start the program, andCirclesCanvas, which is a class for a graphical user interface (GUI) component onto which you will draw your circle. You will not need to modify theCirclesMainclass for this project. TheCirclesCanvasclass has two methods of interest, namely the constructor and thepaintComponentmethod.paintComponentis a special method that is automatically called whenever the portion of screen that contains the canvas needs to be redrawn (e.g. if the window is uncovered, or you resize it, etc.). We'll modify this method to draw the circle. Find thepaintComponentmethod and look at the code. Notice that it takes a parameter,pen, which is an object of the classGraphics. Thepenobject has methods for drawing on the canvas.Complete the following steps:
- Add a
Circleinstance variable to theCirclesCanvasclass, and modify the constructor to initialize it. The initial position should be at the center of the canvas; to find the center, you must use thesizeparameter, which is aDimensionobject that contains the width and height of the canvas. Take a look at the Javadoc pages in the Java API Specification to learn how to use theDimensionclass. - Add a method
void draw(Graphics pen)
to your circle class. You will need to add the lineimport java.awt.Graphics;
to the top of the file. Use thepenobject's methods to draw a solid (i.e. filled-in) red circle with the current radius at the current position (which indicates the center). Look at the Javadoc page for theGraphicsclass and find thesetColoranddrawOvalmethods. See if you can figure out how to use these methods to draw the circle at the position stored in the circle's instance variables. - Modify the
paintComponentmethod in theCirclesCanvasso that it calls the circle'sdrawmethod that you just added. By passing in thepenobject as a parameter, you are allowing theCircleobject to draw itself directly on the canvas.
- Add a
- Simple Animation: Once you've drawn the circle, animate it by modifying the
paintComponentmethod in theCircleCanvasclass so that it calls the circle'smovemethod before thedrawmethod is called. Once this is done, pushing the "step" button should perform one step of the animation at a time, and pushing the "run" button should run the animation continuously. Experiment with different velocity values until you find an acceptable speed.When you have gotten the animation working, submit your project as
Circles2before going on. - Bouncing Circle (conditional statements): Modify the
movemethod of theCircleclass so that the circle does not drift outside the canvas. When the circle hits the edge of the canvas, you should change its velocity so that the circle appears to bounce off of the edge. For example, if the circle hits the left or right edges, you should flip the sign of the x-component of the velocity.Make sure that the circle bounces when the edge of the circle touches the edge of the canvas (not the center of the circle). The bouncing should always function correctly, even if the user resizes the window (what parameters do you need to add to the
movemethod so that it knows how big the window currently is?). When you are done, submit the project asCircle3. - Random Circle: Look at the Javadoc page for the
Randomclass in thejava.utilpackage. TheRandomclass generates pseudorandom numbers, which allow your programs to behave in an unpredictable manner.Using
Random, modify theCirclesCanvasclass so that the circle it constructs has a randomly-generated size, starting position, velocity, and color. That is, each time you run the program, the circle's appearance and motion should be different. When you are done, submit the project asCircles4. - Multiple Circles (loops and ArrayLists): Add an additional parameter
numCirclesto theCircleCanvasconstructor. Modify theCircleCanvasclass so that it createsnumCirclecircles with random properties and then animates them all simultaneously. Each circle should move according to its own velocity. In other words, you should modify thepaintComponentmethod to update and draw all of the circles, instead of just one.When you are done, submit the project as
Circles5. - Final Program: The above exercises are the minimum amount of work required for this project. If you complete all of them, and do nothing else, the maximum grade you can earn on this project will be in the B range. If you would like to try for an A, you must add to the
Circleclass some additional functionality of your own choosing. Some examples would be:- Gravity: Simulate gravity by modifying the velocity of the circles in addition to their position at each update.
- User interaction: Read and understand the code that creates and responds to the buttons at the top of the frame. Augment the GUI by adding new buttons that manipulate the motion of the circles when clicked. For instance, you could have a button that randomly "warps" a circle to a new location, or a button that simulates a force by changing the velocity of circle when clicked.
- Collision detection: Modify your code so that only two circles are on the canvas. Augment the Circle class with a method that takes in another Circle object as a parameter and decides whether or not the two circles are currently overlapping. Give some visual feedback (e.g. change colors) to indicate when the circles are overlapping and when they are not. For an even more ambitious extension, try to have the circles bounce off of each other in a realistic manner.
The final version of the project should be submitted as
CirclesFinal. Follow the submission guidelines -- a README file is required for the final submission. The due date will be announced at least a week in advance.