Home > Java > javaTutorial > Java program to print swastika (swastika) symbol by taking input from user

Java program to print swastika (swastika) symbol by taking input from user

WBOY
Release: 2023-08-30 10:01:03
forward
1445 people have browsed it

Here, two different approaches are used to do this design using Java. In both methods, the size of the swastika is determined by the user. The user provides input for table or frame dimensions. Swastika is often used as an example for learning row, column, and table layout concepts using different languages, including java.

The swastika is a religious symbol of Hinduism, Buddhism and Jainism.

Java program to print swastika (swastika) symbol by taking input from user

Use Java to create swastikas.

Here, three different approaches are used to do this design using Java.

Multiple methods

The given problem will be solved by three different methods.

  • By using two "for loops".

  • By using the drawLine() method.

  • By using the JavaFX library.

Method 1: Use two “for loops”

Column and row numbers will be entered by the user. The swastika pattern will be placed in the square area. The "." characters on the keyboard are used to make patterns. Calculate the midpoint of the width and midpoint of the height of the square. Use two "for loops" to print vertical and horizontal dotted lines with appropriate gaps. The limitation of this method is that the lines are not continuous.

Algorithm 1

  • Step 1 - Import the required libraries.

  • Step 2 - Decide on the size of the shape you want to draw.

  • Step 3 - Set up table rows and columns.

  • Step 4 - Draw the swastika pattern using "." character.

  • Step 5 - Display the results.

Example (Method 1)

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");
      }
   };
}
Copy after login

illustrate

  • import java.util.Scanner - In order to obtain user input, the Scanner class is imported here. It is part of the java.util package.

  • Scanner sc=new Scanner(System.in) - sc is an object of Scanner class. System.in is used to get user input via keyboard.

  • sc.close() - Used to close the scanner object after completing its work.

  • System.out.print() - Used for printing output.

Output (Method 1)

Java program to print swastika (swastika) symbol by taking input from user

Output method 1: Swastika

Method 2:- By using drawLine() method

Use the drawLine function to set the frame size and swastika design. Draw six lines to make the swastika pattern. java.awt.Graphics and java.awt.Graphics2D are used to make drawings. The advantage of this method is that the lines are continuous.

algorithm

  • Step 1 - Import the required java.awt.Graphics2D library.

  • Step 2 - Determine the size of the graphic. Graphic dimensions can also be entered as input.

  • Step 3 - Set the frame size of the symbol drawing.

  • Step 4 - Draw the swastika. Draw a continuous line from one point to another.

  • Step 5 - Display the results.

illustrate

  • import java.util.Scanner - A component is an object that can be displayed on the screen and with which the user can interact. For example, buttons.

  • java.awt.Graphics2D - sThis is the class required for 2D drawing in Java. This class extends the original Graphics class and provides additional functionality and controls.

  • drawLine() - drawLine(int x1, int y1, int x2, int y2) is used to specify two points (x1, y1) and (x2, y2) to join to form a line Wire.

  • Note - Press control C in the command prompt to close the display frame window.

Example (Method 2)

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);
      }
   }
}
Copy after login

Output (Method 2)

Java program to print swastika (swastika) symbol by taking input from user

Output: swastika drawing

Method 3:- Using JavaFX Library

Install JAVAFX library. javafx.scene.shape.Line and javafx.scene.Group are used to make drawings. Set the size of the scene. The design of Swastika is done using Line function. Draw six lines into the scene to form the swastika design. The advantage of this method is that the lines are continuous.

algorithm

  • Step 1 - Import the required javafx libraries.

  • Step 2 - Determine the size of the scene.

  • Step 3 - Set the canvas size of the scene.

  • Step 4 - Draw the swastika. Draw a continuous line from one point to another. Add all rows to the group. Place the group into the scene.

  • Step 5 - Display the results.

illustrate

  • Line() - Line(int x1, int y1, int x2, int y2) is used to specify two points (x1, y1) and (x2, y2) to join to form a line Wire.

  • Note - For using javafx, install it in a separate directory and generate run.bat.

  • Contents of run.bat file -

    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

示例(方法 3)

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);
   }
}
Copy after login

如何使用javafx运行程序? (方法解释)

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
Copy after login

输出(方法 3)

Java program to print swastika (swastika) symbol by taking input from user

输出:使用 javafx 绘制纳粹十字记号

结论

本文中,通过Java语言使用这三种方法来制作卍字图案。第一种方法将图形打印为以表格格式展开的点。另一种方法是使用绘图表单,使用 java.awt.Graphics2D 库使用连续线绘制卍字。第三种方法使用 javafx 库将线条绘制到场景中。

The above is the detailed content of Java program to print swastika (swastika) symbol by taking input from user. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template