The assignment is to use JavaFX to create a the simulation of a running fan, to
ID: 674683 • Letter: T
Question
The assignment is to use JavaFX to create a the simulation of a running fan, to add a slider
to control the speed of the fan, make the fan blades red, and use -fx-background to code the pane background gray. There must be 3 buttons present, left button-pause, center button-resume, and right button-reverse. I have some code completed, but am lost as to how I can correct it to run appropriately?
package application;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FanSimulation extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
FanPane fan = new FanPane();
Slider slSpeed = new Slider();
slSpeed.setMax(20);
HBox paneForButtons = new HBox(20);
HBox hBox = new HBox(5);
Button btResume = new Button("Resume");
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll( btResume);
HBox hBox = new HBox(6);
Button btPause = new Button("Pause");
hBox.setAlignment(Pos.LEFT);
hBox.getChildren().addAll( btPause);
HBox hBox = new HBox(7);
Button btReverse = new Button("Reverse");
hBox.setAlignment(Pos.RIGHT);
hBox.getChildren().addAll( btReverse);
BorderPane pane = new BorderPane();
pane.setCenter(fan);
pane.setBottom(hBox);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("Exercise16_18"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
Timeline animation = new Timeline(
new KeyFrame(Duration.millis(100), e -> fan.move()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));
btReverse.setOnAction(e -> fan.reverse());
}
public static void main(String[] args) {
launch(args);
}
}
class FanPane extends Pane {
private double w = 200;
private double h = 200;
private double radius = Math.min(w, h) * 0.45;
private Arc arc[] = new Arc[4];
private double startAngle = 30;
private Circle circle = new Circle(w / 2, h / 2, radius);
public FanPane() {
circle.setStroke(Color.BLACK);
circle.setFill(Color.LIGHTGRAY);
getChildren().add(circle);
for (int i = 0; i < 4; i++) {
arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35);
arc[i].setFill(Color.RED); // Set fill color
arc[i].setType(ArcType.ROUND);
getChildren().addAll(arc[i]);
}
}
private double increment = 5;
public void reverse() {
increment = -increment;
}
public void move() {
setStartAngle(startAngle + increment);
}
public void setStartAngle(double angle) {
startAngle = angle;
setValues();
}
public void setValues() {
radius = Math.min(w, h) * 0.45;
circle.setRadius(radius);
circle.setCenterX(w / 2);
circle.setCenterY(h / 2);
for (int i = 0; i < 4; i++) {
arc[i].setRadiusX(radius * 0.9);
arc[i].setRadiusY(radius * 0.9);
arc[i].setCenterX(w / 2);
arc[i].setCenterY(h / 2);
arc[i].setStartAngle(startAngle + i * 90);
}
}
public void setW(double w) {
this.w = w;
setValues();
}
public void setH(double h) {
this.h = h;
setValues();
}
}
HELP PLSSSS!!!
Explanation / Answer
JavaFX is a software platform for creating and delivering desktop applications, as well as rich internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future.
In general, event handlers for the shape objects in the Gesture Events example perform similar operations for each type of event that is handled. For all types of events, an entry is posted to the log of events.
On platforms that support inertia for gestures, additional events might be generated after the event-type_FINISHED event. For example, SCROLL events might be generated after the SCROLL_FINISHED event if there is any inertia associated with the scroll gesture. Use the isInertia() method to identify the events that are generated based on the inertia of the gesture. If the method returns true, the event was generated after the gesture was completed.
Events are generated by gestures on a touch screen or on a trackpad. SCROLL events are also generated by the mouse wheel. Use the isDirect() method to identify the source of the event. If the method returns true, the event was generated by a gesture on a touch screen. Otherwise, the method returns false. You can use this information to provide different behavior based on the source of the event.
Touches on a touch screen also generate corresponding mouse events. For example, touching an object generates both TOUCH_PRESSED and MOUSE_PRESSED events. Use the isSynthesized() method to determine the source of the mouse event. If the method returns true, the event was generated by a touch instead of by a mouse.
The inc() and dec() methods in the Gesture Events example are used to provide a visual cue that an object is the target of a gesture. The number of gestures in progress is tracked, and the appearance of the target object is changed when the number of active gestures changes from 0 to 1 or drops to 0.
In the Gesture Events example, the handlers for the rectangle and ellipse are similar. Therefore, the code examples in the following sections show the handlers for the rectangle.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class JFXFan extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Create fan pane
FanPane fanPane = new FanPane(100);
KeyFrame keyFrame = new KeyFrame(Duration.millis(10), e-> fanPane.spin());
Timeline fanTimeline = new Timeline(keyFrame);
fanTimeline.setCycleCount(Timeline.INDEFINITE);
// Buttons pause, resume, increase, decrease, reverse
Button pause = new Button("Pause");
pause.setOnAction(e-> fanTimeline.pause());
Button resume = new Button("Resume");
resume.setOnAction(e-> fanTimeline.play());
//fanPane.increase()
Button increase = new Button("Increase");
increase.setOnAction(e-> {fanTimeline.setRate(fanTimeline.getCurrentRate() + 1);
});
Button decrease = new Button("Decrease");
decrease.setOnAction(e-> {
fanTimeline.setRate(fanTimeline.getCurrentRate() - 1);
});
Button reverse = new Button("Reverse");
reverse.setOnAction(e-> fanPane.increment *= -1);
HBox hButtons = new HBox(pause,resume,increase,decrease, reverse);
hButtons.setSpacing(10);
hButtons.setAlignment(Pos.CENTER);
hButtons.setPadding(new Insets(10, 10, 10, 10));
BorderPane borderPane = new BorderPane(fanPane, null, null, hButtons, null);
primaryStage.setScene(new Scene(borderPane));
primaryStage.setTitle("Spinning fan");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
private class FanPane extends Pane {
private Circle c;
private Arc[] blades = new Arc[4];
private double increment = 1;
FanPane(double radius) {
setMinHeight(400);
setMinWidth(400);
c = new Circle(200,200,radius,Color.TRANSPARENT);
c.setStroke(Color.BLACK);
double bladeRadius = radius * 0.9;
for (int i = 0; i < blades.length; i++) {
blades[i] = new Arc(
c.getCenterX(), c.getCenterY(), // center point
bladeRadius, bladeRadius, // X and Y radius
(i * 90) + 30, 35); // start angle and length
blades[i].setFill(Color.RED);
blades[i].setType(ArcType.ROUND);
}
getChildren().addAll(c);
getChildren().addAll(blades);
}
private void spin() {
for (Arc blade : blades) {
double prevStartAngle = blade.getStartAngle();
blade.setStartAngle(prevStartAngle + increment);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.