Skip to Content
Java 2D Graphics
book

Java 2D Graphics

by Jonathan Knudsen
May 1999
Intermediate to advanced
368 pages
9h 6m
English
O'Reilly Media, Inc.
Content preview from Java 2D Graphics

3.3. Lines and Curves

The 2D API includes shape classes that represent straight and curved line segments. These classes all implement the Shape interface, so they can be rendered and manipulated like any other Shape. Although you could create a single straight or curved line segment yourself using GeneralPath, it's easier to use these canned shape classes. It's interesting that these classes are Shapes, even though they represent the basic segment types that make up a Shape's path.

3.3.1. Line2D

The java.awt.geom.Line2D class represents a line whose coordinates can be retrieved as doubles. Like Point2D, Line2D is abstract. Subclasses can store coordinates in any way they wish. Figure 3.11 shows the hierarchy.

Figure 3.11. Line2D family of classes

Line2D includes several setLine() methods you can use to set a line's endpoints:

public abstract void setLine(double x1, double x2, double y1, double y2)

This method sets the endpoints of the line to x1, y1, and x2, y2.

public void setLine(Point2D p1, Point2D p2)

This method sets the endpoints of the line to p1 and p2.

public void setLine(Line2D l)

This method sets the endpoints of the line to be the same as the endpoints of the given line.

Here are the constructors for the Line2D.Float class. Two of them allow you to specify the endpoints of the line, which saves you the trouble of calling setLine().

public Line2D.Float()

This constructor ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Computer Graphics for Java Programmers, Second Edition

Computer Graphics for Java Programmers, Second Edition

Leen Ammeraal, Kang Zhang
Java Swing

Java Swing

Robert Eckstein, Marc Loy, Dave Wood
Java Extreme Programming Cookbook

Java Extreme Programming Cookbook

Eric M. Burke, Brian M. Coyner

Publisher Resources

ISBN: 1565924843Catalog PageErrata