This JavaFX application displays Cyrillic mathematical formulas using the MathJax library and a WebView component. The application showcases how to render complex mathematical expressions along with Cyrillic text in a JavaFX desktop application.
- JDK 21 or later
- Maven (if using Maven for dependency management)
Add the following dependencies to your pom.xml
:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>21</version>
</dependency>
</dependencies>
Ensure your project structure looks like this:
CyrillicMathFormulaDisplay
├── pom.xml
└── src
└── main
└── java
└── MathFormulaDisplay.java
Create the MathFormulaDisplay.java
file with the following content:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class MathFormulaDisplay extends Application {
@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
String htmlContent = "<html>" +
"<head>" +
"<meta charset='UTF-8'>" +
"<script type='text/javascript' async " +
"src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js'>" +
"</script>" +
"</head>" +
"<body>" +
"<p>Вот формула: \\(\\int_0^\\infty x^2 dx\\)</p>" +
"<p>Кириллический текст и формула: \\(\\sum_{i=1}^n a_i = b\\)</p>" +
"</body>" +
"</html>";
webEngine.loadContent(htmlContent);
Scene scene = new Scene(webView, 800, 600);
primaryStage.setTitle("Cyrillic Math Formula Display");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The code is a JavaFX application that displays mathematical formulas and Cyrillic text using a WebView component. Here’s a breakdown of the key components:
- Extends
Application
: This class extends the JavaFXApplication
class, making it a JavaFX application.
-
WebView and WebEngine:
WebView webView = new WebView();
creates a WebView component for displaying web content.WebEngine webEngine = webView.getEngine();
gets the WebEngine associated with the WebView for loading content.
-
HTML Content:
- The
htmlContent
string contains the HTML content to be displayed in the WebView. It includes:<meta charset='UTF-8'>
to ensure Cyrillic characters are displayed correctly.- A
<script>
tag to load MathJax from a CDN for rendering LaTeX formulas. - Two paragraphs: one with a simple mathematical formula and another combining Cyrillic text and a formula.
- The
-
Loading HTML Content:
webEngine.loadContent(htmlContent);
loads the HTML content into the WebEngine.
-
Scene Setup:
Scene scene = new Scene(webView, 800, 600);
creates a new Scene with the WebView as the root node and sets its size to 800x600 pixels.primaryStage.setTitle("Cyrillic Math Formula Display");
sets the title of the primary stage.primaryStage.setScene(scene);
sets the Scene to be displayed on the primary stage.
-
Showing the Stage:
primaryStage.show();
makes the primary stage visible.
- Launch Application:
launch(args);
launches the JavaFX application.
-
Using Maven:
- Navigate to the project directory.
- Run the following command to compile and run the application:
mvn clean javafx:run
-
Using IDE:
- Import the project into your preferred IDE (e.g., IntelliJ IDEA, Eclipse).
- Ensure the JavaFX SDK is configured correctly in your IDE.
- Run the
MathFormulaDisplay
class.