go to previous page   go to home page   go to next page

Answer:

Yes. Groups are used to make a composite object out of several objects.


Three Triangles in a Group

One Triangle, made of Three Smaller

The above image is three small triangles arranged to make a big triangle (with its center cut out.)

Here is the program that draws that figure. The constructor for TriangleThree expects the (X, Y) coordinates of the three vertices of the big triangle.

It then calculates the (X, Y) coordinates of the midpoints of the three sides as labeled in the figure.


import javafx.stage.*;        
import javafx.scene.*;  
import javafx.scene.shape.*; 
import javafx.scene.paint.*;  

class TriangleThree extends Group
{
  public TriangleThree( Color color, double x0, double y0, double x1, double y1, double x2, double y2 )
  {        
    // Calculate midpoints of the three sides
    
    double xL = ; // Left edge X    
    double yL = ; // Left edge Y    
    double xR = ; // Right edge X    
    double yR = ; // Right edge Y    
    double xB = ; // Bottom edge X    
    double yB = ; // Bottom edge Y    

    Polygon triL = new Polygon( x0, y0, xL, yL, xB, yB );  
    Polygon triR = new Polygon( xB, yB, xR, yR, x2, y2 );  
    Polygon triT = new Polygon( xL, yL, x1, y1, xR, yR );  

    triL.setFill( color );
    triL.setStroke( Color.BLACK );

    triR.setFill( color );
    triR.setStroke( Color.BLACK );

    triT.setFill( color );
    triT.setStroke( Color.BLACK );
    
    getChildren().addAll( triL, triR, triT );
  }
}
  
public class DrawThreeTriangles extends Application
{ 
  
  public void start(Stage stage) 
  { 
    double sceneWidth=400, sceneHeight=400 ;  
  
    double x0=0, y0=sceneHeight, x1=sceneWidth/2, y1=0, x2= sceneWidth, y2=sceneHeight;
    TriangleThree solid = new TriangleThree(  Color.BLUE, x0, y0, x1, y1, x2, y2 );
    
    Scene scene = new Scene( solid, sceneWidth, sceneHeight, Color.SNOW ); 
    stage.setTitle("Three Triangles"); 
    stage.setScene(scene); 
    stage.show(); 
  }      

}

QUESTION 4:

But someone forgot to do the calculations for the midpoints. Luckily, you are here to fill them in.


go to previous page   go to home page   go to next page