Chapter 6.3.5 – Graphics 2D | Introduction to Programming Using Java

Chapter 6.3.5 – Graphics 2D | Introduction to Programming Using Java

 

6.3.5 Graphics 2D

 

java Chapter 6.3.5 - Graphics 2D | Introduction to Programming Using Java

 

All drawing in Java is done through an object of type Graphics. The Graphics class provides basic commands for such things as drawing shapes and text and for selecting a drawing color.

These commands are adequate in many cases, but they fall far short of what’s needed in a serious computer graphics program. Java has another class, Graphics2D, that provides a larger set of drawing operations. Graphics2D is a sub-class of Graphics, so all the methods from the Graphics class are also available in a Graphics2D.

The paintComponent() method of a JComponent gives you a graphics context of type Graphics that you can use for drawing on the component. In fact, the graphics context actually belongs to the sub-class Graphics2D (in Java version 1.2 and later), and can be type-cast to gain access to the advanced Graphics2D drawing methods:

 

Chapter 6.3.5 - Graphics 2D | Introduction to Programming Using Java

 

Drawing in Graphics2D is based on shapes, which are objects that implement an interface named Shape. Shape classes include Line2D, Rectangle2D, Ellipse2D, Arc2D, and CubicCurve2D, among others; all these classes are defined in the package java.awt.geom. CubicCurve2D can be used to draw Bezier Curves, which are used in many graphics programs. Graphics2D has methods draw(Shape) and fill(Shape) for drawing the outline of a shape and for filling its interior.

 

Chapter 6.3.5 - Graphics 2D | Introduction to Programming Using Java

 

Advanced capabilities include: lines that are more than one pixel thick, dotted and dashed lines, filling a shape with a texture (this is, with a repeated image), filling a shape with a gradient, and drawing translucent objects that will blend with their background.

In the Graphics class, coordinates are specified as integers and are based on pixels. The shapes that are used with Graphics2D use real numbers for coordinates, and they are not necessarily bound to pixels. In fact, you can change the coordinate system and use any coordinates that are convenient to your application. In computer graphics terms, you can apply a “transformation” to the coordinate system. The transformation can be any combination of translation, scaling, and rotation.

I mention Graphics2D here for completeness. I will not use any of the advanced capabilities of Graphics2D in this chapter, but I will cover a few of them in Section 12.2.

 

graphics 2d

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

1 thought on “Chapter 6.3.5 – Graphics 2D | Introduction to Programming Using Java”

Leave a Comment