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