Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this assignment is to use JavaFX to create a pie chart. The pie c

ID: 3708253 • Letter: T

Question

The purpose of this assignment is to use JavaFX to create a pie chart. The pie chart will track the percentage of students at a fictional college who are freshman, sophomore, junior, senior, or graduate students. Don't use the PieChart class from the javafx package. You must create your own original pie chart. A sample pie chart is displayed below.

Create a project named StudentAnalysis. Use a package name of edu.arizonastate.studentanalysis. (You can substitute your domain name for edu.arizonastate if you wish.) Make sure you create a regular Java project in Eclipse not a JavaFX project.

Create an application class that extends the javafx.application.Application class. This will be the class that creates the JavaFX application. Name this class StudentAnalysis.

Create the application stage as shown in the example above.

Create a "title" for the pie chart as shown in the example above. This should read "Percent of Students by Class".

The pie chart itself needs 5 "slices" for the percentage of students that are freshman, sophomore, junior, senior, and graduate students. The actual number of students in each category is hard coded as a constant. Create a separate constant for the number of freshman, sophomore, junior, senior, and graduate students. Make sure to use a constant for the values and also use the constant in all calculations. You can choose any numbers you wish for the values. Your application should work fine and present a different pie chart when I change the numbers in the constants. The chart above was created using the following numbers: freshman - 100, sophomore - 200, junior - 50, senior - 75, and graduate students - 125. Each slice must be a different color. You can use any colors you wish. Position the chart in the middle of the application window. You can do this by hard coding the x and y coordinates. You do not need to use binding properties. I will not resize your window.

The lower section of the application shows the legend for the pie chart. This indicates which color corresponds with which student class. This color must match the color of the matching slice in the pie chart. Also, display the percentage of students in each class as shown in the example above. Use the NumberFormat class and the getPercentInstance method to format the percentage.

You must create and use at least one Font object with a specific font name and point size.

3Student Class Analysis Percent of Students by Clas 18%-Freshman 36%-Sophmore 9%Junior 14%Senior 2396-Graduate

Explanation / Answer

package edu.arizonastate.studentanalysis;

import javafx.application.*;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.ArcTo;

import javafx.scene.shape.FillRule;

import javafx.scene.shape.HLineTo;

import javafx.scene.shape.LineTo;

import javafx.scene.shape.MoveTo;

import javafx.scene.shape.Path;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class ApplicationTest extends Application {

int freshman = 18, sophomore = 36, junior = 9, senior = 14, graduate = 23;

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage primaryStage) throws Exception {

Group root = new Group();

BorderPane border = new BorderPane();

border.setTop(addTop());

border.setCenter(addCenter());

border.setBottom(addBottom());

root.getChildren().add(border);

Scene scene = new Scene(root, 350, 350);

primaryStage.setTitle("Percent of students by class");

primaryStage.setScene(scene);

primaryStage.show();

}

public HBox addTop() {

HBox hbox = new HBox();

hbox.setPadding(new Insets(15, 12, 15, 12));

hbox.setAlignment(Pos.CENTER);

hbox.setSpacing(10);

hbox.setStyle("-fx-background-color: #DDD;");

Text title = new Text();

title.setText("Percent of students by class");

title.setStyle("-fx-font: 20 arial;");

title.setX(250);

hbox.getChildren().addAll(title);

return hbox;

}

public HBox addBottom() {

String freshmanLegend = freshman + "% freshman";

String sophomoreLegend = sophomore + "% sophomore";

String juniorLegend = junior + "% junior";

String seniorLegend = senior + "% senior";

String graduateLegend = graduate + "% graduate";

Label freshmanText = new Label(freshmanLegend);

freshmanText.setStyle("-fx-font: 10 arial;");

freshmanText.setTextFill(Color.RED);

Label sophomoreText = new Label(sophomoreLegend);

sophomoreText.setStyle("-fx-font: 10 arial;");

sophomoreText.setTextFill(Color.GREEN);

Label juniorText = new Label(juniorLegend);

juniorText.setStyle("-fx-font: 10 arial;");

juniorText.setTextFill(Color.BLUE);

Label seniorText = new Label(seniorLegend);

seniorText.setStyle("-fx-font: 10 arial;");

seniorText.setTextFill(Color.ORANGE);

Label graduateText = new Label(graduateLegend);

graduateText.setStyle("-fx-font: 10 arial;");

graduateText.setTextFill(Color.BLACK);

HBox hbox = new HBox();

hbox.setPadding(new Insets(15, 12, 15, 12));

hbox.setSpacing(10);

hbox.setStyle("-fx-background-color: #DDD;");

hbox.getChildren().addAll(freshmanText, sophomoreText, juniorText, seniorText, graduateText);

return hbox;

}

public Group addCenter() {

Group group = new Group();

group.setStyle("-fx-padding: 10 10 10 10;");

double center = 0, radius = 130, innerRadius = 0, total = 0;

Path red = drawSemiRing(center, center, radius, total, freshman, Color.RED, Color.RED); total += freshman;

Path green = drawSemiRing(center, center, radius, total, sophomore, Color.GREEN, Color.GREEN); total += sophomore;

Path blue = drawSemiRing(center, center, radius, total, junior, Color.BLUE, Color.BLUE); total += junior;

Path orange = drawSemiRing(center, center, radius, total, senior, Color.ORANGE, Color.ORANGE); total += senior;

Path black = drawSemiRing(center, center, radius, total, graduate, Color.BLACK, Color.BLACK); total += graduate;

group.getChildren().addAll(red, green, blue, orange, black);

return group;

}

private Path drawSemiRing(double centerX, double centerY, double radius, double start, double angularDistance, Color bgColor, Color strkColor) {

double innerRadius = 0;

Path path = new Path();

path.setFill(bgColor);

path.setStroke(strkColor);

path.setFillRule(FillRule.EVEN_ODD);

  

start = start * 360/100;

angularDistance = angularDistance * 360/100;

  

  

double angStart = 2 * Math.PI * start / 360, angEnd = 2 * Math.PI * (start + angularDistance) / 360;

  

double xStart = centerX + radius * Math.cos(angStart), yStart = centerY + radius * Math.sin(angStart),

xEnd = centerX + radius * Math.cos(angEnd), yEnd = centerY + radius * Math.sin(angEnd);

  

MoveTo moveTo = new MoveTo();

moveTo.setX(centerX);

moveTo.setY(centerY);

  

LineTo lineTo = new LineTo();

lineTo.setX(xStart);

lineTo.setY(yStart);

ArcTo arcTo = new ArcTo();

arcTo.setSweepFlag(true);

arcTo.setX(xEnd);

arcTo.setY(yEnd);

arcTo.setRadiusX(radius);

arcTo.setRadiusY(radius);

  

LineTo lineTo2 = new LineTo();

lineTo2.setX(centerX);

lineTo2.setY(centerY);

  

path.getElements().add(moveTo);

path.getElements().add(lineTo);

path.getElements().add(arcTo);

path.getElements().add(lineTo2);

return path;

}

}