Skip to content

Instantly share code, notes, and snippets.

@eliel15000
Created April 10, 2018 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliel15000/a99a6a548033fd00a234d4eca6df9241 to your computer and use it in GitHub Desktop.
Save eliel15000/a99a6a548033fd00a234d4eca6df9241 to your computer and use it in GitHub Desktop.
/*
* Eliezer Encarnacion
* Due Date: 04/09/2018
* Course Number: CSC-112-D01
* Course Name: Interm Topic/Java Prgrmng
* Problem Number: HW #8
* Email: eeencarancion0001@student.stcc.edu
* Set Clock Time and Animate
*/
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
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.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ClockInAction extends Application {
public void start(Stage primaryStage) {
ClockPane clock = new ClockPane();
TimeBox timeBox = new TimeBox(clock);
ControlBox controlBox = new ControlBox(clock, timeBox);
BorderPane borderPane = new BorderPane();
borderPane.setTop(controlBox);
borderPane.setCenter(clock);
borderPane.setBottom(timeBox);
Scene scene = new Scene(borderPane, 600, 600);
primaryStage.setTitle("Clock In Action");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class ControlBox extends HBox {
public ControlBox(ClockPane clock, TimeBox timeBox) {
this.setSpacing(10);
this.setAlignment(Pos.CENTER);
this.setPadding(new Insets(10, 0, 0, 0));
Button btStop = new Button("Stop");
Button btStart = new Button("Start");
Button btCurrTimeStart = new Button("Load Current Time & Start");
Button btCurrTime = new Button("Just Load Current Time");
btStop.setOnAction(e -> {
timeBox.animation.pause();
timeBox.animation2.pause();
});
btStart.setOnAction(e -> {
timeBox.animation2.play();
});
btCurrTimeStart.setOnAction(e -> {
timeBox.animation.play();
});
btCurrTime.setOnAction(e -> {
timeBox.animation.pause();
timeBox.animation2.pause();
clock.setCurrentTime();
});
this.getChildren().addAll(btStop, btStart, btCurrTimeStart, btCurrTime);
}
}
class TimeBox extends HBox {
Timeline animation;
Timeline animation2;
public TimeBox(ClockPane clock) {
this.setSpacing(10);
this.setAlignment(Pos.CENTER);
this.setPadding(new Insets(0, 0, 10, 0));
Label hourLbl = new Label("Hour:");
Label minLbl = new Label("Minute:");
Label secLbl = new Label("Second:");
TextField hourTF = new TextField();
TextField minTF = new TextField();
TextField secTF = new TextField();
hourTF.setPrefWidth(50);
minTF.setPrefWidth(50);
secTF.setPrefWidth(50);
EventHandler<ActionEvent> eventHandler = e -> {
clock.setCurrentTime();
hourTF.setText(Integer.toString(clock.getHour()));
minTF.setText(Integer.toString(clock.getMinute()));
secTF.setText(Integer.toString(clock.getSecond()));
};
EventHandler<ActionEvent> eventHandler2 = e -> {
clock.tick();
hourTF.setText(Integer.toString(clock.getHour()));
minTF.setText(Integer.toString(clock.getMinute()));
secTF.setText(Integer.toString(clock.getSecond()));
};
animation = new Timeline(new KeyFrame(Duration.millis(1000.0), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
animation2 = new Timeline(new KeyFrame(Duration.millis(1000.0), eventHandler2));
animation2.setCycleCount(Timeline.INDEFINITE);
hourTF.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER)
tryInputs(hourTF, minTF, secTF, clock);
});
minTF.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER)
tryInputs(hourTF, minTF, secTF, clock);
});
secTF.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER)
tryInputs(hourTF, minTF, secTF, clock);
});
this.getChildren().addAll(hourLbl, hourTF, minLbl, minTF, secLbl, secTF);
}
private void tryInputs(TextField hourTF, TextField minTF, TextField secTF, ClockPane clock) {
try {
int hour = Integer.parseInt(hourTF.getText().trim());
if (hour >= 0 && hour < 24)
clock.setHour(hour);
else
hourTF.setText("ERR!");
} catch (Exception ex) {
hourTF.setText("ERR!");
}
try {
int min = Integer.parseInt(minTF.getText().trim());
if (min >= 0 && min < 60)
clock.setMinute(min);
else
minTF.setText("ERR!");
} catch (Exception ex) {
minTF.setText("ERR!");
}
try {
int sec = Integer.parseInt(secTF.getText().trim());
if (sec >= 0 && sec < 60)
clock.setSecond(sec);
else
secTF.setText("ERR!");
} catch (Exception ex) {
secTF.setText("ERR!");
}
}
}
// Eliezer Encarnacion
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class ClockPane extends Pane {
private int hour, minute, second;
private double w = 600, h = 500;
public ClockPane() {
setCurrentTime();
}
public ClockPane(int hour, int min, int sec) {
this.hour = hour;
this.minute = min;
this.second = sec;
paintClock();
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
paintClock();
}
public int getMinute() {
return minute;
}
public void setMinute(int min) {
this.minute = min;
paintClock();
}
public int getSecond() {
return second;
}
public void setSecond(int sec) {
this.second = sec;
paintClock();
}
public void setCurrentTime() {
Calendar calendar = new GregorianCalendar();
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock();
}
protected void paintClock() {
double clockRadius = Math.min(w, h) * 0.4;
double centerX = w / 2;
double centerY = h / 2;
Circle backCircle = new Circle(centerX, centerY, clockRadius + 10);
backCircle.setFill(Color.FIREBRICK);
backCircle.setStroke(Color.BLACK);
Circle circle = new Circle(centerX, centerY, clockRadius);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
Text t1 = new Text(centerX -8, centerY - clockRadius +14, "12");
Text t2 = new Text(centerX - clockRadius +3, centerY +5, "9");
Text t3 = new Text(centerX + clockRadius -10, centerY +5, "3");
Text t4 = new Text(centerX -3, centerY + clockRadius -3, "6");
double secLength = clockRadius * 0.8;
double secX = centerX + secLength * Math.sin(second * (2 * Math.PI / 60));
double secY = centerY - secLength * Math.cos(second * (2 * Math.PI / 60));
Line secLine = new Line(centerX, centerY, secX, secY);
secLine.setStroke(Color.GOLD);
secLine.setStrokeWidth(3);
double minLength = clockRadius * 0.65;
double minX = centerX + minLength * Math.sin(minute * (2 * Math.PI / 60));
double minY = centerY - minLength * Math.cos(minute * (2 * Math.PI / 60));
Line minLine = new Line(centerX, centerY, minX, minY);
minLine.setStroke(Color.RED);
minLine.setStrokeWidth(3);
double hourLength = clockRadius * 0.5;
double hourX = centerX + hourLength * Math.sin((hour % 12 + minute / 60) * (2 * Math.PI / 12));
double hourY = centerY - hourLength * Math.cos((hour % 12 + minute / 60) * (2 * Math.PI / 12));
Line hourLine = new Line(centerX, centerY, hourX, hourY);
hourLine.setStroke(Color.BLUE);
hourLine.setStrokeWidth(3);
getChildren().clear();
getChildren().addAll(backCircle, circle, t1, t2, t3,t4, secLine, minLine, hourLine);
}
public void tick() {
this.second++;
if (this.second >= 60) {
this.second = 0;
this.minute++;
if (this.minute >= 60) {
this.minute = 0;
this.hour++;
if (this.hour >= 13)
this.hour %= 12;
}
}
this.paintClock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment