Java Program to Print Swastika Sign By Taking User Input
A Java program to print the Swastika sign by taking user input involves soliciting the desired size of the symbol from the user and then generating the Swastika pattern accordingly.
Here are different approaches used for making this design using Java,
Approach: Using Two Loops
Algorithm
Step 1: Obtain the size of the Swastika pattern from the user.
Step 2: Use nested loops to traverse each row and column of the pattern.
Step 3: Determine the character to be printed for each position in the pattern based on its location and the defined conditions.
FileName: SwastikaUsingLoops.java
// Java program to print a Swastika pattern based on user input size import java.util.Scanner; public class SwastikaUsingLoops { // Main method to execute the program public static void main(String[] args) { // Scanner object for user input Scanner sc = new Scanner(System.in); System.out.print("Enter the size of Swastika: "); int size = sc.nextInt(); // Input size of Swastika sc.close(); // Closing scanner // Nested loops to iterate through each row and column // Prints Swastika pattern based on specified conditions for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i < size / 2) { if (j < size / 2) { if (j == 0) System.out.print("."); else System.out.print(" "); } else if (j == size / 2) System.out.print(" ."); else { if (i == 0) System.out.print(" ."); } } else if (i == size / 2) System.out.print(". "); else { if (j == size / 2 || j == size - 1) System.out.print(". "); else if (i == size - 1) { if (j <= size / 2 || j == size - 1) System.out.print(". "); else System.out.print(" "); } else System.out.print(" "); } } System.out.println(); } } }
Output:
![Java Program to Print Swastika Sign By Taking User Input](https://static.tutorialandexample.com/java/java-program-to-print-swastika-sign-by-taking-user-input1.png)
Complexity Analysis: The time complexity of this Swastika pattern printing program is O(n^2), where n is the size of the Swastika and the space complexity is O(1).
Approach: Using Java Swing
Algorithm
Step 1: Initialize a frame to display the Swastika pattern using the AWT library.
Step 2: Declare variables width and height to store the dimensions of the frame.
Step 3: Prompt the user to input the desired width and height of the frame using a dialog box.
Step 4: Validate the user's input and handle cases of invalid or missing input.
Step 5: Override the paint() method to draw the Swastika pattern on the frame using Graphics2D.
Step 6: Set the size of the frame according to the user-defined or default width and height.
Step 7: Make the frame visible to the user to display the Swastika pattern.
FileName: SwastikaUsingDrawLine.java
import java.awt.*; import javax.swing.*; public class SwastikaUsingDrawLine extends Frame { private int width; private int height; public SwastikaUsingDrawLine(int width, int height) { this.width = width; this.height = height; setSize(width, height); setLocationRelativeTo(null); // Center the frame on the screen setVisible(true); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; int x1 = width / 10, y1 = height / 10; // Adjust the position int x2 = x1, y2 = height / 2; int x3 = width - x1, y3 = y2; int x4 = x3, y4 = height - y1; int x5 = x3, x6 = width / 2; int y5 = y1, y6 = y1; int x7 = x6, y7 = y4; int x8 = x1, 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); } public static void main(String[] args) { int width = 500; // Default width int height = 500; // Default height // Create a dialog to get width and height input from the user JTextField widthField = new JTextField("500"); JTextField heightField = new JTextField("500"); JPanel inputPanel = new JPanel(new GridLayout(2, 2)); inputPanel.add(new JLabel("Width:")); inputPanel.add(widthField); inputPanel.add(new JLabel("Height:")); inputPanel.add(heightField); int result = JOptionPane.showConfirmDialog(null, inputPanel, "Enter Width and Height", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { try { width = Integer.parseInt(widthField.getText()); height = Integer.parseInt(heightField.getText()); } catch (NumberFormatException e) { System.out.println("Invalid input. Using default width and height."); } } new SwastikaUsingDrawLine(width, height); } }
Output:
![Java Program to Print Swastika Sign By Taking User Input](https://static.tutorialandexample.com/java/java-program-to-print-swastika-sign-by-taking-user-input3.png)
Complexity Analysis: The time complexity of this Swastika pattern printing program is O(width + height), and the space complexity is O(width * height).