-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKiloConverter.java
More file actions
251 lines (200 loc) · 7.29 KB
/
KiloConverter.java
File metadata and controls
251 lines (200 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Kilometer Converter application
* Harrison Holsinger
* 3/15/2021
*/
public class KiloConverter extends Application
{
// Fields
private TextField kiloTextField;
private TextField kiloTextField1;
private Label resultLabel;
private Label resultLabel1;
public static void main(String[] args)
{
// Launch the application.
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// Labels
// SCENE 1
Label Title1 = new Label("CHOOSE A TYPE OF CONVERSION");
Label promptLabel4 = new Label("Two Types of Conversions!");
Label KilotoMilesLabel = new Label("Click for Kilometer to miles conversion:");
Label MetertoYardsLabel = new Label("Click for Meters to yards conversion:");
Label ExitLabel = new Label("Exit the program:");
// SCENE 2
Label promptLabel = new Label("Enter a distance in kilometers:");
Label promptLabel2 = new Label("(Kiolmeters to miles)");
Label ExitL = new Label("Done with the Program:");
Label BackLabel = new Label("Go back to your options:");
// SCENE 3
Label promptLabel1 = new Label("Enter a distance in meters:");
Label promptLabel3 = new Label("(Meters to yards)");
Label GoBackLabel = new Label("Go back to your options:");
Label Exit = new Label("Done with the Program:");
// Buttons
// SCENE 1
Button kiloToMiles = new Button("Click for kilometers to miles");
Button metersToYards = new Button("Click for meters to yards");
Button ExitButton = new Button("Exit");
// SCENE 2
Button GoBackButton = new Button("GO BACK");
Button QuitButton = new Button("EXIT");
Button calcButton = new Button("Convert");
// SCENE 3
Button BackButton = new Button("GO BACK");
Button LeaveButton = new Button("EXIT");
Button calcButton1 = new Button("Convert");
// TextField
kiloTextField = new TextField();
kiloTextField1 = new TextField();
// ResultLabel
resultLabel1 = new Label();
resultLabel = new Label();
// SCENE 1
// Hbox and Vbox
HBox title = new HBox(15, Title1);
HBox Option = new HBox(15, promptLabel4);
HBox Button = new HBox(20, KilotoMilesLabel, kiloToMiles, MetertoYardsLabel, metersToYards);
VBox vBox1 = new VBox(20, title, Option, Button, ExitLabel, ExitButton);
// Adjustments
title.setAlignment(Pos.CENTER);
vBox1.setAlignment(Pos.CENTER);
Button.setAlignment(Pos.CENTER);
Option.setAlignment(Pos.CENTER);
vBox1.setPadding(new Insets(15));
Button.setPadding(new Insets(10));
Option.setPadding(new Insets(10));
title.setPadding(new Insets(10));
// SCENE 2
// Gridpane
GridPane gridpane1 = new GridPane();
gridpane1.setHgap(10);
gridpane1.setVgap(10);
gridpane1.add(promptLabel, 0, 1);
gridpane1.add(kiloTextField, 0, 2);
gridpane1.add(calcButton, 0, 3);
gridpane1.add(resultLabel, 0, 4);
gridpane1.add(promptLabel2, 0, 0);
gridpane1.setAlignment(Pos.CENTER);
gridpane1.setPadding(new Insets(10));
// Vbox
VBox result = new VBox(15, gridpane1, calcButton, resultLabel, BackLabel, GoBackButton, ExitL, QuitButton);
// Set the result VBox's alignment to center.
result.setAlignment(Pos.CENTER);
// Set the result VBox's padding to 10 pixels.
result.setPadding(new Insets(10));
// SCENE 3
// Gridpane
GridPane gridpane2 = new GridPane();
gridpane2.setHgap(10);
gridpane2.setVgap(10);
gridpane2.add(promptLabel1, 1, 1);
gridpane2.add(kiloTextField1, 1, 2);
gridpane2.add(calcButton1, 1, 3);
gridpane2.add(resultLabel1, 1, 4);
gridpane2.add(promptLabel3, 1, 0);
gridpane2.setAlignment(Pos.CENTER);
gridpane2.setPadding(new Insets(10));
// Vbox
VBox result2 = new VBox(15, gridpane2, calcButton1, resultLabel1, GoBackLabel, BackButton, Exit, LeaveButton);
result2.setAlignment(Pos.CENTER);
result2.setPadding(new Insets(10));
// Scene
Scene scene1 = new Scene(vBox1, 1000, 600);
Scene scene2 = new Scene(result, 1000, 600);
Scene scene3 = new Scene(result2, 1000, 600);
// Event Handlers
// Scene 1
kiloToMiles.setOnAction(e -> primaryStage.setScene(scene2));
metersToYards.setOnAction(e -> primaryStage.setScene(scene3));
ExitButton.setOnAction(new ButtonClickHandlerExit());
// Scene 2
calcButton.setOnAction(new CalcButtonHandler());
GoBackButton.setOnAction(e -> primaryStage.setScene(scene1));
QuitButton.setOnAction(new QuitButtonHandler());
// Scene 3
calcButton1.setOnAction(new CalcButtonHandler1());
BackButton.setOnAction(e -> primaryStage.setScene(scene1));
LeaveButton.setOnAction(new LeaveButtonHandler());
// Add the Scene to the Stage.
primaryStage.setScene(scene1);
// Set the stage title.
primaryStage.setTitle("Converter");
// Show the window.
primaryStage.show();
}
/*
* Event handler class for calcButton
*/
class CalcButtonHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
// Get the kilometers.
Double kilometers = Double.parseDouble(kiloTextField.getText());
// Convert the kilometers to miles.
Double miles = kilometers * 0.6214;
// Display the results.
resultLabel.setText(String.format("%,.2f miles", miles));
}
}
class CalcButtonHandler1 implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
// Get the kilometers.
Double meters = Double.parseDouble(kiloTextField1.getText());
// Convert the kilometers to miles.
Double yards = meters * 1.094;
// Display the results.
resultLabel1.setText(String.format("%,.2f yards", yards));
}
}
class ButtonClickHandlerExit implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
// Exit the Program.
Platform.exit();
}
}
class QuitButtonHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
// Exit the Program.
Platform.exit();
}
}
class LeaveButtonHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
// Exit the Program.
Platform.exit();
}
}
}