Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a pentagon and Hexagon Class Using your classes, implement the Random Shape Generator application which runs for 7 seconds and draws one random shape

Add a pentagon and Hexagon Class

Using your classes, implement the Random Shape Generator application which runs for 7 seconds and draws one random shape every 100 milliseconds. No shape can be drawn more than 10 times.

The following code uses a key pressed function but it should be using a timer how would I change it?

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Point;

public abstract class Shape {

private Color fillColor;

private Color borderColor;

private Boolean isFilled;

private Point Location;

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);

}

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

this.fillColor=fillColor;

this.borderColor=Color.BLACK;

this.isFilled=true;

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

}

public Shape(int x, int y){

this.fillColor=Color.WHITE;

this.borderColor=Color.BLACK;

this.isFilled=true;

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

}

public Color getFillColor() {

return fillColor;

}

public void setFillColor(Color fillColor) {

this.fillColor = fillColor;

}

public Color getBorderColor() {

return borderColor;

}

public void setBorderColor(Color borderColor) {

this.borderColor = borderColor;

}

public Boolean IsFilled() {

return isFilled;

}

public void setFilled(Boolean isFilled) {

this.isFilled = isFilled;

}

public Point getLocation() {

return Location;

}

public void setLocation(Point location) {

Location = location;

}

public abstract void draw(Graphics g);

}

// Rectangle.java

import java.awt.Color;

import java.awt.Graphics;

public class Rectangle extends Shape{

private int width;

private int height;

public Rectangle(Color fillColor, Color borderColor, int x, int y, int width, int height) {

super(fillColor, borderColor, x, y);

this.width=width;

this.height=height;

}

public Rectangle(Color fillColor, int x, int y, int width, int height) {

super(fillColor, x, y);

this.width=width;

this.height=height;

}

public Rectangle(int x, int y, int width, int height) {

super(x, y);

this.width=width;

this.height=height;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

@Override

public void draw(Graphics g) {

if(IsFilled()){

g.setColor(getFillColor());

g.fillRect(getLocation().x, getLocation().y, width, height);

g.setColor(getBorderColor());

g.drawRect(getLocation().x, getLocation().y, width, height);

}else{

g.setColor(getBorderColor());

g.drawRect(getLocation().x, getLocation().y, width, height);

}

}

@Override

public String toString() {

return "Rectangle: x: "+getLocation().x+", y: "+getLocation().y+", height: "+height+", width: "+width;

}

@Override

public boolean equals(Object o) {

if(o instanceof Rectangle){

Rectangle r=(Rectangle) o;

if(this.height==r.height && this.width==r.width){

return true;

}

}

return false;

}

}

// Ellipse.java

import java.awt.Color;

import java.awt.Graphics;

public class Ellipse extends Shape{

private int width;

private int height;

public Ellipse(Color fillColor, Color borderColor, int x, int y, int width, int height) {

super(fillColor, borderColor, x, y);

this.width=width;

this.height=height;

}

public Ellipse(Color fillColor, int x, int y, int width, int height) {

super(fillColor, x, y);

this.width=width;

this.height=height;

}

public Ellipse(int x, int y, int width, int height) {

super(x, y);

this.width=width;

this.height=height;

}

@Override

public void draw(Graphics g) {

if(IsFilled()){

g.setColor(getFillColor());

g.fillOval(getLocation().x, getLocation().y, width, height);

g.setColor(getBorderColor());

g.drawOval(getLocation().x, getLocation().y, width, height);

}else{

g.setColor(getBorderColor());

g.drawOval(getLocation().x, getLocation().y, width, height);

}

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

this.height = height;

}

@Override

public String toString() {

return "Ellipse: x: "+getLocation().x+", y: "+getLocation().y+", height: "+height+", width: "+width;

}

@Override

public boolean equals(Object o) {

if(o instanceof Ellipse){

Ellipse r=(Ellipse) o;

if(this.height==r.height && this.width==r.width){

return true;

}

}

return false;

}

}

// Triangle.java

import java.awt.Color;

import java.awt.Graphics;

public class Triangle extends Shape {

private int width;

private int height;

public Triangle(Color fillColor, Color borderColor, int x, int y,

int width, int height) {

super(fillColor, borderColor, x, y);

this.width = width;

this.height = height;

}

public Triangle(Color fillColor, int x, int y, int width, int height) {

super(fillColor, x, y);

this.width = width;

this.height = height;

}

public Triangle(int x, int y, int width, int height) {

super(x, y);

this.width = width;

this.height = height;

}

@Override

public void draw(Graphics g) {

if(IsFilled()){

g.setColor(getFillColor());

g.fillPolygon(new int[] { 0, width / 2, width }, new int[] { height, 0,

height }, 3);

g.setColor(getBorderColor());

g.drawPolygon(new int[] { 0, width / 2, width }, new int[] { height, 0,

height }, 3);

}else{

g.setColor(getBorderColor());

g.drawPolygon(new int[] { 0, width / 2, width }, new int[] { height, 0,

height }, 3);

}

}

@Override

public String toString() {

return "Triangle: x: "+getLocation().x+", y: "+getLocation().y+", height: "+height+", base width: "+width;

}

@Override

public boolean equals(Object o) {

if(o instanceof Triangle){

Triangle r=(Triangle) o;

if(this.height==r.height && this.width==r.width){

return true;

}

}

return false;

}

}

// Square.java

import java.awt.Color;

public class Square extends Rectangle {

public Square(Color fillColor, Color borderColor, int x, int y, int size) {

super(fillColor, borderColor, x, y, size, size);

}

public Square(Color fillColor, int x, int y, int size) {

super(fillColor, x, y, size, size);

}

public Square(int x, int y, int size) {

super(x, y, size, size);

}

@Override

public String toString() {

return "Square: x: " + getLocation().x + ", y: " + getLocation().y

+ ", side width: " + getHeight();

}

@Override

public boolean equals(Object o) {

if (o instanceof Square) {

Square r = (Square) o;

if (this.getWidth() == r.getWidth()) {

return true;

}

}

return false;

}

}

// Circle.java

import java.awt.Color;

public class Circle extends Ellipse {

public Circle(Color fillColor, Color borderColor, int x, int y, int diameter) {

super(fillColor, borderColor, x, y, diameter, diameter);

}

public Circle(Color fillColor, int x, int y, int diameter) {

super(fillColor, x, y, diameter, diameter);

}

public Circle(int x, int y, int diameter) {

super(x, y, diameter, diameter);

}

@Override

public String toString() {

return "Circle: x: " + getLocation().x + ", y: " + getLocation().y

+ ", diameter: " + getHeight();

}

@Override

public boolean equals(Object o) {

if (o instanceof Circle) {

Circle r = (Circle) o;

if (this.getWidth() == r.getWidth()) {

return true;

}

}

return false;

}

}

// RandomShapeGenerator.java

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.ArrayList;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class RandomShapeGenerator extends JPanel implements KeyListener {

ArrayList shapesList;

Random random;

public RandomShapeGenerator() {

shapesList = new ArrayList();

random = new Random();

setPreferredSize(new Dimension(500, 500));

setBackground(Color.GRAY);

}

public void generateRandomShape(char c) {

Shape s;

switch (c) {

case 'c':

s = new Circle(random.nextInt(getWidth()),

random.nextInt(getHeight()), random.nextInt(100));

s.setFillColor(new Color(random.nextInt(255), random.nextInt(255),

random.nextInt(255)));

s.setBorderColor(new Color(random.nextInt(255),

random.nextInt(255), random.nextInt(255)));

shapesList.add(s);

repaint();

break;

case 'e':

s = new Ellipse(random.nextInt(getWidth()),

random.nextInt(getHeight()), random.nextInt(100),

random.nextInt(100));

s.setFillColor(new Color(random.nextInt(255), random.nextInt(255),

random.nextInt(255)));

s.setBorderColor(new Color(random.nextInt(255),

random.nextInt(255), random.nextInt(255)));

shapesList.add(s);

repaint();

break;

case 'r':

s = new Rectangle(random.nextInt(getWidth()),

random.nextInt(getHeight()), random.nextInt(100),

random.nextInt(100));

s.setFillColor(new Color(random.nextInt(255), random.nextInt(255),

random.nextInt(255)));

s.setBorderColor(new Color(random.nextInt(255),

random.nextInt(255), random.nextInt(255)));

shapesList.add(s);

repaint();

break;

case 's':

s = new Square(random.nextInt(getWidth()),

random.nextInt(getHeight()), random.nextInt(100));

s.setFillColor(new Color(random.nextInt(255), random.nextInt(255),

random.nextInt(255)));

s.setBorderColor(new Color(random.nextInt(255),

random.nextInt(255), random.nextInt(255)));

shapesList.add(s);

repaint();

break;

case 't':

s = new Triangle(random.nextInt(getWidth()),

random.nextInt(getHeight()), random.nextInt(100),

random.nextInt(100));

s.setFillColor(new Color(random.nextInt(255), random.nextInt(255),

random.nextInt(255)));

s.setBorderColor(new Color(random.nextInt(255),

random.nextInt(255), random.nextInt(255)));

shapesList.add(s);

repaint();

break;

default:

break;

}

}

@Override

public void paint(Graphics g) {

super.paint(g);

for (Shape s : shapesList) {

s.draw(g);

}

}

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

RandomShapeGenerator generator = new RandomShapeGenerator();

frame.add(generator);

frame.pack();

frame.addKeyListener(generator);

}

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

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

=+g) What would the residual of the team in part f be? h) The R2

Answered: 1 week ago

Question

=+1. What is a stakeholder? Define the term in your own words.

Answered: 1 week ago