Draw Arc and Filled Arc in Java (Graphics Class)

In Java, we can draw shapes like arcs or semi-circles using Graphics Class methods drawArc() and fillArc().
जावा में Arc या Semi-Circle जैसी आकृतियाँ बनाने के लिए Graphics Class के drawArc() और fillArc() methods का उपयोग किया जाता है।


1️⃣ drawArc() Method

यह मेथड केवल arc की outline बनाता है।
This method only draws the outline of an arc.

Syntax / प्रारूप:

g.drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
  • x, y → वह पॉइंट जहां से rectangle का top-left corner शुरू होगा

  • width, height → arc को घेरने वाले rectangle की चौड़ाई और ऊँचाई

  • startAngle → arc कहाँ से शुरू होगा (0° = 3 o’clock direction)

  • arcAngle → arc का degree (positive = anticlockwise, negative = clockwise)


2️⃣ fillArc() Method

यह मेथड arc को color से भर देता है।
This method fills the arc with the current color.

Syntax / प्रारूप:

g.fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);

Example 1: Draw Simple Arc in Java Applet

import java.applet.Applet;
import java.awt.*;

public class ArcExample extends Applet {
    public void paint(Graphics g) {
        g.drawArc(50, 50, 100, 100, 0, 90);     // 1st arc
        g.drawArc(200, 50, 100, 100, 0, -90);   // 2nd arc
        g.drawArc(350, 50, 100, 100, 45, 180);  // 3rd arc
    }
}

Output:

  • पहला arc → 0° से 90° (anticlockwise)

  • दूसरा arc → 0° से -90° (clockwise)

  • तीसरा arc → 45° से 180°


Example 2: Filled Arc (Pie Shape)

import java.applet.Applet;
import java.awt.*;

public class FillArcExample extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillArc(50, 50, 150, 150, 0, 120);   // Red Pie
        g.setColor(Color.GREEN);
        g.fillArc(50, 50, 150, 150, 120, 120); // Green Pie
        g.setColor(Color.BLUE);
        g.fillArc(50, 50, 150, 150, 240, 120); // Blue Pie
    }
}

Output:

  • यह एक RGB Pie Chart तैयार करेगा, जो 3 रंगों के arc से बना होगा।


Example 3: Moving Fan using Arc

import java.awt.*;
import java.applet.*;

public class MovingFan extends Applet implements Runnable {
    int angle = 0;
    Thread t;

    public void init() {
        t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            while (true) {
                angle += 20;
                repaint();
                Thread.sleep(100);
            }
        } catch (Exception e) {
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillOval(100, 100, 200, 200); // fan boundary
        g.setColor(Color.BLUE);
        g.fillArc(100, 100, 200, 200, angle, 30);
        g.fillArc(100, 100, 200, 200, angle + 120, 30);
        g.fillArc(100, 100, 200, 200, angle + 240, 30);
    }
}

Output:

  • यह एक rotating fan animation तैयार करेगा।


⭐ Key Points to Remember

  • drawArc() केवल arc की outline बनाता है।

  • fillArc() arc को color fill करता है।

  • Angle की unit हमेशा degree होती है।

  • Animation के लिए Thread और repaint() का उपयोग किया जाता है।

Comments

Popular posts from this blog

Java program to implement file input stream class to read binary data from image file.

Java program to find length of given string and convert it in to upper or lower case.

Java program to read a text file and displays it's contents on screen.

Java program to check marks out of Bound exception.

Java program to calculate area and perimeter of circle using interfaces.

Java program to check greatest among three numbers where input numbers are given by user.

Java program to merge two text files in to third file.

Java Applet program to draw rectangle and right-aligned oval of orange color.

Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना