Java Graphics: Drawing a Line with drawLine() जावा ग्राफ़िक्स: drawLine() मेथड से लाइन ड्रा करना
Java provides the Graphics class in the AWT package to draw 2D shapes like lines, rectangles, circles, and polygons.
जावा AWT पैकेज में Graphics क्लास प्रदान करता है, जिसका उपयोग 2D शेप जैसे लाइन, रेक्टेंगल, सर्कल और पॉलिगन बनाने के लिए किया जाता है।
The drawLine() method of the Graphics class is used to draw a straight line between two points.
Graphics क्लास का drawLine() मेथड दो बिंदुओं के बीच एक सीधी रेखा खींचने के लिए प्रयोग किया जाता है।
📌 Syntax | सिंटैक्स
void drawLine(int x1, int y1, int x2, int y2);
-
x1, y1 → starting point of the line.
x1, y1 → लाइन का शुरुआती बिंदु। -
x2, y2 → ending point of the line.
x2, y2 → लाइन का अंतिम बिंदु।
💻 Example: Drawing a Line in Applet ऐपलेट में लाइन ड्रा करना
import java.applet.Applet;
import java.awt.*;
public class DrawLineExample extends Applet {
public void paint(Graphics g) {
g.setColor(Color.RED); // Set line color
g.drawLine(10, 20, 200, 100); // Draw line from (10,20) to (200,100)
}
}
🌐 HTML file to run the applet एप्लेट चलाने के लिए HTML फाइल
<html>
<body>
<applet code="DrawLineExample.class" width="300" height="200">
</applet>
</body>
</html>
🎯 Output | आउटपुट
-
A red line will be drawn from point
(10,20)
to(200,100)
.
(10,20)
से(200,100)
तक एक लाल रंग की लाइन प्रदर्शित होगी।
📝 Key Points | मुख्य बातें
-
drawLine()
can be used multiple times to make shapes like polygons or stars.
drawLine()
का प्रयोग बार-बार करके बहुभुज या स्टार जैसी आकृतियाँ बनाई जा सकती हैं। -
Coordinates
(x1, y1, x2, y2)
are relative to the applet or frame’s top-left corner(0,0)
.
कॉर्डिनेट्स(x1, y1, x2, y2)
ऐपलेट या फ्रेम के टॉप-लेफ्ट कॉर्नर(0,0)
से लिए जाते हैं। -
Colors can be set using
g.setColor(Color.colorName)
.
रंगg.setColor(Color.colorName)
द्वारा सेट किया जा सकता है।
Comments
Post a Comment