روابط کو نئے ٹیب میں کھوليں
    • کام کی رپورٹ
    • ای میل
    • دوبارہ لکھیں
    • تقریر
    • عنوان جنریٹر
    • اسمارٹ جواب
    • نظم
    • مقالہ
    • لطیفہ
    • Instagram پوسٹ
    • X پوسٹ
    • Facebook پوسٹ
    • سٹوری
    • تعارفی خط
    • نصاب حیات
    • جاب کی تفصیل
    • سفارشی خط
    • استعفیٰ کا خط
    • دعوت کا خط
    • مبارکباد کا پیغام
    • مزید سانچے آزمائیں
  1. In Java, unlike C/C++ where graphics.h is used, drawing graphics is done using AWT or Swing with the Graphics or Graphics2D classes. The drawLine() method is the direct equivalent for drawing a line between two points.

    Below is a simple Swing example that draws a line on a window.

    Steps to Implement

    1. Create a Swing Component Extend JFrame or JPanel and override the paintComponent() (for JPanel) or paint() (for JFrame) method.

    2. Use Graphics/Graphics2D Call drawLine(x1, y1, x2, y2) to draw a line between two coordinates.

    3. Run the Application Use SwingUtilities.invokeLater() to ensure thread safety for GUI creation.

    Example Code

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

    public class LineDrawingExample extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    // Upgrade to Graphics2D for advanced features
    Graphics2D g2d = (Graphics2D) g;

    // Set color and stroke thickness
    g2d.setColor(Color.RED);
    g2d.setStroke(new BasicStroke(3));

    // Draw a simple line
    g2d.drawLine(50, 50, 250, 50);

    // Draw using Line2D for floating-point precision
    g2d.setColor(Color.BLUE);
    g2d.draw(new Line2D.Double(50.5, 100.5, 250.5, 100.5));
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
    JFrame frame = new JFrame("Draw Line Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new LineDrawingExample());
    frame.setSize(400, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    });
    }
    }
    نقل کر لیا گیا!
  2. Java Simple Line Drawing Program - Stack Overflow

    2 اکتوبر، 2011 · I want to create a simple java application for drawing only lines. My program is like that now; User can draw everything by draggin his mouse, but by the time he release his finger, I deleted

  3. Drawing lines examples with Java Graphics2D

    10 اگست، 2019 · In this Java graphics tutorial, you will learn how to draw lines with various code examples. A line is a graphics primitive that connects two points. In …

  4. How to Draw Lines in Java Using Graphics and AWT

    Learn how to draw lines in Java with Graphics and AWT. Step-by-step guide with code snippets and common mistakes to avoid.

  5. How to Draw a Line in Java - Delft Stack

    11 مارچ، 2025 · We will import the java.applet.Applet, java.awt and java.awt.event package from the library. The drawLine() method of the Graphics class is used to …

  6. Draw Lines in Java

    11 فروری، 2025 · This topic explains to draw lines in Java. It includes the environment configuration, step-by-step logic, and a working sample code for easy line drawing in Java.

  7. Class StdDraw - Princeton University

    The StdDraw class provides static methods for creating drawings with your programs. It uses a simple graphics model that allows you to create drawings consisting of points, lines, squares, circles, and …

  8. GitHub - MuhamedHekal/java-drawing-application: A simple ...

    A simple drawing application built using Java and AWT (Abstract Window Toolkit). This project allows users to draw shapes like lines, rectangles, ovals, and freehand sketches. It also includes an eraser …

  9. Mastering `drawLine` in Java: A Comprehensive Guide

    16 جنوری، 2026 · In Java, the Graphics class provides a set of methods for drawing various shapes, including lines. A Graphics object represents a graphics context, which is a destination for drawing …

  10. Creating Straight Lines in Java: A Step-by-Step Guide

    9 جولائی، 2024 · By creating instances of the LinesComponent class, specifying line coordinates, colors, and utilizing Swing components like JFrame and JButton, you can dynamically draw and clear lines …

اس سائٹ کا استعمال کر کے آپ تجزیات، ذاتی بنائے گئے مواد، اور اشتہارات کے لئے کوکیز کے استعمال سے اتفاق کرتے ہیں۔فریق ثالث کی کوکیز کے بارے میں مزید جانیں|Microsoft نجی حیثیت پالیسی