我觉得我想以面向对象的方式练习Java,它应该是。
我正在做一个小爱好项目,我从一个名为HexaTime的Android应用程序中获得灵感。
我瞄准的想法或最终结果如下:
打开应用程序, 数字时钟以十六进制格式显示#093343而不是通常的09:33:44 / hh:mm:ss。
窗口本身的背景颜色将使用精确的当前时间进行更改,并将小时,分钟和秒数计数/转换为实际的十六进制数。然后颜色函数将改变背景的颜色,采用十六进制并将其转换为R,G,B格式/或者如果可以将纯十六进制放入Paint.valueOf(); ?
一开始我觉得我可能很简单,但现在我已经尝试了一点,我意识到它可能,但是需要花费更多的时间,比我想象的更复杂。
非常感谢一些帮助,例如在窗口上显示时钟。 就我所知,我已经尝试了一些不同的方法,使用Timer.scheduleFixRate或它被调用的方法,但由于缺乏进一步的知识,可以继续使用它。
ClockView
package hexatime;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ClockView extends Application{
public static void main (String[] args) throws InterruptedException{
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("HexaClock");
primaryStage.setScene(new Scene(new ClockController(), 450,300));
primaryStage.show();
}
}
ClockController
package hexatime;
import java.util.Calendar;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.control.Label;
import javafx.util.Duration;
public class ClockController extends Label{
public ClockModel clock;
public ClockController(){
//updateTime();
}
/*private void updateTime(){
Timeline timeline = new Timeline();
new KeyFrame(Duration.seconds(0),
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
Calendar time = Calendar.getInstance();
SimpleDateFormat sdf = SimpleDateFormat("HH:mm:ss");
setText(sdf.format(time.getTime()));
}
}
),
new KeyFrame(Duration.seconds(1))
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}*/
public static int[] hexToNum(String hex){
//if (hex.charAt(0) == "#"){
//hex = hex.split("#");
hex = hex.toUpperCase();
String hex_alphabets = "0123456789ABCDEF";
int[] value = new int[3];
int k = 0;
int int1, int2;
for (int i = 0; i < 6; i+=2){
int1 = hex_alphabets.indexOf(hex.charAt(i));
int2 = hex_alphabets.indexOf(hex.charAt(i+1));
value[k] = (int1 * 16) + int2;
k++;
}
return value;
}
}
ClockModel
package hexatime;
import java.util.Date;
public class ClockModel implements ClockInterface {
public int hour;
public int minute;
public int seconds;
public String hexHour;
public String hexMinute;
public String hexSecond;
@SuppressWarnings("deprecation")
public ClockModel(Date time){
hour = time.getHours();
minute = time.getMinutes();
seconds= time.getSeconds();
}
public int getHour(){
return hour;
}
public int getMinutes(){
return minute;
}
public int getSeconds(){
return seconds;
}
}
答案 0 :(得分:4)
我无法评论如何使用OOP概念,但我可以通过简单的方式向您展示如何在JavaFX中执行此操作。
pane.setStyle("-fx-background-color : your hex code");
Timeline
代替Timer.scheduleFixRate
,它将始终保留您对JavaFX应用程序主题的更改显示这些内容的简单示例(在一个单独的类中,您可以稍后将它们分开添加更多功能并将OOP应用于它;)
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
public class HexTime extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(getColoredPane(), 450, 500);
primaryStage.setTitle("Hexa Time");
primaryStage.setScene(scene);
primaryStage.show();
}
public BorderPane getColoredPane() {
BorderPane pane = new BorderPane();
Label label = new Label();
label.setFont(Font.font(20));
Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
KeyFrame changeColor = new KeyFrame(Duration.seconds(1),
new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Map<String, String> colortime = generateColor();
label.setText(colortime.get("time"));
label.setStyle("-fx-text-fill: white");
pane.setStyle("-fx-background-color : " + colortime.get("color"));
}
});
timeline.getKeyFrames().add(changeColor);
timeline.play();
pane.setCenter(label);
return pane;
}
public Map<String, String> generateColor() {
int hour;
int min;
int sec;
Calendar cal;
String hexValue;
Map<String, String> map = new HashMap<String, String>();
cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
sec = cal.get(Calendar.SECOND);
// Color Code Production
hexValue = String.format("%02X%02X%02X", hour, min, sec);
map.put("color", "#" + hexValue);
map.put("time", "#" + String.format("%02d", hour) + String.format("%02d", min) + String.format("%02d", sec));
return map;
}
public static void main(String[] args) {
launch(args);
}
}