Friday, August 1, 2025

Java Graphics (AWT) – Lamp Design Example Program

import java.awt.*;

public class Lamp extends java.applet.Applet {

    public void paint(Graphics g) {

        g.fillRect(0,250,290,290);        // Lamp platform

        g.drawLine(125,250,125,160);      // Lamp stand

        g.drawLine(175,250,175,160);


        g.drawArc(85,157,130,50,-65,312); // Bottom of shade

        g.drawArc(85,87,130,50,62,58);    // Top of shade

        g.drawLine(85,177,119,89);        // Left side

        g.drawLine(215,177,181,89);       // Right side


        g.fillArc(78,120,40,40,63,-174);  // Dots

        g.fillOval(120,96,40,40);

        g.fillArc(173,100,40,40,110,180);

    }

}


Java Swing – Introduction जावा स्विंग परिचय, Swing Core Components स्विंग के मुख्य अवयव

Swing, जावा का Modern GUI (Graphical User Interface) Toolkit है, जो प्लेटफॉर्म-इंडिपेंडेंट Desktop Applications बनाने के लिए प्रयोग किया जाता है। Swing is Java’s modern GUI toolkit used to create platform-independent desktop applications.


🖥️ Swing की विशेषताएँ / Features of Swing

  1. Lightweight Components
    Swing के सभी GUI components lightweight होते हैं और प्लेटफॉर्म पर निर्भर नहीं होते।
    All Swing components are lightweight and platform-independent.

  2. Rich Set of Components
    Swing में JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar आदि जैसे बहुत से advanced GUI components मौजूद हैं।
    Swing provides advanced GUI components like JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar, etc.

  3. Pluggable Look and Feel
    Swing एप्लिकेशन का look and feel change किया जा सकता है, जैसे Windows, Metal या Nimbus।
    Swing applications support changing their look and feel, e.g., Windows, Metal, Nimbus.

  4. MVC Architecture
    Swing components Model-View-Controller (MVC) architecture पर काम करते हैं।
    Swing components follow the Model-View-Controller (MVC) architecture.

  5. Event-Driven Programming
    Swing इवेंट ड्रिवेन होती है, यानी सभी actions जैसे button click, mouse hover, key press events से handle होते हैं।
    Swing is event-driven, meaning actions like button clicks, mouse hovers, and key presses are handled via events.


🏗️ Swing के Common Classes

  1. JFrame → Main Window बनाने के लिए।
    JFrame → Used to create the main window.

  2. JPanel → GUI components को रखने के लिए।
    JPanel → Used as a container for GUI components.

  3. JButton → Button create करने के लिए।
    JButton → Used to create buttons.

  4. JLabel → Text या Image display करने के लिए।
    JLabel → Used to display text or images.

  5. JTextField → Single line text input के लिए।
    JTextField → Used for single-line text input.


💻 पहला Swing Program – Basic JFrame

import javax.swing.*;
class FirstSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing Example");
frame.setSize(400, 300); // width, height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:

  • एक खाली 400x300 JFrame Window दिखाई देगी।


Key Points:

  • Swing classes javax.swing package में होती हैं।

  • JFrame पर components जोड़ने के लिए add() method का use करते हैं।

  • एप्लिकेशन को बंद करने के लिए setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) जरूरी है। 


🌟 Java Swing Core Components


1️⃣ JFrame – Main Window

JFrame is the main window in any Swing application. It acts as a container that holds all GUI components like buttons, labels, and text fields.

JFrame, किसी भी Swing एप्लिकेशन की मुख्य विंडो होती है। यह एक कंटेनर की तरह काम करती है, जिसमें बटन, लेबल और टेक्स्ट फील्ड जैसे सभी GUI components रखे जाते हैं।

Key Points:

  • JFrame is top-level container.

  • Use setSize(width, height) to define its size.

  • Use setVisible(true) to make it visible.

  • Close operation must be set with setDefaultCloseOperation().

Example:

import javax.swing.*;

class JFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My JFrame Example");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

2️⃣ JPanel – Sub Container

JPanel is a sub-container used inside a JFrame to organize GUI components. It helps in grouping elements and managing layouts.

JPanel, JFrame के अंदर प्रयोग होने वाला सब-कंटेनर है। इसका उपयोग elements को group करने और layout manage करने के लिए किया जाता है।

Key Points:

  • JPanel is invisible by default.

  • You can add multiple JPanels in a JFrame.

  • It supports layouts like FlowLayout, BorderLayout, GridLayout.

Example:

import javax.swing.*;
import java.awt.*;

class JPanelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JPanel Example");
        JPanel panel = new JPanel(); // create panel

        panel.add(new JButton("Click Me")); // add button in panel

        frame.add(panel);  // add panel to frame
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3️⃣ JButton – Button Component

JButton is a clickable button that performs an action when clicked.

JButton एक क्लिक करने योग्य बटन है, जिस पर क्लिक करने पर कोई कार्य (action) perform होता है।

Key Points:

  • Use new JButton("Text") to create a button.

  • Events are handled using ActionListener.

Example:

import javax.swing.*;
import java.awt.event.*;

class JButtonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JButton Example");
        JButton button = new JButton("Click Me");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button Clicked!");
            }
        });

        frame.add(button);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

4️⃣ JLabel – Display Text or Image

JLabel is used to display static text, image, or both in a Swing window.

JLabel का प्रयोग स्थिर टेक्स्ट, इमेज या दोनों को Swing विंडो में प्रदर्शित करने के लिए किया जाता है।

Key Points:

  • JLabel cannot take input; it is read-only.

  • Supports image icons with text.

Example:

import javax.swing.*;

class JLabelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JLabel Example");
        JLabel label = new JLabel("Welcome to Java Swing!");
        
        frame.add(label);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

5️⃣ JTextField – Single Line Input Field

JTextField is used to take single-line input from the user in a Swing application.

JTextField का उपयोग यूज़र से एक लाइन का इनपुट लेने के लिए किया जाता है।

Key Points:

  • Use new JTextField(columns) to create text field.

  • Use getText() to read input and setText() to set text.

Example:

import javax.swing.*;

class JTextFieldExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTextField Example");
        JTextField textField = new JTextField(20);
        
        frame.add(textField);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎯 Summary

Component Purpose / उद्देश्य
JFrame Main Window (मुख्य विंडो)
JPanel Sub-container for GUI components
JButton Clickable Button (क्लिक करने वाला बटन)
JLabel Display text or image
JTextField Take single-line input from user


Example:- Java Swing Stylish Login Form with Background Image

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class StylishLoginForm extends JFrame {
    StylishLoginForm() {
        // Frame Settings
        setTitle("Stylish Login Form");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);

        // Background Image
        JLabel background = new JLabel(new ImageIcon("background.jpg"));
        background.setBounds(0, 0, 500, 300);
        setContentPane(background);
        setLayout(null);

        // Font for Labels and TextFields
        Font font = new Font("Arial", Font.BOLD, 16);

        // Username Label
        JLabel userLabel = new JLabel("Username:");
        userLabel.setBounds(80, 70, 100, 30);
        userLabel.setForeground(Color.WHITE);
        userLabel.setFont(font);
        add(userLabel);

        // Username Field
        JTextField userText = new JTextField();
        userText.setBounds(200, 70, 200, 30);
        userText.setFont(font);
        add(userText);

        // Password Label
        JLabel passLabel = new JLabel("Password:");
        passLabel.setBounds(80, 120, 100, 30);
        passLabel.setForeground(Color.WHITE);
        passLabel.setFont(font);
        add(passLabel);

        // Password Field
        JPasswordField passText = new JPasswordField();
        passText.setBounds(200, 120, 200, 30);
        passText.setFont(font);
        add(passText);

        // Login Button
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(200, 180, 100, 35);
        loginButton.setBackground(new Color(30, 144, 255));
        loginButton.setForeground(Color.WHITE);
        loginButton.setFont(new Font("Arial", Font.BOLD, 16));
        add(loginButton);

        // Button Action
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passText.getPassword());

                if (username.equals("admin") && password.equals("1234")) {
                    JOptionPane.showMessageDialog(null, 
                        "Login Successful! Welcome " + username);
                } else {
                    JOptionPane.showMessageDialog(null, 
                        "Invalid Username or Password!",
                        "Login Error",
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        new StylishLoginForm();
    }
}

📝 How It Works / यह कैसे काम करता है

  1. Background image is applied using a JLabel with ImageIcon.

  2. Custom font & colors are used to make it attractive.

  3. Button color is changed using setBackground() and setForeground().

  4. JOptionPane shows messages on login success or failure.

🎨 Output Look

+---------------------------------------+
|     [ Background Image Visible ]       |
|  Username: [__________]                |
|  Password: [__________]                |
|               [ Login ]                |
+---------------------------------------+

🔺 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 के बराबर होना चाहिए।

Draw Arc and Filled Arc in Java (Graphics Class)

In Java, we can draw shapes like arcs or semi-circles using Graphics Class methods drawArc() and fillArc().
जावा में Arc या Semi-Circle जैसी आकृतियाँ बनाने के लिए Graphics Class के drawArc() और fillArc() methods का उपयोग किया जाता है।


1️⃣ drawArc() Method

यह मेथड केवल arc की outline बनाता है।
This method only draws the outline of an arc.

Syntax / प्रारूप:

g.drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
  • x, y → वह पॉइंट जहां से rectangle का top-left corner शुरू होगा

  • width, height → arc को घेरने वाले rectangle की चौड़ाई और ऊँचाई

  • startAngle → arc कहाँ से शुरू होगा (0° = 3 o’clock direction)

  • arcAngle → arc का degree (positive = anticlockwise, negative = clockwise)


2️⃣ fillArc() Method

यह मेथड arc को color से भर देता है
This method fills the arc with the current color.

Syntax / प्रारूप:

g.fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);

Example 1: Draw Simple Arc in Java Applet

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

public class ArcExample extends Applet {
    public void paint(Graphics g) {
        g.drawArc(50, 50, 100, 100, 0, 90);     // 1st arc
        g.drawArc(200, 50, 100, 100, 0, -90);   // 2nd arc
        g.drawArc(350, 50, 100, 100, 45, 180);  // 3rd arc
    }
}

Output:

  • पहला arc → 0° से 90° (anticlockwise)

  • दूसरा arc → 0° से -90° (clockwise)

  • तीसरा arc → 45° से 180°


Example 2: Filled Arc (Pie Shape)

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

public class FillArcExample extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillArc(50, 50, 150, 150, 0, 120);   // Red Pie
        g.setColor(Color.GREEN);
        g.fillArc(50, 50, 150, 150, 120, 120); // Green Pie
        g.setColor(Color.BLUE);
        g.fillArc(50, 50, 150, 150, 240, 120); // Blue Pie
    }
}

Output:

  • यह एक RGB Pie Chart तैयार करेगा, जो 3 रंगों के arc से बना होगा।


Example 3: Moving Fan using Arc

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

public class MovingFan extends Applet implements Runnable {
    int angle = 0;
    Thread t;

    public void init() {
        t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            while (true) {
                angle += 20;
                repaint();
                Thread.sleep(100);
            }
        } catch (Exception e) {
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillOval(100, 100, 200, 200); // fan boundary
        g.setColor(Color.BLUE);
        g.fillArc(100, 100, 200, 200, angle, 30);
        g.fillArc(100, 100, 200, 200, angle + 120, 30);
        g.fillArc(100, 100, 200, 200, angle + 240, 30);
    }
}

Output:

  • यह एक rotating fan animation तैयार करेगा।


Key Points to Remember

  • drawArc() केवल arc की outline बनाता है।

  • fillArc() arc को color fill करता है।

  • Angle की unit हमेशा degree होती है।

  • Animation के लिए Thread और repaint() का उपयोग किया जाता है।

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) द्वारा सेट किया जा सकता है।

🎨 Graphics Class in Java AWT जावा AWT में ग्राफ़िक्स क्लास

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 एप्लीकेशन के लिए आकर्षक बनाते हैं।

🎨 Java Graphics Programming with AWT 🎨 जावा में ग्राफ़िक्स प्रोग्रामिंग (AWT)

Java allows us to create interactive Graphical User Interfaces (GUIs) using Applets and AWT (Abstract Windowing Toolkit).

जावा हमें ग्राफिकल यूजर इंटरफेस (GUI) बनाने की सुविधा देता है, जो एप्लेट और AWT (एब्स्ट्रैक्ट विंडोइंग टूलकिट) की मदद से तैयार होते हैं।

AWT contains a collection of classes and components for building platform-independent GUI software.
AWT में क्लासेस और कॉम्पोनेन्ट्स का समूह होता है जिससे प्लेटफॉर्म-इंडिपेंडेंट GUI सॉफ्टवेयर बनाए जा सकते हैं।

These components include buttons, checkboxes, labels, text fields, dialogs, menus, fonts, colors, and also event-handling support.
इन कॉम्पोनेन्ट्स में बटन, चेकबॉक्स, लेबल, टेक्स्ट फील्ड, डायलॉग, मेन्यू, फॉन्ट, कलर और इवेंट-हैंडलिंग सपोर्ट शामिल है।


🏗️ AWT Class Hierarchy AWT क्लास हायरार्की

  1. Component (कॉम्पोनेन्ट)
    All AWT elements like Button, Label, TextField are Components.
    सभी AWT एलिमेंट जैसे बटन, लेबल, टेक्स्ट फील्ड आदि कॉम्पोनेन्ट कहलाते हैं।

  2. Container (कंटेनर)
    Holds other components and manages their layout.
    यह अन्य कॉम्पोनेन्ट्स को रखता है और उनके लेआउट को मैनेज करता है।

  3. Window (विंडो)
    A container without border or menu bar; created using Frame or Dialog.
    एक कंटेनर जो बॉर्डर और मेन्यू बार के बिना होता है; इसे Frame या Dialog से बनाया जाता है।

  4. Panel (पैनल)
    A simple container used to group components.
    साधारण कंटेनर जो कॉम्पोनेन्ट्स को ग्रुप करने के लिए प्रयोग होता है।

  5. Frame (फ्रेम)
    A top-level container with title bar, menu bar, and border, which can hold other AWT elements.
    शीर्ष स्तर का कंटेनर जिसमें टाइटल बार, मेन्यू बार और बॉर्डर होता है, और यह अन्य AWT एलिमेंट्स को रख सकता है।


⚡ Commonly Used AWT Methods जावा AWT में प्रयुक्त मेथड्स

Method Name Description विवरण
add(Component c) Adds a component to the container. कंटेनर में कॉम्पोनेन्ट जोड़ता है
setSize(int w, int h) Sets width and height of the component. कॉम्पोनेन्ट का आकार (चौड़ाई और ऊँचाई) सेट करता है
setLayout(LayoutManager m) Defines layout for the container. कंटेनर के लिए लेआउट परिभाषित करता है
setVisible(boolean status) Makes the component visible/invisible. कॉम्पोनेन्ट को दृश्य/अदृश्य बनाता है

🧪 Example: Creating a Simple AWT Window with Button बटन के साथ एक साधारण AWT विंडो बनाना

import java.awt.*;
class AWTExample extends Frame {
AWTExample() {
// Create a button
Button b = new Button("Click Here");
b.setBounds(50, 100, 100, 40); // x, y, width, height
// Add button to frame
add(b);
// Frame settings
setSize(400, 400); // Frame width and height
setLayout(null); // No layout manager
setVisible(true); // Make frame visible
}
public static void main(String args[]) {
new AWTExample();
}
}

Output: A 400x400 window with a Click Here button.
आउटपुट: 400x400 की एक विंडो जिसमें Click Here बटन होगा।


🎯 Notes on AWT Graphics Programming जावा AWT ग्राफ़िक्स प्रोग्रामिंग पर नोट्स

  • AWT is platform-independent but heavyweight because it depends on native OS components.
    AWT प्लेटफॉर्म-इंडिपेंडेंट है लेकिन हेवीवेट है क्योंकि यह नेटिव OS कॉम्पोनेन्ट्स पर निर्भर करता है।

  • For advanced GUI, Swing is preferred as it is lightweight and fully Java-based.
    एडवांस GUI के लिए Swing को प्राथमिकता दी जाती है क्योंकि यह लाइटवेट और पूरी तरह जावा-बेस्ड है।

  • Event Handling is important to make GUI interactive.
    GUI को इंटरैक्टिव बनाने के लिए इवेंट हैंडलिंग महत्वपूर्ण है।

Aligning Applet Display using align Attribute एप्लेट डिस्प्ले को align एट्रिब्यूट से संरेखित करना

The align attribute sets the alignment of the applet inside the HTML page.

align एट्रिब्यूट HTML पेज में एप्लेट को संरेखित करने का कार्य करता है।

Note: This attribute is not supported in HTML5.
नोट: यह एट्रिब्यूट HTML5 में समर्थित नहीं है।

✅ Syntax: प्रारूप:

<applet align="left | right | center | justify">

📌 Attribute Values: एट्रिब्यूट वैल्यू:

  • left: Aligns the applet to the left.
    left: एप्लेट को बाएँ संरेखित करता है।

  • right: Aligns the applet to the right.
    right: एप्लेट को दाएँ संरेखित करता है।

  • center: Centers the applet.
    center: एप्लेट को केंद्र में संरेखित करता है।

  • justify: Aligns the applet in both directions.
    justify: एप्लेट को दोनों दिशाओं में संरेखित करता है।


🧪 Example - Aligning Applet

<!DOCTYPE html>
<html>
<head><title>Applet Alignment</title></head>
<body>
<applet code="HelloStudents.class" width="200" height="100" align="right"></applet>

<h1>HTML applet align attribute</h1>
<h2>Hello Students</h2>
<p>It is a Java learning portal for you.</p>
</body>
</html>

Taking Input from User in Java Applet जावा एप्लेट में यूजर से इनपुट ग्रहण करना

Java Applets allow user input through components like TextFieldLabel, and Button.

जावा एप्लेट में यूजर इनपुट को प्राप्त करने के लिए TextFieldLabel, और Button जैसे कंपोनेंट्स का उपयोग किया जाता है।

To use them, we first create their objects and add them using the add() method.
इनका प्रयोग करने के लिए पहले इनके ऑब्जेक्ट बनाए जाते हैं और add() मेथड से एप्लेट में जोड़ा जाता है।

📌 Component Declaration Format कंपोनेंट डिक्लेरेशन प्रारूप:

TextField object_name = new TextField(size);  
Label label_object_name = new Label("label_name");  
Button button_object_name = new Button("button_name");

🔧 Example: Taking two inputs and displaying total दो इनपुट लेकर कुल मान प्रदर्शित करना

// File: ThreeControlApplet.java  
import java.awt.*;  
import java.applet.*;  

public class ThreeControlApplet extends Applet {  
    TextField txtBox1 = new TextField(10);  
    TextField txtBox2 = new TextField(10);  
    TextField txtTotal = new TextField(10);  

    Label lblLabel1 = new Label("First Value");  
    Label lblLabel2 = new Label("Second Value");  
    Label lblTotal = new Label("Total");  

    Button OKButton = new Button("Calculate");  

    public void init() {  
        add(lblLabel1);  
        add(txtBox1);  
        add(lblLabel2);  
        add(txtBox2);  
        add(lblTotal);  
        add(txtTotal);  
        add(OKButton);  
    }  
}

🧾 This applet displays input fields and a button, but you must add event handling using ActionListener for full interactivity.
🧾 यह एप्लेट इनपुट फील्ड्स और बटन तो दिखाता है, लेकिन पूरी इंटरैक्टिविटी हेतु ActionListener का प्रयोग आवश्यक है।

📚 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...