Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
294 views

Control A Group of Fans With Java FX

This document contains the source code for a JavaFX application that displays three animated fan panes. Each fan pane contains a circle and four arcs that rotate around the circle. The code defines a FanPane class to represent each fan, and includes controls to start, stop, pause, and reverse each individual fan as well as start/stop all fans simultaneously. Sliders are used to control the rotation speed of each fan. The application displays the three fan panes arranged in a border pane with control buttons.

Uploaded by

josue868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
294 views

Control A Group of Fans With Java FX

This document contains the source code for a JavaFX application that displays three animated fan panes. Each fan pane contains a circle and four arcs that rotate around the circle. The code defines a FanPane class to represent each fan, and includes controls to start, stop, pause, and reverse each individual fan as well as start/stop all fans simultaneously. Sliders are used to control the rotation speed of each fan. The application displays the three fan panes arranged in a border pane with control buttons.

Uploaded by

josue868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Josue Rodriguez 0407037

Source Code
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

javafx.animation.KeyFrame;
javafx.animation.KeyFrame;
javafx.animation.Timeline;
javafx.application.Application;
javafx.geometry.Pos;
javafx.scene.Scene;
javafx.scene.control.Button;
javafx.scene.layout.BorderPane;
javafx.beans.property.DoubleProperty;
javafx.scene.layout.HBox;
javafx.scene.layout.Pane;
javafx.scene.paint.Color;
javafx.scene.shape.Arc;
javafx.scene.shape.ArcType;
javafx.scene.shape.Circle;
javafx.stage.Stage;
javafx.util.Duration;
javafx.scene.control.Slider;

public class HW_16_19 extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
FanPane fan = new FanPane();
FanPane fan2 = new FanPane();
FanPane fan3 = new FanPane();
HBox hBox = new HBox(5);
Button btPause = new Button("Pause");
Button btResume = new Button("Resume");

Josue Rodriguez 0407037


Button btReverse = new Button("Reverse");
hBox.setAlignment(Pos.BOTTOM_CENTER);
hBox.getChildren().addAll(btPause, btResume, btReverse);
HBox hBox2 = new HBox(5);
Button btPause2 = new Button("Pause");
Button btResume2 = new Button("Resume");
Button btReverse2 = new Button("Reverse");
hBox2.setAlignment(Pos.BOTTOM_CENTER);
hBox2.getChildren().addAll(btPause2, btResume2, btReverse2);
HBox hBox3 = new HBox(5);
Button btPause3 = new Button("Pause");
Button btResume3 = new Button("Resume");
Button btReverse3 = new Button("Reverse");
hBox3.setAlignment(Pos.BOTTOM_CENTER);
hBox3.getChildren().addAll(btPause3, btResume3, btReverse3);
HBox Command = new HBox(5);
Button btStart = new Button("Start All");
Button btStop = new Button("Stop All");
Command.setAlignment(Pos.CENTER);
Command.getChildren().addAll(btStart, btStop);
Slider slSpeed = new Slider(0, 10, 3);
Slider slSpeed2 = new Slider(0, 10, 3);
Slider slSpeed3 = new Slider(0, 10, 3);
fan.rateProperty().bind(slSpeed.valueProperty());
fan2.rateProperty().bind(slSpeed2.valueProperty());
fan3.rateProperty().bind(slSpeed3.valueProperty());
BorderPane pane = new BorderPane();
pane.setTop(hBox);
pane.setCenter(fan);
pane.setBottom(slSpeed);
BorderPane pane2 = new BorderPane();
pane2.setTop(hBox2);
pane2.setCenter(fan2);
pane2.setBottom(slSpeed2);
BorderPane pane3 = new BorderPane();
pane3.setTop(hBox3);
pane3.setCenter(fan3);
pane3.setBottom(slSpeed3);
BorderPane bpane = new BorderPane();
bpane.setCenter(pane);
bpane.setLeft(pane2);
bpane.setRight(pane3);

Josue Rodriguez 0407037


bpane.setBottom(Command);
// Create a scene and place it in the stage
Scene scene = new Scene(bpane, 600, 300);
primaryStage.setTitle("Exercise16_19"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));
btPause.setOnAction(e -> fan.pause());
btResume.setOnAction(e -> fan.play());
btReverse.setOnAction(e -> fan.reverse());
btPause2.setOnAction(e -> fan2.pause());
btResume2.setOnAction(e -> fan2.play());
btReverse2.setOnAction(e -> fan2.reverse());
btPause3.setOnAction(e -> fan3.pause());
btResume3.setOnAction(e -> fan3.play());
btReverse3.setOnAction(e -> fan3.reverse());
btStart.setOnAction(e -> fan.play());
btStop.setOnAction(e -> fan.stop());
btStart.setOnAction(e -> fan2.play());
btStop.setOnAction(e -> fan2.stop());
btStart.setOnAction(e -> fan3.play());
btStop.setOnAction(e -> fan3.stop());
}
public static void main(String[] args) {
launch(args);
}
}
class FanPane extends Pane {
private double w = 200;
private double h = 250;
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);
private Timeline animation;
public FanPane() {
circle.setStroke(Color.BLACK);

Josue Rodriguez 0407037


circle.setFill(Color.WHITE);
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]);
}
animation = new Timeline(
new KeyFrame(Duration.millis(100), e -> move()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
}
public void play() {
animation.play();
}
public void pause() {
animation.pause();
}
public void stop() {
animation.stop();
}
public void increaseSpeed() {
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed() {
animation.setRate(
animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty() {
return animation.rateProperty();
}
private double increment = 5;
public void reverse() {
increment = -increment;
}
public void move() {
setStartAngle(startAngle + increment);
}

Josue Rodriguez 0407037

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();
}
}

Output Sample

Josue Rodriguez 0407037

You might also like