133 lines
11 KiB
HTML
Raw Normal View History

2023-09-01 00:14:38 -05:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PieChart.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.number {color: #6897bb}
.string {color: #6a8759}
.ST1 {color: #ffc66d}
.ST4 {color: #8a653b}
.comment {color: #808080}
.whitespace {color: #505050}
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP1_FX_CalebFontenot/src/mp1_fx_calebfontenot/PieChart.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> mp1_fx_calebfontenot;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> javafx.application.Application;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">geometry</span><span class="comment">.</span><span class="comment">Pos</span><span class="comment">;</span>
<span class="literal">import</span> javafx.scene.Group;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.layout.Pane;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">layout</span><span class="comment">.</span><span class="comment">StackPane</span><span class="comment">;</span>
<span class="literal">import</span> javafx.scene.paint.Color;
<span class="literal">import</span> javafx.scene.shape.Arc;
<span class="literal">import</span> javafx.scene.shape.ArcType;
<span class="literal">import</span> javafx.scene.text.Text;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> PieChart <span class="literal">extends</span> Application {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST1">start</span>(Stage primaryStage) <span class="literal">throws</span> Exception {
<span class="comment">// Create pane</span>
Pane pane = <span class="literal">new</span> Pane();
ArrayList&lt;Arc&gt; pie = <span class="literal">new</span> ArrayList&lt;Arc&gt;();
ArrayList&lt;Text&gt; pieDesc = <span class="literal">new</span> ArrayList&lt;Text&gt;();
ArrayList&lt;Group&gt; layoutSlice = <span class="literal">new</span> ArrayList&lt;Group&gt;();
<span class="literal">float</span>[] pieValues = {<span class="number">20.0f</span>, <span class="number">10.0f</span>, <span class="number">20.0f</span>, <span class="number">40.0f</span>, <span class="number">30.0f</span>}; <span class="comment">// First value controls the initial angle</span>
String[] textValues = { <span class="string">&quot;</span><span class="string">Quiz</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Project</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Final</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Midterm</span><span class="string">&quot;</span>};
<span class="literal">float</span>[][] textPos = { {<span class="number">165</span>, <span class="number">50</span>}, {<span class="number">70</span>, <span class="number">120</span>}, {<span class="number">150</span>, <span class="number">300</span>}, {<span class="number">250</span>, <span class="number">204</span>} }; <span class="comment">// I really didn&#39;t want to hard code the coords for the text, but JavaFX forced my hand</span>
<span class="literal">int</span> j = <span class="number">1</span>;
<span class="literal">float</span> startAngle = <span class="number">0.0f</span>;
<span class="literal">final</span> <span class="literal">float</span> PIE_SIZE = <span class="number">200.0f</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) {
<span class="comment">// Arc</span>
startAngle = startAngle + <span class="ST2">toDegrees</span>(pieValues[i]);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">i:</span><span class="string">&quot;</span> + <span class="ST2">toDegrees</span>(pieValues[i]) + <span class="string">&quot;</span><span class="string"> j: </span><span class="string">&quot;</span> + <span class="ST2">toDegrees</span>(pieValues[j]));
Arc pieSlice = <span class="literal">new</span> Arc();
pieSlice.setCenterX(PIE_SIZE);
pieSlice.setCenterY(PIE_SIZE);
pieSlice.setRadiusX(PIE_SIZE);
pieSlice.setRadiusY(PIE_SIZE);
pieSlice.setStartAngle(startAngle); <span class="comment">//toDegrees(pieValues[i])</span>
pieSlice.setLength(<span class="ST2">t</span><span class="ST2">oDegrees</span>(pieValues[j]));
pieSlice.setType(ArcType.<span class="ST3">ROUND</span>);
<span class="comment">// Text</span>
String descriptionString = textValues[i] + <span class="string">&quot;</span><span class="string">--</span><span class="string">&quot;</span> + pieValues[j] + <span class="string">&quot;</span><span class="string">%</span><span class="string">&quot;</span>;
Text descText = <span class="literal">new</span> Text(textPos[i][<span class="number">0</span>], textPos[i][<span class="number">1</span>], descriptionString);
<span class="comment">// Add Arcs and text together into a Group</span>
Group pieSliceStack = <span class="literal">new</span> Group();
pieSliceStack.getChildren().addAll(pieSlice, descText);
<span class="comment">// Add Arc and text to respective ArrayLists</span>
pie.add(pieSlice);
pieDesc.add(descText);
layoutSlice.add(pieSliceStack);
j++;
}
pie.get(<span class="number">0</span>).setFill(Color.<span class="ST3">BLUE</span>);
pie.get(<span class="number">1</span>).setFill(Color.<span class="ST3">RED</span>);
pie.get(<span class="number">2</span>).setFill(Color.<span class="ST3">YELLOW</span>);
pie.get(<span class="number">3</span>).setFill(Color.<span class="ST3">GREEN</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">3</span>; i &gt;= <span class="number">0</span>; i--) {
pane.getChildren().add(layoutSlice.get(i));
}
Scene scene = <span class="literal">new</span> Scene(pane);
primaryStage.setTitle(<span class="string">&quot;</span><span class="string">Pie Chart</span><span class="string">&quot;</span>);
primaryStage.setScene(scene);
primaryStage.show();
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">This</span> <span class="comment">function</span> <span class="comment">converts</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">between</span><span class="comment"> 0 </span><span class="comment">and</span><span class="comment"> 100 </span><span class="comment">to</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">between</span><span class="comment"> 0 </span><span class="comment">and</span><span class="comment"> 360, </span><span class="comment">while</span> <span class="comment">retaining</span> <span class="comment">the</span> <span class="comment">ratio</span> <span class="comment">between</span> <span class="comment">those</span> <span class="comment">two</span> <span class="comment">number</span> <span class="comment">values</span><span class="comment">.</span> <span class="comment">So</span> <span class="comment">for</span> <span class="comment">example</span><span class="comment">, 35 </span><span class="comment">turns</span> <span class="comment">into</span><span class="comment"> 90</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">oldValue</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">float</span> <span class="ST5">toDegrees</span>(<span class="literal">float</span> oldValue) {
<span class="literal">final</span> <span class="literal">float</span> oldMin = <span class="number">0</span>;
<span class="literal">final</span> <span class="literal">float</span> oldMax = <span class="number">100</span>;
<span class="literal">final</span> <span class="literal">float</span> newMin = <span class="number">0</span>;
<span class="literal">final</span> <span class="literal">float</span> newMax = <span class="number">360</span>;
<span class="literal">float</span> newValue = ((oldValue - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin;
<span class="literal">return</span> Math.<span class="ST2">round</span>(newValue);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST2">launch</span>(args);
}
}
</pre></body>
</html>