程序代写案例-C212

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
C212 Lab 9
Intro to Software Systems
Instructions:
● Review the requirements given below and complete your work. Please submit all files through
Canvas. While compressing, do not use .rar format!
● The grading scheme is provided on Canvas
● JUnits are not required for this lab
Colliding Shapes
• This lab builds on top of Lab 7, where you drew random shapes on to the Panel/Component based
on a Timer. You can reuse/modify those classes/methods appropriately for this lab
• Here, instead of drawing random shapes, specific shapes will be drawn only on certain key presses.
See table below:

Shape Key
Square S or s
Circle C or c
Rhombus R or r
Octagon O or o
Polygon P or p
Oval V or v
Triangle T or T
Quadrilateral Q or q

● Only one key will be pressed at a time.

● As soon as a shape is drawn, it should start moving in a random direction. You can hard-code the
speed with which all shapes move (can use any logic to implement this. For example, increment the
x and y coordinates of the shape by a specific delta value)

● Each shape should have the following properties
o Random fill color
o Random size (don’t make them too large. Small to medium size should be the aim)
o Shapes can be irregular/regular
o Should be drawn at a random location initially (within the bounds of the Frame)
o There is no limit to total number of shapes per type nor should they only appear in clusters
(like in Lab 7)
o Starts moving in a random direction after being drawn

● While in motion, shapes may collide/overlap with each other. You need to implement the following
rules when they do collide.

● For simplicity of collision detection, implement collision rules only for Squares and Circles. i.e.,
Circles will only collide with other circles & Squares will only collide with other Squares. We can
ignore collision for other shape types (i.e., they can just overlap with each other).

● Collision rules:
o When circles/squares of different fill colors collide, they should swap their fill colors. For
example, Circle 1 has Red fill color and Circle 2 has Blue fill color. When they collide, Circle 1
will now be Blue and Circle 2 will have Red fill color while retaining all other properties (i.e.,
speed & direction of motion will be same as before, radius/size of shape will be same as
before collision)
o When circles/squares of same fill colors collide, they will merge together to form a new
circle/square. The area of the new shape will be thrice the average area of original 2 shapes.
i.e., Area of new shape = 3 * ((area of shape1 + area of shape2) / 2)

● Collision rules are only mandatory for Circles and Squares. There is a 10% bonus for implementing
collision rules for all shapes

● You will again need a Timer, and will also need to implement the ActionListener and KeyListener
interfaces. You can choose the time in milliseconds to provide to the Timer (note that one of your
end goals should be to make all shapes move across the frame seamlessly. The timer value decides
this, so choose it carefully.)

● Starter code is not given to you as you can reuse/modify parts of the ShapeDriver,
ShapeWindow, abstract Shape and other classes from previous lab. Use of both ShapeDriver &
ShapeWindow is not mandatory for this lab i.e., if needed, you could do all the logic through
ShapeDriver itself. Choose whichever approach is convenient for you.

● You can use any logic convenient for you to detect collisions (inbuilt methods or by your own math)
It need not be highly precise but should be able to identify whether circles/squares collided.

● Note: The x and y location for any Shape is actually at the top left hand corner of the Shape (and
not at the center!)
o That is how AWT Graphics draw a Shape
o If needed, in the Shape class, you may create another Point called center (which will track
the center position of the shapes as opposed to the top-left (x,y) coordinates)
o When moving the location of the Shape you can update the center location as well
▪ This makes collision detection better than using the x and y location for drawing the
Shape.
o First, you could just work towards using the Point location in the abstract Shape class
▪ Once collision/motion with this is working, add Point center to your Shape class
and update its x and y value the same as you update location
▪ Then use center to calculate the distance during collision detection. Again, this is
not mandatory but a suggestion on how to improve your collision detection
o Remember, subclasses do not have access to private super class fields and methods
o Adhere to good Object Oriented Design Principles:
▪ Keep data in classes private, and access with get and set methods
▪ Only make methods public if other classes use them. Make them private if only the
current class needs it.
▪ You can use the keyPressed(KeyEvent e)method from KeyListener for
listening to key presses (you would still need to define keyReleased and keyTyped
functions even though you won’t be using them. An empty function body would
suffice)
▪ You can use the getKeyCode() function of KeyEventto check which key has been
pressed. Refer the API here and find out appropriate codes for each letter
https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html


欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468