Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

The following Java tutorial introduces the Java 2D API and demonstrates some of its powerful graphics capabilities, including shapes, strokes, fills, gradients, line styles, line joins, line end caps and general paths (which can be used to create your own two-dimensional shapes). This tutorial is intended for students and professionals who are already familiar with Java and Swing graphical user interface fundamentals. Download the examples for this tutorial here.

[Notes: This tutorial is an excerpt (Section 12.8) of Chapter 12, Graphics and Java 2D, from our best-selling textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. When you purchase this book you also get free access to the Web-based Java Multimedia Cyber Classroom, 6/e, for six months. The Cyber Classroom includes audio descriptions of the examples in Chapters 1-14, solutions to approximately one-half of the end-of-chapter exercises, interactive true/false self-assessment questions and a searchable Web-based e-book. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp. 697-706. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Introduction to the Java 2D API (Continued)

General Paths

Next we present a general path—a shape constructed from straight lines and complex curves. A general path is represented with an object of class GeneralPath (package java.awt.geom). The application of Fig. 12.31 and Fig. 12.32 demonstrates drawing a general path in the shape of a five-pointed star.


Fig. 12.31 Java 2D general paths.
     1    // Fig. 12.31: Shapes2JPanel.java
  2   // Demonstrating a general path.
     3
   import java.awt.Color;
     4
   import java.awt.Graphics;
     5
   import java.awt.Graphics2D;
     6
   import java.awt.geom.GeneralPath;
     7
   import java.util.Random;
     8
   import javax.swing.JPanel;
     9
     
     10
  public class Shapes2JPanel extends JPanel
     11
   {
     12
// draw general paths
  13 public void paintComponent( Graphics g )
   14     {
   15        super.paintComponent( g ); // call superclass's paintComponent
   16        Random random = new Random(); // get random number generator
   17     
   18        int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
   19        int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
   20     
   21        Graphics2D g2d = ( Graphics2D ) g;
   22        GeneralPath star = new GeneralPath(); // create GeneralPath object
   23     
   24        // set the initial coordinate of the General Path
   25        star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );      
   26     
   27        // create the star--this does not draw the star
   28        for ( int count = 1; count < xPoints.length; count++ )
   29        star.lineTo( xPoints[ count ], yPoints[ count ] );
   30     
   31        star.closePath(); // close the shape
   32     
   33        g2d.translate( 200, 200 ); // translate the origin to (200, 200)
   34     
   35        // rotate around origin and draw stars in random colors
   36        for ( int count = 1; count <= 20; count++ )
   37        {
   38           g2d.rotate( Math.PI / 10.0 ); // rotate coordinate system
   39     
   40           // set random drawing color
   41           g2d.setColor( new Color( random.nextInt( 256 ),
   42           random.nextInt( 256 ), random.nextInt( 256 ) ) );
   43     
   44           g2d.fill( star ); // draw filled star
   45        } // end for
   46     } // end method paintComponent
   47  } // end class Shapes2JPanel

Page 1 | 2 | 3 | 4 | 5 | 6

Tutorial Index