Showing posts with label java graphics applet shapes. Show all posts
Showing posts with label java graphics applet shapes. Show all posts

Friday, August 1, 2025

Java Graphics – Drawing Rectangles & Squares (drawRect & fillRect) जावा ग्राफ़िक्स में Rectangles (आयत) और Squares (वर्ग) बनाना

Java Graphics provides predefined methods to draw shapes like rectangles and squares.

जावा ग्राफ़िक्स, रेक्टेंगल और स्क्वायर जैसी आकृतियाँ बनाने के लिए पूर्व परिभाषित मेथड प्रदान करता है।

Java Graphics class provides two important methods:
जावा ग्राफ़िक्स क्लास दो महत्वपूर्ण मेथड प्रदान करती है –

  1. drawRect(int x, int y, int width, int height)
    Draws an outline of a rectangle (only border).
    यह केवल रेक्टेंगल की आउटलाइन (बॉर्डर) बनाता है।

  2. fillRect(int x, int y, int width, int height)
    Draws a filled rectangle.
    यह भरे हुए रेक्टेंगल को बनाता है।

Parameters / पैरामीटर्स

  • x → Starting x-coordinate / प्रारंभिक x-निर्देशांक

  • y → Starting y-coordinate / प्रारंभिक y-निर्देशांक

  • width → Width of rectangle / रेक्टेंगल की चौड़ाई

  • height → Height of rectangle / रेक्टेंगल की ऊँचाई


🖥 Example 1: Draw Rectangle using drawRect()

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

public class DrawRectExample extends Applet {  
    public void paint(Graphics g) {  
        g.drawRect(50, 50, 150, 100); // Outline rectangle  
    }  
}  
/*  
<applet code="DrawRectExample.class" width="300" height="250">  
</applet>  
*/

This program will display an empty rectangle at position (50,50) with width 150 and height 100.
यह प्रोग्राम (50,50) पर खाली रेक्टेंगल दिखाएगा जिसकी चौड़ाई 150 और ऊँचाई 100 होगी।


🖥 Example 2: Draw Filled Rectangle using fillRect()

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

public class FillRectExample extends Applet {  
    public void paint(Graphics g) {  
        g.setColor(Color.BLUE);       // Set color to blue  
        g.fillRect(60, 60, 120, 80);  // Filled rectangle  
    }  
}  
/*  
<applet code="FillRectExample.class" width="300" height="250">  
</applet>  
*/

This program will display a filled blue rectangle on the applet window.
यह प्रोग्राम एक भरा हुआ नीला रेक्टेंगल प्रदर्शित करेगा।

यदि हम ऐसा आयत तैयार  करना चाहते है जिसके कोने गोलाकार हो तब हमें drawRoundRect() एवं fillRoundRect() मेथड का प्रयोग करना होता है। इन मेथड्स में दो अतिरिक्त आर्गुमेंट होते हैंजो आयत के कोनो की गोलाई तय करते हैं।

g.drawRoundRect(20,20,60,60,10,10);

g.fillRoundRect(120,20,60,60,20,20);


🟢 Key Notes / मुख्य बिंदु

  • Use drawRect() for outlines and fillRect() for solid shapes.
    drawRect() का प्रयोग बॉर्डर के लिए और fillRect() का प्रयोग भरे हुए आकृतियों के लिए करें।

  • Use setColor() to change the color of the shape.
    आकृति का रंग बदलने के लिए setColor() का उपयोग करें।

  • Coordinates (x, y) are for top-left corner of the rectangle.
    (x, y) रेक्टेंगल के ऊपरी-बाएँ कोने को दर्शाता है।

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 | मुख्य बातें

  1. drawLine() can be used multiple times to make shapes like polygons or stars.
    drawLine() का प्रयोग बार-बार करके बहुभुज या स्टार जैसी आकृतियाँ बनाई जा सकती हैं।

  2. Coordinates (x1, y1, x2, y2) are relative to the applet or frame’s top-left corner (0,0).
    कॉर्डिनेट्स (x1, y1, x2, y2) ऐपलेट या फ्रेम के टॉप-लेफ्ट कॉर्नर (0,0) से लिए जाते हैं।

  3. Colors can be set using g.setColor(Color.colorName).
    रंग g.setColor(Color.colorName) द्वारा सेट किया जा सकता है।

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...