๐บ 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
Post a Comment