这里,使用两种不同的方法使用 java 进行此设计。在这两种方法中,十字记号的大小均由用户决定。用户提供表格或框架尺寸的输入。卍字经常被用作使用不同语言(包括 java)学习行、列和表格布局概念的示例。
万字符是印度教、佛教和耆那教的宗教象征。

此处,使用三种不同的方法使用 Java 进行此设计。
给定的问题将通过三种不同的方法来解决。
通过使用两个“for 循环”。
立即学习“Java免费学习笔记(深入)”;
通过使用drawLine()方法。
通过使用 JavaFX 库。
列号和行号将由用户输入。卍字图案将放置在方形区域中。这 ”。”键盘上的字符用于制作图案。计算正方形的宽度的中点和高度的中点。使用两个“for 循环”以适当的间隙打印垂直和水平的点线。这种方法的局限性是线条不连续。
第 1 步 - 导入所需的库。
第 2 步 - 决定要绘制的图形的大小。
第 3 步 - 设置表格行和列。
第 4 步 - 使用“.”绘制万字符图案。性格。
第 5 步 - 显示结果。
import java.util.Scanner;
public class swastika{
public static void main (String[] args){
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the row and col value <Select a number between 10 to 50 > :- ");
n=sc.nextInt();
sc.close();
int row = n, col = n;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++){
if (i < row / 2){
if (j < col / 2){
if (j == 0)
System.out.print(".");
else
System.out.print(" "+ " ");
}
else if (j == col / 2)
System.out.print(" .");
else{
if (i == 0)
System.out.print(" .");
}
}
else if (i == row / 2)
System.out.print(". ");
else{
if (j == col / 2 || j == col - 1)
System.out.print(". ");
else if (i == row - 1){
if (j <= col / 2 || j == col - 1)
System.out.print(". ");
else
System.out.print(" "+ " ");
}
else
System.out.print(" "+" ");
}
}
System.out.print("\n");
}
};
}
import java.util.Scanner - 为了获取用户输入,这里导入了 Scanner 类。它是 java.util 包的一部分。
Scanner sc=new Scanner(System.in) - sc 是 Scanner 类的对象。 System.in 用于通过键盘获取用户输入。
sc.close() - 用于在完成工作后关闭扫描仪对象。
System.out.print() - 用于打印输出。

输出方法 1:十字记号
使用drawLine函数设置框架尺寸和十字记号设计。绘制六条线来制作卍字图案。 java.awt.Graphics和java.awt.Graphics2D用于制作绘图。这种方法的好处是线条是连续的。
第 1 步 - 导入所需的 java.awt.Graphics2D 库。
第 2 步 - 确定图形的大小。也可以输入图形尺寸作为输入。
第 3 步 - 设置符号绘图的框架大小。
第 4 步 - 绘制十字记号。绘制从一点到另一点的连续线。
第 5 步 - 显示结果。
import java.util.Scanner - 组件是可以显示在屏幕上并且用户可以与其交互的对象。例如,按钮。
java.awt.Graphics2D - s这是 Java 中 2D 绘图所需的类。此类扩展了原始 Graphics 类并提供了附加功能和控件。
drawLine() - drawLine(int x1, int y1, int x2, int y2) 用于指定两个点 (x1, y1) 和 (x2, y2)加入形成一条线。
注意 - 在命令提示符中按control+C 可关闭显示框架窗口。
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Scanner;
public class swastika_draw {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of frame <Seclect from 300 to 700> :- ");
n=sc.nextInt();
sc.close();
Frame frame = new Frame();
frame.add(new CustomPaintComponent());
int frameWidth = n;
int frameHeight = n;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
}
static class CustomPaintComponent extends Component {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
int x1 = 10;
int y1 = 10;
int x2 = x1;
int y2 = getSize().height/2;
int x3 = getSize().width - 10;
int y3 = y2;
int x4= x3;
int y4= getSize().height -10;
int x5 = x3;
int x6 = getSize().width/2;
int y5= y1;
int y6=y1;
int x7=x6;
int y7=y4;
int x8=x1;
int y8=y7;
g2d.drawLine(x1, y1, x2, y2);
g2d.drawLine(x2, y2, x3, y3);
g2d.drawLine(x3, y3, x4, y4);
g2d.drawLine(x5, y5, x6, y6);
g2d.drawLine(x6, y6, x7, y7);
g2d.drawLine(x7, y7, x8, y8);
}
}
}

输出:十字记号绘图
安装 JAVAFX 库。 javafx.scene.shape.Line 和 javafx.scene.Group 用于制作绘图。设置场景的大小。 Swastika 的设计是使用 Line 函数完成的。将六条线绘制到场景中以形成纳粹标志设计。这种方法的好处是线条是连续的。
第 1 步 - 导入所需的 javafx 库。
第 2 步 - 确定场景的大小。
第 3 步 - 设置场景的画布大小。
第 4 步 - 绘制十字记号。绘制从一点到另一点的连续线。将所有行添加到组中。将小组放入场景中。
第 5 步 - 显示结果。
Line() - Line(int x1, int y1, int x2, int y2) 用于指定两个点 (x1, y1) 和 (x2, y2)加入形成一条线。
注意 - 对于使用javafx,将其安装在单独的目录中并生成run.bat。
run.bat 文件的内容 -
javac --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml %1.java
java --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml %1
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class swastika_draw1 extends Application{
//swastika app starts here..
@Override
public void start(Stage stg1) {
int x1 = 10;
int y1 = 10;
int x2 = x1;
int y2 = 500/2;
int x3 = 500 - 10;
int y3 = y2;
int x4= x3;
int y4= 500 -10;
int x5 = x3;
int x6 = 500/2;
int y5= y1;
int y6=y1;
int x7=x6;
int y7=y4;
int x8=x1;
int y8=y7;
//Creating swastika now....
Line ln1= new Line(x1, y1, x2, y2);
Line ln2= new Line(x2, y2, x3, y3);
Line ln3= new Line(x3, y3, x4, y4);
Line ln4= new Line(x5, y5, x6, y6);
Line ln5= new Line(x6, y6, x7, y7);
Line ln6= new Line(x7, y7, x8, y8);
Group grp1 = new Group();
adding all lines to grp1
grp1.getChildren().add(ln1);
grp1.getChildren().add(ln2);
grp1.getChildren().add(ln3);
grp1.getChildren().add(ln4);
grp1.getChildren().add(ln5);
grp1.getChildren().add(ln6);
//Creating a Scene canvas
Scene swastika_canvas = new Scene(grp1, 500, 500);
//Set the title of the scene canvas
stg1.setTitle("Swastika Example using JavaFx");
//Adding the swastika_canvas to the stg
stg1.setScene(swastika_canvas);
//Displaying Swastika now...
stg1.show();
}
//main method starts ...
public static void main(String args[]){
launch(args);
}
}
C:\java\javaprgstu>run.bat swastika_draw1 C:\java\javaprgstu>javac --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml swastika_draw1.java C:\java\javaprgstu>java --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml swastika_draw1

输出:使用 javafx 绘制纳粹十字记号
本文中,通过Java语言使用这三种方法来制作卍字图案。第一种方法将图形打印为以表格格式展开的点。另一种方法是使用绘图表单,使用 java.awt.Graphics2D 库使用连续线绘制卍字。第三种方法使用 javafx 库将线条绘制到场景中。
以上就是Java程序通过从用户输入打印卐(卍)符号的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号