正如您在我的代码中看到的,我的makeDigit()和makeSmallDigit()方法太长了,我希望它们位于一个单独的文件中,一个新类。但是我如何在另一个类中使用setId()方法?只是一个led功能在这里展示了太多的LED (我知道有一个单独的类提供,但我没有使用它)
我的主要课程:
public class Digital_Analog_Clock_Beta_1 extends Application
{
@Override
public void start(Stage primaryStage)
{
double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 3;
Rectangle outerBox = new Rectangle(0, 0, outerBoxWidth, outerBoxHeight);
outerBox.setId("outer-box");
Rectangle part1 = new Rectangle(0, 0, outerBoxWidth * .35, outerBoxHeight);
part1.setId("partition");
Rectangle part2 = new Rectangle(outerBoxWidth * .35, 0, outerBoxWidth * .05, outerBoxHeight);
part2.setId("partition-alternate");
Rectangle part3 = new Rectangle(outerBoxWidth * .4, 0, outerBoxWidth * .35, outerBoxHeight);
part3.setId("partition");
Rectangle part4 = new Rectangle(outerBoxWidth * .75, 0, outerBoxWidth * .25, outerBoxHeight);
part4.setId("partition");
double bigNumWidth = outerBoxWidth * .35;
double colonWidth = outerBoxWidth * .05;
double digitWidth = (.9 * bigNumWidth / 2) * 0.95;
double digitHeight = .9 * outerBoxHeight;
double smallBoxWidth = outerBoxWidth * .25;
double smallDigitWidth = (.9 * smallBoxWidth / 2) * 0.95;
double smallDigitHeight = (outerBoxHeight / 2) * .9;
Digit Digit1 = new Digit(outerBoxWidth * .02, outerBoxHeight * .05, digitWidth, digitHeight);
Digit Digit2 = new Digit(outerBoxWidth * .04 + digitWidth, outerBoxHeight * .05, digitWidth, digitHeight);
Digit Digit3 = new Digit(outerBoxWidth * .02 + bigNumWidth + colonWidth, outerBoxHeight * .05, digitWidth, digitHeight);
Digit Digit4 = new Digit(outerBoxWidth * .04 + bigNumWidth + colonWidth + digitWidth, outerBoxHeight * .05, digitWidth, digitHeight);
SmallDigit upperSmallDigit1 = new SmallDigit(bigNumWidth * 2 + colonWidth + smallBoxWidth * .04, outerBoxHeight * .03, smallDigitWidth, smallDigitHeight);
SmallDigit upperSmallDigit2 = new SmallDigit(bigNumWidth * 2 + colonWidth + smallBoxWidth * .08 + smallDigitWidth, outerBoxHeight * .03, smallDigitWidth, smallDigitHeight);
SmallDigit lowerSmallDigit1 = new SmallDigit(bigNumWidth * 2 + colonWidth + smallBoxWidth * .04, outerBoxHeight * .07 + smallDigitHeight, smallDigitWidth, smallDigitHeight);
SmallDigit lowerSmallDigit2 = new SmallDigit(bigNumWidth * 2 + colonWidth + smallBoxWidth * .08 + smallDigitWidth, outerBoxHeight * .07 + smallDigitHeight, smallDigitWidth,
smallDigitHeight);
Pane pane = new Pane();
pane.getChildren().add(outerBox);
pane.getChildren().addAll(part1, part2, part3, part4);
off(Digit1);
off(Digit2);
off(Digit3);
off(Digit4);
offSmall(lowerSmallDigit2);
offSmall(lowerSmallDigit1);
offSmall(upperSmallDigit2);
offSmall(upperSmallDigit1);
pane.getChildren().addAll(Digit1, Digit2, Digit3, Digit4, upperSmallDigit1, upperSmallDigit2, lowerSmallDigit1, lowerSmallDigit2);
Scene scene = new Scene(pane, outerBoxWidth, outerBoxHeight);
scene.getStylesheets().add(getClass().getResource("styleSheet.css").toExternalForm());
primaryStage.setTitle("DIgital Analog Clock Beta 1");
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer animator = new AnimationTimer()
{
@Override
public void handle(long arg0)
{
// update
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int minutes = calendar.get(Calendar.MINUTE);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int seconds = calendar.get(Calendar.SECOND);
int unitsMinutes = minutes % 10;
int tensMinutes = minutes / 10;
int unitsSeconds = seconds % 10;
int tensSeconds = seconds / 10;
int unitsHours = hours % 10;
int tensHours = hours / 10;
// render
off(Digit1);
off(Digit2);
off(Digit3);
off(Digit4);
offSmall(lowerSmallDigit2);
offSmall(lowerSmallDigit1);
offSmall(upperSmallDigit2);
offSmall(upperSmallDigit1);
makeDigit(Digit1, tensHours);
makeDigit(Digit2, unitsHours);
makeDigit(Digit3, tensMinutes);
makeDigit(Digit4, unitsMinutes);
makeSmallDigit(upperSmallDigit1, tensSeconds);
makeSmallDigit(upperSmallDigit2, unitsSeconds);
// makeSmallDigit();
}
};
animator.start();
}
public void off(Digit digit)
{
digit.bottom.setId("off-digits");
digit.middle.setId("off-digits");
digit.top.setId("off-digits");
digit.lowerLeft.setId("off-digits");
digit.upperLeft.setId("off-digits");
digit.lowerRight.setId("off-digits");
digit.upperRight.setId("off-digits");
}
public void offSmall(SmallDigit digit)
{
digit.bottom.setId("off-digits");
digit.middle.setId("off-digits");
digit.top.setId("off-digits");
digit.lowerLeft.setId("off-digits");
digit.upperLeft.setId("off-digits");
digit.lowerRight.setId("off-digits");
digit.upperRight.setId("off-digits");
}
public void makeDigit(Digit digit, int n)
{
if (n == 0)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 1)
{
digit.upperRight.setId("digits");
digit.lowerRight.setId("digits");
}
else if (n == 2)
{
digit.top.setId("digits");
digit.upperRight.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.bottom.setId("digits");
}
else if (n == 3)
{
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
digit.top.setId("digits");
digit.middle.setId("digits");
digit.bottom.setId("digits");
}
else if (n == 4)
{
digit.middle.setId("digits");
digit.lowerRight.setId("digits");
digit.upperLeft.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 5)
{
digit.top.setId("digits");
digit.middle.setId("digits");
digit.bottom.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
}
else if (n == 6)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
//digit.upperRight.setId("digits");
}
else if (n == 7)
{
digit.top.setId("digits");
//digit.bottom.setId("digits");
//digit.middle.setId("digits");
//digit.lowerLeft.setId("digits");
//digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 8)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 9)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
}
public void makeSmallDigit(SmallDigit digit, int n)
{
if (n == 0)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 1)
{
digit.upperRight.setId("digits");
digit.lowerRight.setId("digits");
}
else if (n == 2)
{
digit.top.setId("digits");
digit.upperRight.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.bottom.setId("digits");
}
else if (n == 3)
{
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
digit.top.setId("digits");
digit.middle.setId("digits");
digit.bottom.setId("digits");
}
else if (n == 4)
{
digit.middle.setId("digits");
digit.lowerRight.setId("digits");
digit.upperLeft.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 5)
{
digit.top.setId("digits");
digit.middle.setId("digits");
digit.bottom.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
}
else if (n == 6)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
}
else if (n == 7)
{
digit.top.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 8)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.lowerLeft.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
else if (n == 9)
{
digit.top.setId("digits");
digit.bottom.setId("digits");
digit.middle.setId("digits");
digit.upperLeft.setId("digits");
digit.lowerRight.setId("digits");
digit.upperRight.setId("digits");
}
}
public static void main(String[] args)
{
launch(args);
}
}