C212/A592 Lab 9
Intro to Software Systems
Instructions:
• Review the requirements given below and Complete your work. Please submit all files through Canvas.
• The grading scheme is provided on Canvas
Lab9: Bouncing Hexagon Game
• We will be removing the functionality of drawing other shapes except Hexagon (just comment those out)
• The application draws a Hexagon whenever the H key is pressed. You may have multiple hexagons at any time.
• When you run this application, the Hexagon is drawn at the key press and then it starts moving after 1 second of its creation in random direction.
o The Hexagon should vanish (or get deleted) when it touches the perimeter of the frame
o The Hexagons of different color should bounce off each other, while interchanging colors. i.e., if a small hexagon of black color bounces off a blue hexagon of larger size, then smaller hexagon will get blue color after bouncing off. After bouncing off, Hexagons should move in opposite direction of the incident angle or direction (just like how light would reflect from the surface).
o ThehexagonsofsameColorwillmergetogetherbycreatinganotherHexagon that has the average size of the larger and smaller hexagons. The direction of the new Hexagon will now be the direction of previously smaller Hexagon.
• ShapeDriver will again need a Timer, and will also need to implement the ActionListener interface
o Add the following to you ShapeDriver class:
import javax.swing.Timer;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class ShapeDriver implements ActionListener { private Timer timer
public ShapeDriver() {
// the second argument to the Timer Constructor takes an ActionListener // the this key word informs the JVM to look inside this class for
// the actionPerformed method that must be overridden when
// ActionListener is implemented
// Every tick of the clock will now run the actionPerformed method
timer = new Timer(1000/60, this);
timer.start();
}
// Method that must be implemented since the class implements ActionListener public void actionPerformed(ActionEvent e) {
// move each Hexagon
// check if Hexagon is in bounds, and delete if it touches the borders
// check if Hexagon hits another Hexagon of different color,
// bounce Hexagons off each other and interchanges colors
// if Hexagon of one color hits a Hexagon of same color, create average Hexagon with same color
// call repaint
this.repaint(); }
}
• Note: The x and y location is actually in the top left hand corner of the Hexagon
o This is how the AWT Graphics draws an Hexagon
o In the Hexagon class, You may creat another Point called center
o When moving the location of the Hexagon You can update the center location
this makes collision detection better than using the x and y location for drawing the Hexagon
o First just work towards using the location Point in the Shape class
Once this is working, add the center Point to your Hexagon class and
update its x and y value the same as you update location Then use center to calculate the distance
o For example, in the Hexagon Class:
I added Point center instance field
I overrode the move() method from the Shape class
I called super.move() inside of move() to do what the method was doing
in the super class, then updated the center field local to the Hexagon
class
o Note: Remember subclasses do not have access to private super class fields and
methods
o Adhere to good Object Oriented Principles:
keep data in classes private, and access with get and set methods
only have methods public if other classes use them. Make them private if
only the class needs it