Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions: this is for my Java class. I already have written the Shape class, and have written a subclass Hexagon that extends Shape. I need

Instructions:

this is for my Java class.

I already have written the Shape class, and have written a subclass Hexagon that extends Shape. I need help with creating the below bouncing Hexagon Game. This is what I currently have in my draw method inside my Hexagon subclass:

public void draw(Graphics g) {

int[] xPoints = new int[] { this.getLocation().x, this.getLocation().x + width / 2, this.getLocation().x + width};

int[] yPoints = new int[] { this.getLocation().y, this.getLocation().y + height, this.getLocation().y};

g.setColor(getFillColor());

g.fillPolygon(xPoints, yPoints, 6);

g.setColor(getBorderColor());

g.drawPolygon(xPoints, yPoints, 6);

}

For your reference, this is the code I currently have have for my Shape class:

public abstract class Shape {

private Color fillColor;

private Color borderColor;

private Boolean isFilled;

private Point location;

public abstract void draw(Graphics g);

// the three constructors initialize the instance fields

public Shape(Color fillColor, Color borderColor, int x, int y) {

this.fillColor = fillColor;

this.borderColor = borderColor;

this.isFilled = true;

this.location = new Point(x, y);

}

// set borderColor to Black since not provided

public Shape(Color fillColor, int x, int y) {

this.fillColor=fillColor;

this.borderColor=Color.BLACK;

this.isFilled=true;

this.location=new Point(x, y);

}

// set fillColor to white and border color to black

public Shape(int x, int y) {

this.fillColor=Color.WHITE;

this.borderColor=Color.BLACK;

this.isFilled=true;

this.location=new Point(x, y);

}

// will fill the shape with some random image. You can select any image for larger shapes

public void setFillColor(Color c) {

this.fillColor = fillColor;

}

public Color getFillColor() {

return fillColor;

}

public void setBorderColor(Color c) {

this.borderColor = borderColor;

}

public Color getBorderColor() {

return borderColor;

}

public void setLocation(Point pt) {

this.isFilled = isFilled;

}

public Point getLocation() {

return location;

}

// can use to get the x and y values from the private Point instance field. import random, and make height and width the bounds. so get a random shape

public int getX() {

return location.x;

}

public void setX(int x) {

}

public int getY() {

return location.y;

}

public void setY(int y) {

}

// if fillColor is white returns true, else returns false

public boolean isFilled() {

return isFilled;

}

// moves location by dx and dy

private void moveLocation(int dx, int dy) {

}

abstract double getArea();

abstract double getPerimeter();

}

I have imported random into my ShapeDriver, and am generating random colored hexagons. Can you help me with the below bouncing hexagon game instructions?

Bouncing Hexagon Game

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 The hexagons of same Color will merge together by creating another Hexagon 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 create 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions