๐Ÿ”บ Draw Polygon เค”เคฐ Polyline in Java (Graphics Class)

Polygon เค”เคฐ Polyline เคœाเคตा เค—्เคฐाเฅžिเค•्เคธ เคช्เคฐोเค—्เคฐाเคฎिंเค— เคฎें เคเคธे shapes เคนैं เคœिเคจเคฎें เค•เคˆ connected lines เคนोเคคी เคนैं।

In Java Graphics programming, Polygons and Polylines are shapes formed using multiple connected lines.

  • Polygon (เคฌเคนुเคญुเคœ) → Closed shape (เคถुเคฐुเค†เคค เค”เคฐ เค…ंเคค เคœुเฅœे เคนोเคคे เคนैं)

  • Polyline (เคชॉเคฒीเคฒाเค‡เคจ) → Open shape (เคถुเคฐुเค†เคค เค”เคฐ เค…ंเคค เคœुเฅœे เคจเคนीं เคนोเคคे)


1️⃣ Polygon in Java

เคœाเคตा เคฎें polygon เคฌเคจाเคจे เค•े เคฒिเค Graphics Class เค•े drawPolygon() เค”เคฐ fillPolygon() methods เค•ा เค‰เคชเคฏोเค— เคนोเคคा เคนै।
To draw polygons in Java, we use Graphics Class methods drawPolygon() and fillPolygon().

Syntax / เคช्เคฐाเคฐूเคช:

g.drawPolygon(int xPoints[], int yPoints[], int nPoints);
g.fillPolygon(int xPoints[], int yPoints[], int nPoints);
  • xPoints[] → X-axis เค•े เคธเคญी points

  • yPoints[] → Y-axis เค•े เคธเคญी points

  • nPoints → เค•िเคคเคจे points polygon เคฎें เคนोंเค—े


Example 1: Draw a Triangle

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

public class PolygonExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {100, 150, 50};
        int y[] = {50, 150, 150};
        g.setColor(Color.RED);
        g.drawPolygon(x, y, 3);
    }
}

Output:

  • เคเค• Red Triangle เคธ्เค•्เคฐीเคจ เคชเคฐ เคฆिเค–ाเคˆ เคฆेเค—ा।


Example 2: Filled Polygon (Pentagon)

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

public class FillPolygonExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {100, 150, 200, 175, 125};
        int y[] = {150, 100, 150, 200, 200};
        g.setColor(Color.BLUE);
        g.fillPolygon(x, y, 5);
    }
}

Output:

  • เคฏเคน เคเค• Blue Filled Pentagon เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐेเค—ा।


2️⃣ Polyline in Java

Polyline, polygon เค•ी เคคเคฐเคน เคนोเคคी เคนै เคฒेเค•िเคจ open shape เคนोเคคी เคนै।
A polyline is like a polygon but remains open (does not connect last point to the first).

Syntax / เคช्เคฐाเคฐूเคช:

g.drawPolyline(int xPoints[], int yPoints[], int nPoints);

Example 3: Draw Polyline

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

public class PolylineExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {50, 100, 150, 200, 250};
        int y[] = {200, 150, 100, 150, 200};
        g.setColor(Color.MAGENTA);
        g.drawPolyline(x, y, 5);
    }
}

Output:

  • เคเค• V-shape wave line (zigzag) เคธ्เค•्เคฐीเคจ เคชเคฐ เคฌเคจेเค—ी।


⭐ Key Points to Remember

  • Polygon เคนเคฎेเคถा closed shape เคนोเคคी เคนै।

  • Polyline open shape เคนोเคคी เคนै।

  • fillPolygon() polygon เค•ो color fill เค•เคฐเคคा เคนै।

  • Points arrays เค•ा size เคนเคฎेเคถा nPoints เค•े เคฌเคฐाเคฌเคฐ เคนोเคจा เคšाเคนिเค।

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 เคœाเคตा เคฎें เคซ़ाเค‡เคฒों เค•ो เคœोเคก़เคจा เค”เคฐ เคฌเคซเคฐ เค•เคฐเคจा