karen_constantine

Untitled

Mar 10th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Открыть модальное окно с JavaFX</title>
  5. <script>
  6. function openModal() {
  7. // Открыть модальное окно
  8. var modal = document.getElementById("myModal");
  9. modal.style.display = "block";
  10. }
  11.  
  12. function closeModal() {
  13. // Закрыть модальное окно
  14. var modal = document.getElementById("myModal");
  15. modal.style.display = "none";
  16. }
  17. </script>
  18. /* Стили для модального окна */
  19. .modal {
  20. display: none; /* Скрыто по умолчанию */
  21. position: fixed; /* Относительно видимой области браузера */
  22. z-index: 1; /* Поверх всего остального */
  23. left: 0;
  24. top: 0;
  25. width: 100%;
  26. height: 100%;
  27. overflow: auto; /* Добавить прокрутку, если содержимое длинное */
  28. background-color: rgba(0,0,0,0.4); /* Темный фон с небольшим прозрачным */
  29. }
  30.  
  31. /* Стили для содержимого модального окна */
  32. .modal-content {
  33. background-color: #fefefe;
  34. margin: 15% auto;
  35. padding: 20px;
  36. border: 1px solid #888;
  37. width: 80%;
  38. }
  39.  
  40. /* Стили для кнопки закрытия */
  41. .close {
  42. color: #aaa;
  43. float: right;
  44. font-size: 28px;
  45. font-weight: bold;
  46. }
  47.  
  48. .close:hover,
  49. .close:focus {
  50. color: black;
  51. text-decoration: none;
  52. cursor: pointer;
  53. }
  54.  
  55. </head>
  56. <body>
  57.  
  58. <h2>Пример открытия модального окна с JavaFX</h2>
  59.  
  60. <button onclick="openModal()">Открыть модальное окно</button>
  61.  
  62. <!-- Модальное окно -->
  63. <div id="myModal" class="modal">
  64. <div class="modal-content">
  65. <span class="close" onclick="closeModal()">&times;</span>
  66. import javafx.application.Application;
  67. import javafx.scene.Scene;
  68. import javafx.scene.canvas.Canvas;
  69. import javafx.scene.canvas.GraphicsContext;
  70. import javafx.scene.control.Alert;
  71. import javafx.scene.control.Alert.AlertType;
  72. import javafx.scene.input.KeyCode;
  73. import javafx.scene.layout.VBox;
  74. import javafx.scene.paint.Color;
  75. import javafx.stage.Stage;
  76.  
  77. public class HangmanGame extends Application {
  78.  
  79. private static final String[] WORDS = {"JAVA", "PYTHON", "HTML", "CSS", "JAVASCRIPT"};
  80.  
  81. private String wordToGuess;
  82. private StringBuilder currentGuess;
  83. private int attemptsLeft;
  84. private boolean gameEnded;
  85.  
  86. @Override
  87. public void start(Stage primaryStage) {
  88. Canvas canvas = new Canvas(400, 400);
  89. GraphicsContext gc = canvas.getGraphicsContext2D();
  90.  
  91. resetGame();
  92.  
  93. canvas.setOnKeyPressed(event -> {
  94. if (!gameEnded && event.getCode().isLetterKey()) {
  95. char guessedLetter = event.getText().toUpperCase().charAt(0);
  96. boolean letterFound = false;
  97. for (int i = 0; i < wordToGuess.length(); i++) {
  98. if (wordToGuess.charAt(i) == guessedLetter) {
  99. currentGuess.setCharAt(i, guessedLetter);
  100. letterFound = true;
  101. }
  102. }
  103. if (!letterFound) {
  104. attemptsLeft--;
  105. drawHangman(gc);
  106. }
  107. drawWord(gc);
  108. if (attemptsLeft == 0 || !currentGuess.toString().contains("_")) {
  109. endGame(attemptsLeft == 0);
  110. }
  111. }
  112. });
  113.  
  114. VBox root = new VBox(canvas);
  115. Scene scene = new Scene(root);
  116. primaryStage.setScene(scene);
  117. primaryStage.setTitle("Hangman Game");
  118. primaryStage.show();
  119.  
  120. canvas.requestFocus();
  121. drawHangman(gc);
  122. drawWord(gc);
  123. }
  124.  
  125. private void resetGame() {
  126. wordToGuess = WORDS[(int) (Math.random() * WORDS.length)];
  127. currentGuess = new StringBuilder("_".repeat(wordToGuess.length()));
  128. attemptsLeft = 6;
  129. gameEnded = false;
  130. }
  131.  
  132. private void drawWord(GraphicsContext gc) {
  133. gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
  134. gc.setFill(Color.BLACK);
  135. gc.fillText(currentGuess.toString(), 150, 350);
  136. }
  137.  
  138. private void drawHangman(GraphicsContext gc) {
  139. gc.setStroke(Color.BLACK);
  140. gc.setLineWidth(2);
  141. if (attemptsLeft < 6) {
  142. gc.strokeOval(100, 50, 50, 50); // Head
  143. }
  144. if (attemptsLeft < 5) {
  145. gc.strokeLine(125, 100, 125, 200); // Body
  146. }
  147. if (attemptsLeft < 4) {
  148. gc.strokeLine(125, 120, 75, 100); // Left Arm
  149. }
  150. if (attemptsLeft < 3) {
  151. gc.strokeLine(125, 120, 175, 100); // Right Arm
  152. }
  153. if (attemptsLeft < 2) {
  154. gc.strokeLine(125, 200, 75, 250); // Left Leg
  155. }
  156. if (attemptsLeft < 1) {
  157. gc.strokeLine(125, 200, 175, 250); // Right Leg
  158. }
  159. }
  160.  
  161. private void endGame(boolean lost) {
  162. gameEnded = true;
  163. String message = lost ? "You lost! The word was: " + wordToGuess : "Congratulations! You won!";
  164. Alert alert = new Alert(AlertType.INFORMATION, message);
  165. alert.setTitle("Game Over");
  166. alert.setHeaderText(null);
  167. alert.showAndWait();
  168. resetGame();
  169. }
  170.  
  171. public static void main(String[] args) {
  172. launch(args);
  173. }
  174. }
  175.  
  176. <div id="javafx-container"></div>
  177. </div>
  178. </div>
  179.  
  180. </body>
  181. </html>
  182.  
Advertisement
Add Comment
Please, Sign In to add comment