Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Открыть модальное окно с JavaFX</title>
- <script>
- function openModal() {
- // Открыть модальное окно
- var modal = document.getElementById("myModal");
- modal.style.display = "block";
- }
- function closeModal() {
- // Закрыть модальное окно
- var modal = document.getElementById("myModal");
- modal.style.display = "none";
- }
- </script>
- /* Стили для модального окна */
- .modal {
- display: none; /* Скрыто по умолчанию */
- position: fixed; /* Относительно видимой области браузера */
- z-index: 1; /* Поверх всего остального */
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- overflow: auto; /* Добавить прокрутку, если содержимое длинное */
- background-color: rgba(0,0,0,0.4); /* Темный фон с небольшим прозрачным */
- }
- /* Стили для содержимого модального окна */
- .modal-content {
- background-color: #fefefe;
- margin: 15% auto;
- padding: 20px;
- border: 1px solid #888;
- width: 80%;
- }
- /* Стили для кнопки закрытия */
- .close {
- color: #aaa;
- float: right;
- font-size: 28px;
- font-weight: bold;
- }
- .close:hover,
- .close:focus {
- color: black;
- text-decoration: none;
- cursor: pointer;
- }
- </head>
- <body>
- <h2>Пример открытия модального окна с JavaFX</h2>
- <button onclick="openModal()">Открыть модальное окно</button>
- <!-- Модальное окно -->
- <div id="myModal" class="modal">
- <div class="modal-content">
- <span class="close" onclick="closeModal()">×</span>
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- public class HangmanGame extends Application {
- private static final String[] WORDS = {"JAVA", "PYTHON", "HTML", "CSS", "JAVASCRIPT"};
- private String wordToGuess;
- private StringBuilder currentGuess;
- private int attemptsLeft;
- private boolean gameEnded;
- @Override
- public void start(Stage primaryStage) {
- Canvas canvas = new Canvas(400, 400);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- resetGame();
- canvas.setOnKeyPressed(event -> {
- if (!gameEnded && event.getCode().isLetterKey()) {
- char guessedLetter = event.getText().toUpperCase().charAt(0);
- boolean letterFound = false;
- for (int i = 0; i < wordToGuess.length(); i++) {
- if (wordToGuess.charAt(i) == guessedLetter) {
- currentGuess.setCharAt(i, guessedLetter);
- letterFound = true;
- }
- }
- if (!letterFound) {
- attemptsLeft--;
- drawHangman(gc);
- }
- drawWord(gc);
- if (attemptsLeft == 0 || !currentGuess.toString().contains("_")) {
- endGame(attemptsLeft == 0);
- }
- }
- });
- VBox root = new VBox(canvas);
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Hangman Game");
- primaryStage.show();
- canvas.requestFocus();
- drawHangman(gc);
- drawWord(gc);
- }
- private void resetGame() {
- wordToGuess = WORDS[(int) (Math.random() * WORDS.length)];
- currentGuess = new StringBuilder("_".repeat(wordToGuess.length()));
- attemptsLeft = 6;
- gameEnded = false;
- }
- private void drawWord(GraphicsContext gc) {
- gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
- gc.setFill(Color.BLACK);
- gc.fillText(currentGuess.toString(), 150, 350);
- }
- private void drawHangman(GraphicsContext gc) {
- gc.setStroke(Color.BLACK);
- gc.setLineWidth(2);
- if (attemptsLeft < 6) {
- gc.strokeOval(100, 50, 50, 50); // Head
- }
- if (attemptsLeft < 5) {
- gc.strokeLine(125, 100, 125, 200); // Body
- }
- if (attemptsLeft < 4) {
- gc.strokeLine(125, 120, 75, 100); // Left Arm
- }
- if (attemptsLeft < 3) {
- gc.strokeLine(125, 120, 175, 100); // Right Arm
- }
- if (attemptsLeft < 2) {
- gc.strokeLine(125, 200, 75, 250); // Left Leg
- }
- if (attemptsLeft < 1) {
- gc.strokeLine(125, 200, 175, 250); // Right Leg
- }
- }
- private void endGame(boolean lost) {
- gameEnded = true;
- String message = lost ? "You lost! The word was: " + wordToGuess : "Congratulations! You won!";
- Alert alert = new Alert(AlertType.INFORMATION, message);
- alert.setTitle("Game Over");
- alert.setHeaderText(null);
- alert.showAndWait();
- resetGame();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- <div id="javafx-container"></div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment