The Graphics class in Java is an essential part of the AWT (Abstract Windowing Toolkit), used for drawing shapes, texts, and images in GUI components or Applets.
ग्राफ़िक्स क्लास जावा के AWT पैकेज का एक महत्वपूर्ण भाग है, जिसका प्रयोग शेप्स, टेक्स्ट और इमेज को GUI कॉम्पोनेन्ट या एप्लेट में ड्रॉ करने के लिए किया जाता है।
This class provides predefined methods to draw lines, rectangles, circles, arcs, polygons, and also allows setting colors and fonts.
यह क्लास पूर्व-परिभाषित मेथड्स प्रदान करती है जिनकी मदद से हम लाइन, रेक्टेंगल, सर्कल, आर्क, पॉलिगॉन बना सकते हैं और कलर और फॉन्ट्स भी सेट कर सकते हैं।
The drawing is usually done inside the paint() method, which automatically gets called when the component is rendered.
ड्रॉइंग सामान्यतः paint() मेथड में की जाती है, जिसे कॉम्पोनेन्ट के रेंडर होते समय ऑटोमैटिकली कॉल किया जाता है।
import java.awt.Graphics;
🖌️ Commonly Used Methods of Graphics Class ग्राफ़िक्स क्लास के सामान्य मेथड्स
| Method |
Description |
विवरण |
drawLine(int x1, int y1, int x2, int y2) |
Draws a line between two points. |
दो बिंदुओं के बीच लाइन बनाता है |
drawRect(int x, int y, int width, int height) |
Draws rectangle outline. |
केवल रेक्टेंगल की बॉर्डर बनाता है |
fillRect(int x, int y, int width, int height) |
Draws filled rectangle. |
भरा हुआ रेक्टेंगल बनाता है |
drawOval(int x, int y, int width, int height) |
Draws an oval or circle outline. |
अंडाकार या सर्कल की बॉर्डर बनाता है |
fillOval(int x, int y, int width, int height) |
Draws filled oval or circle. |
भरा हुआ अंडाकार या सर्कल बनाता है |
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) |
Draws arc of given angles. |
दिए गए कोण का आर्क बनाता है |
drawPolygon(int[] xPoints, int[] yPoints, int nPoints) |
Draws polygon with n points. |
n बिंदुओं वाला पॉलिगॉन बनाता है |
setColor(Color c) |
Sets the current drawing color. |
ड्रॉइंग का रंग सेट करता है |
setFont(Font f) |
Sets the current font for text. |
टेक्स्ट के लिए फॉन्ट सेट करता है |
drawString(String str, int x, int y) |
Draws a text string. |
स्क्रीन पर स्ट्रिंग ड्रॉ करता है |
🧪 Example 1: Drawing Shapes using Graphics Class ग्राफ़िक्स क्लास द्वारा शेप्स बनाना
import java.awt.*;
import javax.swing.*;
class GraphicsExample extends JFrame {
GraphicsExample() {
setSize(500, 500);
setTitle("Graphics Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
// Set color for shapes
g.setColor(Color.RED);
g.drawLine(50, 50, 200, 50); // Line
g.drawRect(50, 70, 100, 50); // Rectangle outline
g.fillRect(200, 70, 100, 50); // Filled rectangle
g.setColor(Color.BLUE);
g.drawOval(50, 150, 100, 100); // Circle outline
g.fillOval(200, 150, 100, 100); // Filled circle
g.setColor(Color.GREEN);
g.drawArc(50, 270, 100, 100, 0, 180); // Semi-circle
}
public static void main(String[] args) {
new GraphicsExample();
}
}
Output: A window showing line, rectangle, circle, and arc in different colors.
आउटपुट: एक विंडो जिसमें लाइन, रेक्टेंगल, सर्कल और आर्क अलग-अलग रंगों में दिखेंगे।
🧪 Example 2: Drawing Polygon and Text पॉलिगॉन और टेक्स्ट बनाना
import java.awt.*;
import javax.swing.*;
class PolygonTextExample extends JFrame {
PolygonTextExample() {
setSize(500, 500);
setTitle("Polygon & Text Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
// Polygon
int[] x = {100, 150, 200, 150, 100};
int[] y = {300, 250, 300, 350, 350};
g.setColor(Color.MAGENTA);
g.drawPolygon(x, y, 5);
// Text with font
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("Hello Graphics!", 100, 450);
}
public static void main(String[] args) {
new PolygonTextExample();
}
}
Output: A pentagon polygon and a bold text message “Hello Graphics!” will appear.
आउटपुट: एक पेंटागन पॉलिगॉन और बोल्ड टेक्स्ट मैसेज “Hello Graphics!” दिखाई देगा।
🎯 Key Points for Java Graphics Programming जावा ग्राफ़िक्स प्रोग्रामिंग के मुख्य बिंदु
-
Graphics drawing is coordinate-based (x, y) with origin at top-left corner (0,0).
ग्राफिक्स ड्रॉइंग कोऑर्डिनेट आधारित (x, y) होती है, जिसका origin टॉप-लेफ्ट कोने (0,0) पर होता है।
-
Always use paint() or paintComponent() for drawing; avoid drawing in main().
हमेशा paint() या paintComponent() का प्रयोग करें; main() में ड्रॉइंग न करें।
-
For dynamic graphics or animations, call repaint() to refresh the window.
डायनामिक ग्राफिक्स या एनीमेशन के लिए, विंडो को रिफ्रेश करने हेतु repaint() कॉल करें।
-
Colors and Fonts make your graphics attractive for GUI applications.
रंग और फॉन्ट आपके ग्राफिक्स को GUI एप्लीकेशन के लिए आकर्षक बनाते हैं।