Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

fill in the blanks of java program import java.awt.Color; import java.awt.Graphics; class Disk extends Point { protected int radius; public Vect velocity; public Color borderColor;

fill in the blanks of java program

import java.awt.Color;

import java.awt.Graphics;

class Disk extends Point {

protected int radius;

public Vect velocity;

public Color borderColor;

public boolean anchored = false; // default value

public Disk(int x, int y, int radius, Color color ) {

// you write: call constructor with (int, int, int, Color, Color). The last two params are just color

this(x,y,color)

}

public Disk(int radius, Color color) {

// you write: call the default Point constructor

// you write: call init

Super();

}

Disk(int x, int y, int radius, Vect velocity, Color c) {

// you write: call the four parameter constructor.

// you write: set the velocity

}

public Disk(int x, int y, int radius, Color color, Color borderColor) {

// you write: call the Point 2-parameter constructor

// you write: call init

}

private void init(int radius, Color color, Color borderColor) {

// you write: initialize values, set velocity to zero.

}

void copyDisk(Disk d) {

// you write: set all values of this to ds values.

}

public Disk(Disk[] otherDisks) // create a random anchored disk, avoiding overlaps with other disks.

{

// Nothing to do here but understand how it works.

boolean collision;

Disk temp;

anchored = true;

int tries, maxTries = 10000, minRadius = 40;

do {

tries = 0;

do {

temp = new Disk(

Util.getRandom(minX, maxX),

Util.getRandom(minY,maxY),

Util.getRandom(minRadius, minRadius + (maxX - minX)/9),

Util.getDarkColor() ,

Color.LIGHT_GRAY);

temp.anchored = true;

collision = false;

for (int j = 0; j < otherDisks.length; j++) {

Disk other = otherDisks[j];

if (other != null && other.collision(temp))

collision = true;

}

tries++;

} while (collision && tries < maxTries);

minRadius--;

//if (minRadius < 40) System.out.println(minRadius);

} while (collision && minRadius >= 1);

copyDisk(temp);

}

public boolean collision(Disk d) {

double r = getDistance(d);

return r <= radius + d.radius && d!= this;

}

public void draw(Graphics g, Color c, Color border) {

// everything in here is written for you but you should understand how it works.

Color temp = g.getColor(); // save color

g.setColor(c); // temporarily change color

int d = Util.round(2*radius);

g.fillOval(Util.round(x-radius), Util.round(y-radius), d,d);

if (!this.color.equals(this.borderColor)) {

g.setColor(border);

g.drawOval(Util.round(x-radius), Util.round(y-radius), d,d);

}

g.setColor(temp); // restore color

}

public void draw(Graphics g) {

// you write: call the draw above with color and borderColor.

g.setColor(Color.blue)

}

public void setRadius(int radius) {

// you write: set the radius

radius = radius;

}

public double getArea() {

// you write: return the area

}

public void erase(Graphics g) {

draw(g,background, background);

}

public void move() {

// you write: update velocity if disk boundary crosses border and is going it that direction

move(velocity); // calls Point move

}

public void move(Disk [] disks) {

if (anchored) return;

for (int i = 0; i < disks.length; i++)

if (disks[i] != null)

handleCollision(disks[i]);

double speed = this.velocity.getLength();

if (speed > 6) this.velocity.setLength(speed * 0.99); // some friction for super fast speeds

move();

}

public void elasticCollision(Disk d) {

// you write this (about 15 to 20 lines of code)

}

public void handleCollision(Disk d) {

if (collision(d))

if (!d.anchored) elasticCollision(d);

else {

// you write: bounce of anchored disk.

}

}

public String toString() {

// you write: call toString of Point then append velocity in square brackets e.g. (44,55) [0.2,3.22]

// remember, there is already a Velocity.toString().

}

}

_______________________________point___________________________

import java.awt.Color;

import java.awt.Graphics;

public class Point {

protected static int maxX = 400, maxY = 400, minX = 0, minY = 0; //default values

protected double x,y;

public static Color background;

public Color color;

public Point() {

this(Util.getRandom(minX, maxX), Util.getRandom(minX, maxX)); // random point

}

public Point(double x, double y) {

setX(x);

setY(y);

}

public double getX() {return x;}

public double getY() {return y;}

public void setX(double x) {

this.x = Math.min(maxX, Math.max(minX, x));

}

public void setY(double y) {

this.y = Math.min(maxY, Math.max(minY, y));

}

static public void setMaxX(int maxX) {

if (maxX < minX) maxX = minX;

Point.maxX = maxX;

}

static public void setMaxY(int maxY) {

if (maxY < minY) maxY = minY;

Point.maxY = maxY;

}

static public void setMinX(int minX) {

if (minX > maxX) minX = maxX;

Point.minX = minX;

}

static public void setMinY(int minY) {

if (minY > maxY) minY = maxY;

Point.minY = minY;

}

public static int getMaxX() {return maxX;}

public static int getMaxY() {return maxY;}

public static int getMinX() {return minX;}

public static int getMinY() {return minY;}

public int roundX() {return Util.round(x);}

public int roundY() {return Util.round(y);}

public String toString() {return "(" + Util.round(x,2) + "," + Util.round(y ,2)+ ")";}

public void draw(Graphics g) {

Graphics temp = g;

g.fillOval(roundX()-1,roundY()-1,2,2);

}

public void move(Vect a) {

setX(x + a.vx);

setY(y + a.vy);

}

public boolean equals(Object o) {

if (o == null || o.getClass() != this.getClass())

return false;

Point temp = (Point) o;

return temp.x == x && temp.y == y;

}

public double getDistance(Point p) {

double dx = p.x - x, dy = p.y - y;

return Math.sqrt(dx*dx + dy*dy);

}

}

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

More Books

Students also viewed these Databases questions

Question

=+1. Which of the following is true of boys compared with girls?

Answered: 1 week ago

Question

using signal flow graph

Answered: 1 week ago