Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code level: beginner write a method Triangle Implement the draw() method. public void draw(Group group, Color c, boolean fill) Create a Polygon whose three

java code

level: beginner

write a method

Triangle

Implement the draw() method.

public void draw(Group group, Color c, boolean fill)

  • Create a Polygon whose three vertices are at each point of the calling Triangle object. All you need to do is call the constructor, change its color, and add the Polygon object to the group.
  • The parameters for our Triangle.draw() is different from the parameters of the Polygon object. You need to convert our information (coordinates of points) into information the constructor expects (array of doubles corresponding to the x and y values of each point in order). For example, for a Triangle with points (0, 1), (2, 3), and (4, 5), the array would be in the order of 0, 1, 2, 3, 4, 5.
  • Make sure that the fill parameter will cause the function to choose the appropriate JavaFX class to instantiate. If fill is false, make the fill of the shape empty by passing in null to the appropriate method and set the stroke outline to the color passed in.
public void drawTriforce(Group group, boolean fill, int n)

The triforce is simply a triangle that contains an inner triangle whose vertices are at the midpoint of the sides belonging to the outer triangle.

Inner Triangles Three Points

The color of the triangles will be random, and the parameter n tells how many triangles we want to draw for this particular instance of the fancy shape.

Recall that Triangles are made of three Points, based on our class definition of a Triangle. Find the three midpoints between the outer triangles three Points. These three midpoints will become the Points of the inner Triangle. You may want to use a private helper method to find these midpoints.

Terminating Case (Base case)

If n==0, return without drawing any Triangles.

Recursion

As usual, there are only three parameters: the group, the fill, and n (a non-negative input that tells how many Triangles to draw; you may assume this will never be a negative number). The color of each triangle will be random - generate a new random color (see Shape class for a hint) and make a call to draw().

For reference, if n==1, draw a single triangle.

In Triangle Class

image text in transcribed

image text in transcribed

image text in transcribed

import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape. import java.util.*; t* a class specifying a Triangle can be drawn into a javafx.scene.Group public class Triangle extends Shape private Point point1; private Point point2; private Point point3; public Point getP1() return this.point1; public Point getP2() return this.point2; public Point getP3() return this.point3; private void setP1( Point point1) this.point1 -point1; private void setP2( Point point2) this.point2 -point2; private void setP3( Point point3 this.point3 point3; public Triangle( Point pointi, Point point2, Point point3) super "NoName" ); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle( Point point1, Point point2, Point point3, string name) super( name); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle(Triangle tri) ( super "NoName" ); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle) ( this(new Point(0,0), new Point (0,0), new Point (0,0), "NoName"); override public string tostring() f string.format("Triangle: Point : getshapeName(), point1.tostring(), point2.tostring(), point3.tostring)); return %s; p1: Point : %s; p2: %s; p3: Point : %s", @Override public void draw(Group group, Color c, boolean fill) ( group.getchildren().add(polygon); public void drawTriforce(Group group, boolean fill, int n) Color c getRandomcolor(); import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape. import java.util.*; t* a class specifying a Triangle can be drawn into a javafx.scene.Group public class Triangle extends Shape private Point point1; private Point point2; private Point point3; public Point getP1() return this.point1; public Point getP2() return this.point2; public Point getP3() return this.point3; private void setP1( Point point1) this.point1 -point1; private void setP2( Point point2) this.point2 -point2; private void setP3( Point point3 this.point3 point3; public Triangle( Point pointi, Point point2, Point point3) super "NoName" ); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle( Point point1, Point point2, Point point3, string name) super( name); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle(Triangle tri) ( super "NoName" ); this.setP1( new Point( pointi) ); this.setP2( new Point( point2) ); this.setP3( new Point( point3) ); public Triangle) ( this(new Point(0,0), new Point (0,0), new Point (0,0), "NoName"); override public string tostring() f string.format("Triangle: Point : getshapeName(), point1.tostring(), point2.tostring(), point3.tostring)); return %s; p1: Point : %s; p2: %s; p3: Point : %s", @Override public void draw(Group group, Color c, boolean fill) ( group.getchildren().add(polygon); public void drawTriforce(Group group, boolean fill, int n) Color c getRandomcolor()

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