Skip to content

Commit 19897ba

Browse files
author
Pablo Fernandez
authored
Merge pull request #19 from WhitmanSWDesignSpring2017/master
Master
2 parents 6eec17a + c0f32b3 commit 19897ba

File tree

4 files changed

+75
-55
lines changed

4 files changed

+75
-55
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
# project1-quinn-pablo
22
project1-quinn-pablo created by GitHub Classroom
33

4-
1) Our design creates a layout with two distinct buttons each with their own event handles. The start button shows a dialogue box that asks for an input. That input is then chopped and parsed into an integer, such that if the user entered 0-115 sets it at the starting note. Then it plays the scale. If the stop button is pressed at any time methods are called utilize built in methods to stop and clear the sequence of notes that had been created (and is currently playing). The scale is played using 2 for loops. One up, one down.
4+
1) Our start method now instead of containing the logic and construction of the entire program, now really only reads in the FXML file, which it uses to build the scene then show it. There are three separate methods, one for the start button, stop button, and close button, as well as two additional helper functions to allow us to easily play the scale. We changed several functions from public to private, as their original designation was unnecessary. We also fixed the core functionality so that it actually plays a scale, rather then the simple ladder we had before.
55

6+
2) This way the entire logic and construction of the program is not contained in the ‘start’ method. It is broken into smaller bits to make the program more readable/maintainable.
67

7-
2) The dialogue box and getting the user input all happen in the method to handle when the play button is pressed, since these things will only ever occur in that scenario. We didn’t feel the need to create helper functions for these.
8+
3) Speaking to a few other groups it appeared the only option was to use the dialogue window the same way we did in the first iteration of this project. This means there is still a chunk of code constructing a window in our controller (under the method for the start button being pressed). Maybe this can be done with another FXML file?
89

9-
We did create helper functions to easily use the Midi class. These stop, clear, or play the scale. We thought especially for playing it would be more eloquent and reusable to be able to play a scale based off of just a given number, hiding the ugly for loop from the rest of the code.
10+
4) We worked together on using scene builder to reconstruct the window, as well as figure out how to link the FXML file to the controller class. Quinn worked a little more on populating the ScalePlayer class’s new methods, while Pablo worked on fixing mistakes from our last project.
1011

11-
12-
3) Hiding away the midi class interactions makes the control flow much more readable and understandable.
13-
14-
15-
4) The part where we handle the dialogue box is somewhat ineloquent. Maybe it could all be contained in a function called promptForNote() or something like that, but it was a struggle even to get to this point.
16-
17-
18-
5) We worked side by side. Quinn handling most of the layout and Pablo handling most of the interaction with the Midi class. It worked well since we both discussed issues we were facing with each other, so we both got to understand what the other was working on.

src/scaleplayer/ScalePlayer.fxml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
<?import javafx.scene.layout.BorderPane?>
1010
<?import javafx.scene.layout.HBox?>
1111

12-
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
12+
<AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="400.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="scaleplayer.ScalePlayer">
1313
<stylesheets>
1414
<URL value="@scaleplayer.fxml.css" />
1515
</stylesheets>
1616
<children>
17-
<BorderPane prefHeight="400.0" prefWidth="600.0">
17+
<BorderPane prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" >
1818
<top>
1919
<MenuBar prefHeight="29.0" prefWidth="138.0" BorderPane.alignment="CENTER">
2020
<menus>
2121
<Menu mnemonicParsing="false" text="File">
2222
<items>
23-
<MenuItem mnemonicParsing="false" text="Close" />
23+
<MenuItem fx:id="closeItem" mnemonicParsing="false" onAction="#handleCloseClick" text="Close" />
2424
</items>
2525
</Menu>
2626
</menus>
@@ -29,8 +29,8 @@
2929
<center>
3030
<HBox alignment="CENTER" prefHeight="371.0" prefWidth="358.0" style="-fx-spacing: 20;" BorderPane.alignment="CENTER">
3131
<children>
32-
<Button alignment="CENTER" mnemonicParsing="false" pickOnBounds="false" style="-fx-background-color: lightgreen; -fx-border-color: green;" text="Start Playing" />
33-
<Button alignment="CENTER" mnemonicParsing="false" style="-fx-background-color: pink; -fx-border-color: red;" text="Play Scale" />
32+
<Button id = "startButton" fx:id="startButton" alignment="CENTER" mnemonicParsing="false" onAction="#handleStartClick" pickOnBounds="false" text="Start Playing" />
33+
<Button id = "closeButton" fx:id="closeButton" alignment="CENTER" mnemonicParsing="false" onAction="#handleStopClick" text="Stop Scale" />
3434
</children>
3535
</HBox>
3636
</center>

src/scaleplayer/ScalePlayer.java

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44

55
package scaleplayer;
66

7-
import javafx.application.Application;
8-
import javafx.application.Platform;
7+
import java.util.Optional;
98
import javafx.event.ActionEvent;
10-
import javafx.event.EventHandler;
11-
import javafx.geometry.Pos;
12-
import javafx.scene.Scene;
139
import javafx.scene.control.Button;
14-
import javafx.scene.layout.StackPane;
15-
import javafx.stage.Stage;
1610
import javafx.scene.layout.*;
1711
import javafx.scene.control.*;
1812
import javafx.*;
19-
import java.util.*;
13+
import javafx.fxml.FXML;
2014

2115
import javafx.application.Application;
2216
import javafx.fxml.FXMLLoader;
2317
import javafx.scene.Scene;
24-
import javafx.scene.layout.StackPane;
2518
import javafx.stage.Stage;
2619

2720

@@ -33,10 +26,8 @@
3326
*/
3427
public class ScalePlayer extends Application {
3528

36-
//TODO: Why does this field exist?
37-
private static int startingNote;
38-
//TODO: Could this field be static, final?
39-
private MidiPlayer sequence = new MidiPlayer(2, 60);
29+
private static int startingNote; // Needed for method calls
30+
private static MidiPlayer sequence = new MidiPlayer(2, 60);
4031

4132
@Override
4233
public void start(Stage primaryStage) throws Exception {
@@ -47,27 +38,67 @@ public void start(Stage primaryStage) throws Exception {
4738
primaryStage.setScene(scene);
4839
primaryStage.show();
4940
primaryStage.setOnCloseRequest(e->System.exit(0));
50-
5141
} catch (Exception ex) {
42+
43+
}
44+
}
45+
46+
@FXML
47+
private MenuItem closeItem;
48+
49+
@FXML
50+
private Button startButton;
51+
52+
@FXML
53+
private Button closeButton;
54+
55+
@FXML
56+
void handleCloseClick(ActionEvent event) {
57+
System.exit(0);
58+
}
59+
60+
@FXML
61+
void handleStartClick(ActionEvent event) {
62+
63+
TextInputDialog dialog = new TextInputDialog();
64+
dialog.setTitle("Starting Note");
65+
dialog.setHeaderText("Please enter a note (0-115)");
66+
67+
//get result, parse it into an int in a roundabout way, then play scale once its had
68+
Optional<String> result = dialog.showAndWait();
69+
if (result.isPresent()){
70+
String stringResult = result.toString();
71+
stringResult = stringResult.substring(9, stringResult.length()-1);
72+
startingNote = Integer.parseInt(stringResult);
73+
stopScale(sequence);
74+
clearScale(sequence);
75+
playScale(sequence, startingNote);
5276
}
53-
}
77+
}
78+
79+
@FXML
80+
void handleStopClick(ActionEvent event) {
81+
stopScale(sequence);
82+
clearScale(sequence);
83+
}
5484

5585
/**
5686
* Stops the current MidiPlayer sequence
57-
* @param sequence //TODO: What is it?
87+
* @param sequence
88+
* Stops the scale, method is built in case functionality requirements
89+
* should grow in future versions
5890
*/
59-
//TODO: Why is this public?
60-
public void stopScale(MidiPlayer sequence) {
61-
//TODO: Why do you need to define a one line method?
91+
private void stopScale(MidiPlayer sequence) {
6292
sequence.stop();
6393
}
6494

6595
/**
6696
* Clears the current MidiPlayer sequence
6797
* @param sequence
98+
* Clears the scale, method is built in case functionality requirements
99+
* should grow in future versions.
68100
*/
69101
public void clearScale(MidiPlayer sequence) {
70-
//TODO: See above
71102
sequence.clear();
72103
}
73104

@@ -77,29 +108,21 @@ public void clearScale(MidiPlayer sequence) {
77108
* @param sequence
78109
* @param startingNote
79110
*/
80-
//TODO: Why public?
81-
public void playScale(MidiPlayer sequence, int startingNote) {
82-
//TODO: Fix this so it's a Do-Re-Mi scale
83-
//https://en.wikipedia.org/wiki/Solfège#Major
84-
//TODO: Replace magic numbers with constants
85-
//TODO: Fix indentation
86-
for(int i=1; i<9; i++)
87-
{
111+
private void playScale(MidiPlayer sequence, int startingNote) {
112+
int[] scale = {0,2,2,1,2,2,2,1,0,0,1,2,2,2,1,2,2};
113+
for(int i=1; i<9; i++) {
114+
startingNote = startingNote + scale[i-1];
88115
sequence.addNote(startingNote, 100, i, 1,
89116
1, 1);
90-
startingNote = startingNote + 1;
91117
}
92-
93-
startingNote = startingNote - 1;
94-
for(int i=10; i<18; i++)
95-
{
118+
for(int i=10; i<18; i++) {
119+
startingNote = startingNote - scale[i-1];
96120
sequence.addNote(startingNote, 100, i, 1,
97121
1, 1);
98-
startingNote = startingNote - 1;
99122
}
100-
sequence.play();
123+
sequence.play();
101124
}
102-
125+
103126
/**
104127
* Starts the program
105128
* @param args the command line arguments
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/*
22
* Empty Stylesheet file.
33
*/
4-
5-
.mainFxmlClass {
6-
4+
#closeButton {
5+
-fx-background-color: pink;
6+
-fx-border-color: red;
7+
}
8+
#startButton {
9+
-fx-background-color: lightgreen;
10+
-fx-border-color: green;
711
}

0 commit comments

Comments
 (0)