๐Ÿ”บ 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

What is a Web Browser? เคตेเคฌ เคฌ्เคฐाเค‰เคœ़เคฐ เค•्เคฏा เคนै?

Java's Support System เคœाเคตा เค•ा เคธเคนเคฏोเค—ी เคคंเคค्เคฐ

The Internet and Java เค‡ंเคŸเคฐเคจेเคŸ เค”เคฐ เคœाเคตा เค•ा เคธเคฎ्เคฌเคจ्เคง