The Basic Recipe for Rendering in Java 2D

A fundamental three-step recipe exists for programming graphics in Java. Get (or modify) the Graphics2D context, create some geometry (or an image), and call a rendering method. The following short code snippet demonstrates this recipe:

public void paint(Graphics g) {

//get instance of graphics context
Graphics2D g2d  = (Graphics2D) g;

//modify the graphics context
g2d.setColor(Color.blue);

//call a render method
g2d.fill(new Ellipse2D.Float(5.f,5.f, 40.f, 40.f,);

}

Several complete examples are developed in Chapter 3, “Graphics Programming with the Java 2D API.”

Get Java™ Media APIs: Cross-Platform Imaging, Media, and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.