AWT Line2D Class

The Line2D class states a line segment in (x,y) coordinate space.
Following is the declaration for java.awt.geom.Line2D class:
public abstract class Line2D
   extends Object
      implements Shape, Cloneable
protected Line2D() () 
 This is an abstract class that cannot be instantiated directly.
Object clone()  
Creates a new object of the same class as this object.
boolean contains(double x, double y) 
 Tests if a specified coordinate is inside the boundary of this Line2D.
boolean contains(double x, double y, double w, double h)  
Tests if the interior of this Line2D entirely contains the specified set of rectangular coordinates.
boolean contains(Point2D p)  
Tests if a given Point2D is inside the boundary of this Line2D.
boolean contains(Rectangle2D r)  
Tests if the interior of this Line2D entirely contains the specified Rectangle2D.
Rectangle getBounds()  
Returns an integer Rectangle that completely encloses the Shape.
abstract Point2D getP1()  
Returns the start Point2D of this Line2D.
abstract Point2D getP2()  
Returns the end Point2D of this Line2D.
PathIterator getPathIterator(AffineTransform at) 
Returns an iteration object that defines the boundary of this Line2D.
PathIterator getPathIterator(AffineTransform at, double flatness)  
Returns an iteration object that defines the boundary of this flattened Line2D.
abstract double getX1()  
Returns the X coordinate of the start point in double precision.
abstract double getX2()  
Returns the X coordinate of the end point in double precision.
abstract double getY1()  
Returns the Y coordinate of the start point in double precision.
abstract double getY2()  
Returns the Y coordinate of the end point in double precision.
boolean intersects(double x, double y, double w, double h)  
 Tests if the interior of the Shape intersects the interior of a specified rectangular area.
boolean intersects(Rectangle2D r)  
Tests if the interior of the Shape intersects the interior of a specified Rectangle2D.
boolean intersectsLine(double x1, double y1, double x2, double y2)  
Tests if the line segment from (x1,y1) to (x2,y2) intersects this line segment.
boolean intersectsLine(Line2D l)  
Tests if the specified line segment intersects this line segment.
static boolean linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)  
 Tests if the line segment from (x1,y1) to (x2,y2) intersects the line segment from (x3,y3) to (x4,y4).
double ptLineDist(double px, double py)  
 Returns the distance from a point to this line.
static double ptLineDist(double x1, double y1, double x2, double y2, double px, double py)
Returns the distance from a point to a line.
double ptLineDist(Point2D pt)
Returns the distance from a Point2D to this line.
double ptLineDistSq(double px, double py)
Returns the square of the distance from a point to this line.
static double ptLineDistSq(double x1, double y1, double x2, double y2, double px, double py)
Returns the square of the distance from a point to a line.
double ptLineDistSq(Point2D pt)
Returns the square of the distance from a specified Point2D to this line.
double ptSegDist(double px, double py)  
Returns the distance from a point to this line segment.
static double ptSegDist(double x1, double y1, double x2, double y2, double px, double py)
Returns the distance from a point to a line segment.
double ptSegDist(Point2D pt)  
Returns the distance from a Point2D to this line segment.
double ptSegDistSq(double px, double py)  
Returns the square of the distance from a point to this line segment.
static double ptSegDistSq(double x1, double y1, double x2, double y2, double px, double py)
Returns the square of the distance from a point to a line segment.
double ptSegDistSq(Point2D pt)  
Returns the square of the distance from a Point2D to this line segment.
int relativeCCW(double px, double py)  
Returns an indicator of where the specified point (px,py) lies with respect to this line segment.
static int relativeCCW(double x1, double y1, double x2, double y2, double px, double py)
Returns an indicator of where the specified point (px,py) lies with respect to the line segment from (x1,y1) to (x2,y2).

int relativeCCW(Point2D p)
Returns an indicator of where the specified Point2D lies with respect to this line segment.
abstract void setLine(double x1, double y1, double x2, double y2)  
Sets the location of the end points of this Line2D to the specified double coordinates.
void setLine(Line2D l)
Sets the location of the end points of this Line2D to the same as those end points of the specified Line2D.
void setLine(Point2D p1, Point2D p2)
Sets the location of the end points of this Line2D to the specified Point2D coordinates.
This class inherits methods from the following classes:
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

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

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Line2D shape = new Line2D.Double();
      shape.setLine(250D,250D,150D,150D);  
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Line2D.Line", 100, 120);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page