Friday, August 1, 2025

๐ŸŽจ 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 เคเคช्เคฒीเค•ेเคถเคจ เค•े เคฒिเค เค†เค•เคฐ्เคทเค• เคฌเคจाเคคे เคนैं।

No comments:

Post a Comment

๐Ÿ“š 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...