Modify the clock pane class by adding complete clock numbers with ticks. 12, 1,
ID: 3817559 • Letter: M
Question
Modify the clock pane class by adding complete clock numbers with ticks. 12, 1, 2, 3 4, 5, 6, 7, 8, 9, 10,11,12
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;
private int minute;
private int second;
//Add custom width and height
private double width;
private double height;
/** Construct a default clock with the current time */
public ClockPane() {
setCurrentTime(); }
/** Construct a clock with specified hour, minute, and second */
public ClockPane(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second; }
/** Construct a clock with specified hour, minute, and second */ public ClockPane(int hour, int minute, int second,double width,double height) { this.hour = hour; this.minute = minute; this.second = second; this.width = width; this.height = height; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; paintClock(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; paintClock(); } /** Return second */ public int getSecond() { return second; } /** Set a new second */ public void setSecond(int second) { this.second = second; paintClock(); } /* Set the current time for the clock */ public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); paintClock(); // Repaint the clock } /** Paint the clock */ private void paintClock() { // Initialize clock parameters //Use custom width and height double clockRadius = Math.min(width, height) * 0.8 * 0.5; double centerX = width / 2; double centerY = height / 2; // Draw circle Circle circle = new Circle(centerX, centerY, clockRadius); circle.setFill(Color.WHITE); circle.setStroke(Color.BLACK); Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12"); Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9"); Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3"); Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6"); // Draw second hand double sLength = clockRadius * 0.8; double secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60)); double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60)); Line sLine = new Line(centerX, centerY, secondX, secondY); sLine.setStroke(Color.RED); // Draw minute hand double mLength = clockRadius * 0.65; double xMinute = centerX + mLength * Math.sin(minute * (2 * Math.PI / 60)); double minuteY = centerY - mLength * Math.cos(minute * (2 * Math.PI / 60)); Line mLine = new Line(centerX, centerY, xMinute, minuteY); mLine.setStroke(Color.BLUE); // Draw hour hand double hLength = clockRadius * 0.5; double hourX = centerX + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)); double hourY = centerY - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)); Line hLine = new Line(centerX, centerY, hourX, hourY); hLine.setStroke(Color.GREEN); getChildren().clear(); getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine); } @Override public void setWidth(double width) { super.setWidth(width); paintClock(); } @Override public void setHeight(double height) { super.setHeight(height); paintClock(); } } //Clock driver class: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Clock extends Application { @Override public void start(Stage primaryStage) { double height = 400; double width = 400; //Create clock pane with height & width ClockPane clock = new ClockPane(4, 20, 45, width / 2, height/2); HBox hBox = new HBox(clock); Scene scene = new Scene(hBox, width, height); primaryStage.setScene(scene); primaryStage.setTitle("Clock"); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } }
Explanation / Answer
Ans:: I explained the program with comments here.Thank you.
///Program///
import java.util.Calendar; // import calender
import java.util.GregorianCalendar; // Gregorian_cal
import javafx.scene.layout.Pane; // pane
import javafx.scene.paint.Color; // color
import javafx.scene.shape.Circle; // circle
import javafx.scene.shape.Line; // Line
import javafx.scene.text.Text; // Text
import java.util.Collections; // collections
import javafx.animation.KeyFrame; // keyframe
import javafx.animation.Timeline; Timeline
import javafx.collections.ObservableList; // ObservbleLIST
import javafx.geometry.Point2D; // pOINT_2d
import javafx.scene.Node; // nODE
import javafx.util.Duration; // dURATION
// Clock-Pane_class
public class ClockPane extends Pane {
// int hour, min, secnd
private int hour;
private int minute;
private int second;
// handVisible true
private boolean hourHandVisible = true;
private boolean minuteHandVisible = true;
private boolean secondHandVisible = true;
private Timeline timeline;
// Clock_pane height & width
private double w = 300, h = 300;
// Construct default_clock with current_time
// ClockPane()
public ClockPane() {
setPrefHeight(h); // height-h
setPrefWidth(w); // width-w
// timeline
timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> update()));
// CycleCount
timeline.setCycleCount(Timeline.INDEFINITE);
// Current_Time
setCurrentTime();
}
// clock with secnd,min, hour
public ClockPane(int hour, int minute, int second) {
// this
this();
this.hour = hour; // hour
this.minute = minute; // min
this.second = second; // sec
// paint
paintClock();
}
// int hour, min, sec, double- width, height
public ClockPane(int hour, int minute, int second, double width, double height) {
// this
this(hour, minute, second);
this.w = width;
this.h = height;
// paint
paintClock();
}
// returns the hour
public int getHour() {
return hour;
}
// setting new hour
public void setHour(int hour) {
this.hour = hour;
paintClock();
}
// returns_minute
public int getMinute() {
return minute;
}
// sets new_minute
public void setMinute(int minute) {
this.minute = minute;
paintClock();
}
// retrns seconds
public int getSecond() {
return second;
}
// setting new seconds
public void setSecond(int second) {
this.second = second;
paintClock();
}
//retrbs clock_pane width
public double getW() {
return w;
}
// Setting clock-pane width
public void setW(double w) {
this.w = w;
paintClock();
}
// Returns clock-pane height
public double getH() {
return h;
}
// Settiong clock-pane height
public void setH(double h) {
this.h = h;
paintClock();
}
// boolean Hours hand visible
public boolean isHourHandVisible() {
// return
return hourHandVisible;
}
// boolean set visible
public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
paintClock();
}
// boolean min hand visibl
public boolean isMinuteHandVisible() {
// return
return minuteHandVisible;
}
// set min hand visibl
public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
paintClock();
}
// bool secnd hand visible
public boolean isSecondHandVisible() {
// return
return secondHandVisible;
}
// set secnd visible- bool
public void setSecondHandVisible(boolean secondHandVisible) {
this.secondHandVisible = secondHandVisible;
paintClock();
}
// Setting current_time for clock
public void setCurrentTime() {
// gregorian_calender
Calendar calendar = new GregorianCalendar();
// Setting the current_hour, minute & sec
// this..hour
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
// this.min
this.minute = calendar.get(Calendar.MINUTE);
// this.secnd
this.second = calendar.get(Calendar.SECOND);
// re_paints the clock
paintClock();
}
/// Paintint the clock
private void paintClock() {
// Initializing the clock_parameters radius_math.(w,h)
double clockRadius = Math.min(w, h) * 0.8 * 0.5;
// centerX = width divide by 2
double centerX = w / 2;
// centerY = height divide by 2
double centerY = h / 2;
// point_2D center x,y
Point2D center = new Point2D(centerX, centerY);
// Drawing the circle X,Y, radius
Circle circle = new Circle(centerX, centerY, clockRadius);
// filling white_color
circle.setFill(Color.WHITE);
// stroke black_color
circle.setStroke(Color.BLACK);
// Drawing the time numbers 1 to 12
Text[] texts = new Text[12];
// for loop for numbers 1 to 12
for (int i = 0; i < 12; i++) {
int time = (i + 3 > 12) ? i + 3 - 12 : i + 3;
// initialising object center X+ radius+ Math.cos(i * 2 * Math.PI / 12) and math.sin
Point2D b = new Point2D(
centerX + clockRadius * Math.cos(i * 2 * Math.PI / 12),
centerY + clockRadius * Math.sin(i * 2 * Math.PI / 12));
// point_Closer_to_A - center, b, 0.82
b = getPointBCloserToA(center, b, 0.82);
// text[]
texts[i] = new Text(b.getX() - (clockRadius * 0.03125), b.getY() + (clockRadius * 0.025), "" + time);
}
// Drawing the dashes
// Line[60]
Line[] dashes = new Line[60];
// for loop i< dashes
for (int i = 0; i < dashes.length; i++) {
// point_2D start centerX + clockRadius * Math.cos(i * 2 * Math.PI divide by 60), & math.sin
Point2D start = new Point2D(
centerX + clockRadius * Math.cos(i * 2 * Math.PI / 60),
centerY + clockRadius * Math.sin(i * 2 * Math.PI / 60));
// coefficient 0.955
double coefficient = (i % 5 == 0) ? 0.91 : 0.955;
// point_2D get Point B Closer To A
Point2D end = getPointBCloserToA(center,start, coefficient);
// dashes[i]
dashes[i] = new Line(start.getX(), start.getY(), end.getX(), end.getY());
}
// Drawing seconds_hand
// sLength r*0.8
double sLength = clockRadius * 0.8;
// seconX = centerX*sLength math.sin()
double secondX = centerX + sLength *
Math.sin(second * (2 * Math.PI / 60));
// secondY = centerY - sLength* math.cos()
double secondY = centerY - sLength *
Math.cos(second * (2 * Math.PI / 60));
Line sLine = new Line(centerX, centerY, secondX, secondY);
// set_ strome color= red
sLine.setStroke(Color.RED);
// set_visible
sLine.setVisible(isSecondHandVisible());
// Drawing the minuteshand
// m_Length
double mLength = clockRadius * 0.65;
// x_minute
double xMinute = centerX + mLength *
Math.sin(minute * (2 * Math.PI / 60));
// minuteY
double minuteY = centerY - mLength *
Math.cos(minute * (2 * Math.PI / 60));
// mLine
Line mLine = new Line(centerX, centerY, xMinute, minuteY);
// setStroke colour is blue
mLine.setStroke(Color.BLUE);
// set Visible
mLine.setVisible(isMinuteHandVisible());
// Drawing the hour hand
// h_Length
double hLength = clockRadius * 0.5;
// hour_X
double hourX = centerX + hLength *
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
// hour_Y
double hourY = centerY - hLength *
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
Line hLine = new Line(centerX, centerY, hourX, hourY);
hLine.setStroke(Color.GREEN);
hLine.setVisible(isHourHandVisible());
// Drawing the time (HH:MM:SS)
String s = "" + getHour() + ":" + getMinute() + ":" + getSecond();
Text timeText = new Text(getW() * 0.4, getH() - 10, s);
// Add_nodes to pane
getChildren().clear(); // clear()
ObservableList<Node> list = getChildren(); // List
list.add(circle); // circle
Collections.addAll(list, dashes); // collection
Collections.addAll(list, texts); // list, texts
list.addAll(sLine, mLine, hLine, timeText); // list.addAll
}
// start-- timeline.play()
public void start(){
timeline.play();
}
// stop()
public void stop(){
// pause
timeline.pause();
}
// update
private void update(){
setCurrentTime(); // curr timr
paintClock();
}
// private point_2D
private Point2D getPointBCloserToA(Point2D a, Point2D b, double coefficient) {
// deltaX
double deltaX = b.getX() - a.getX();
// deltaY
double deltaY = b.getY() - a.getY();
// return
return new Point2D(
a.getX() + coefficient * deltaX,
a.getY() + coefficient * deltaY);
}
}
//////// application ////////////
import javafx.application.Application; // import app
import javafx.geometry.Insets; // insets
import javafx.geometry.Pos; // pos
import javafx.scene.Scene; // scene
import javafx.scene.control.Button; // button
import javafx.scene.layout.BorderPane; // border pane
import javafx.scene.layout.HBox; // hbox
import javafx.stage.Stage; // stage
// publc class
public class ClockPane extends Application {
// overridng
@Override
// start and exception
public void start(Stage primaryStage) throws Exception {
// clock_pane
ClockPane clockPane = new ClockPane();
// start
clockPane.start();
// button start
Button btStart = new Button("Start");
// on_action e-> start
btStart.setOnAction(e -> clockPane.start());
// stop button
Button btStop = new Button("Stop");
// onAction stop()
btStop.setOnAction(e -> clockPane.stop());
// HBox start,stop
HBox hBox = new HBox(btStart, btStop);
// spacng -12
hBox.setSpacing(12);
// padding 12,12,12,12
hBox.setPadding(new Insets(12, 12, 12, 12));
// aligning to center
hBox.setAlignment(Pos.CENTER);
// borderpane setting
BorderPane borderPane = new BorderPane(clockPane, null, null, hBox, null);
// set_Scene
primaryStage.setScene(new Scene(borderPane));
// setTitle
primaryStage.setTitle("Clock");
// primStage.show()
primaryStage.show();
}
// main
public static void main(String[] args) {
// application launch()
Application.launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.