javafx给图形上颜色_JavaFX - 颜色( Colors)
时间: 2024-05-16 15:15:27 浏览: 138
在JavaFX中,可以使用预定义的颜色常量或RGB值来设置图形的颜色。以下是一些示例:
1. 使用预定义的颜色常量:
```java
import javafx.scene.paint.Color;
// 设置文本颜色为红色
text.setFill(Color.RED);
// 设置矩形填充颜色为蓝色
rectangle.setFill(Color.BLUE);
```
2. 使用RGB值:
```java
import javafx.scene.paint.Color;
// 设置文本颜色为深灰色
text.setFill(Color.rgb(64, 64, 64));
// 设置矩形边框颜色为浅粉色
rectangle.setStroke(Color.rgb(255, 192, 203));
```
3. 使用十六进制值:
```java
import javafx.scene.paint.Color;
// 设置文本颜色为绿色
text.setFill(Color.web("#00FF00"));
// 设置矩形填充颜色为黄色
rectangle.setFill(Color.web("#FFFF00"));
```
以上是JavaFX中设置图形颜色的几种方法,可以根据具体需求选择不同的方式。
相关问题
记忆力测试javafx代码
以下是一个简单的记忆力测试JavaFX代码示例:
```java
import javafx.application.Application;
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.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;
public class MemoryTest extends Application {
private ArrayList<Button> buttons = new ArrayList<>();
private ArrayList<String> colors = new ArrayList<>();
private int numButtons = 6;
private int currentLevel = 1;
private int buttonCounter = 0;
private int score = 0;
private boolean playerTurn = false;
private boolean gameOver = false;
@Override
public void start(Stage primaryStage) {
// Set up colors
colors.add("red");
colors.add("blue");
colors.add("green");
colors.add("yellow");
colors.add("purple");
colors.add("orange");
// Set up UI
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 400, 400);
Label levelLabel = new Label("Level " + currentLevel);
grid.add(levelLabel, 0, 0);
Button startButton = new Button("Start");
startButton.setOnAction(e -> {
startGame(grid);
});
grid.add(startButton, 1, 0);
StackPane scorePane = new StackPane();
Label scoreLabel = new Label("Score: " + score);
scorePane.getChildren().add(scoreLabel);
StackPane.setAlignment(scoreLabel, Pos.CENTER);
grid.add(scorePane, 2, 0);
// Set up buttons
for (int i = 0; i < numButtons; i++) {
Button button = new Button();
button.setMinSize(50, 50);
button.setMaxSize(50, 50);
button.setOnAction(e -> {
if (playerTurn && !gameOver) {
checkButton(button);
}
});
buttons.add(button);
}
// Add buttons to grid
int row = 1;
int col = 0;
for (int i = 0; i < numButtons; i++) {
grid.add(buttons.get(i), col, row);
col++;
if (col == 3) {
col = 0;
row++;
}
}
primaryStage.setScene(scene);
primaryStage.show();
}
private void startGame(GridPane grid) {
// Disable start button
Button startButton = (Button) grid.getChildren().get(1);
startButton.setDisable(true);
// Shuffle colors and assign to buttons
Collections.shuffle(colors);
for (int i = 0; i < numButtons; i++) {
buttons.get(i).setStyle("-fx-background-color: " + colors.get(i));
}
// Wait for 3 seconds
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Hide colors and enable buttons
for (int i = 0; i < numButtons; i++) {
buttons.get(i).setStyle("");
}
playerTurn = true;
}
private void checkButton(Button button) {
// Increment button counter and update score label
buttonCounter++;
score++;
StackPane scorePane = (StackPane) button.getParent().getChildrenUnmodifiable().get(2);
Label scoreLabel = (Label) scorePane.getChildren().get(0);
scoreLabel.setText("Score: " + score);
// Check if player has clicked all buttons
if (buttonCounter == numButtons) {
playerTurn = false;
buttonCounter = 0;
currentLevel++;
Label levelLabel = (Label) button.getParent().getChildrenUnmodifiable().get(0);
levelLabel.setText("Level " + currentLevel);
// Check if player has won game
if (currentLevel > 5) {
gameOver = true;
Label gameOverLabel = new Label("Game Over!");
StackPane.setAlignment(gameOverLabel, Pos.CENTER);
StackPane.setMargin(gameOverLabel, new Insets(0, 0, 100, 0));
StackPane gamePane = (StackPane) button.getParent().getParent();
gamePane.getChildren().add(gameOverLabel);
} else {
startGame((GridPane) button.getParent());
}
}
}
public static void main(String[] args) {
launch(args);
}
}
```
该应用程序使用 JavaFX 构建,包含一个网格布局和一些按钮。在游戏开始时,应用程序会将按钮上的颜色随机化,并在三秒钟内显示这些颜色。玩家需要记住它们并在随后的回合中点击相应的按钮。如果玩家成功记住所有颜色,他们会进入下一级。如果他们完成了五个级别,他们获胜。如果玩家犯错,则游戏结束。该应用程序还包括一个得分计数器和一个“分数”标签,以及一个“级别”标签。
阅读全文