Deitel & Associates, Inc. Logo

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

© 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)

Fig. 12.32 Creating JFrame to display stars.
     1   // Fig. 12.32:
  // Demonstrating a general path.
  import java.awt.Color;
  import javax.swing.JFrame;
  public class Shapes2
  // execute application
  9  public static void main( String args[] )
   10     {
   11 // create frame for Shapes2JPanel
   12        JFrame frame = new JFrame( "Drawing 2D Shapes" );
   13        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   15        Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
   16        frame.add( shapes2JPanel ); // add shapes2JPanel to frame
   17        frame.setBackground( Color.WHITE ); // set frame background color
   18        frame.setSize( 400, 400 ); // set frame size
   19        frame.setVisible( true ); // display frame
   20     } // end main
   21  } // end class Shapes2  

    Lines 18–19 declare two int arrays representing the x- and y-coordinates of the points in the star. Line 22 creates GeneralPath object star. Line 25 uses GeneralPath method moveTo to specify the first point in the star. The for statement in lines 28–29 uses GeneralPath method lineTo to draw a line to the next point in the star. Each new call to lineTo draws a line from the previous point to the current point. Line 31 uses GeneralPath method closePath to draw a line from the last point to the point specified in the last call to moveTo. This completes the general path.

    Line 33 uses Graphics2D method translate to move the drawing origin to location (200, 200). All drawing operations now use location (200, 200) as (0, 0).

    The for statement in lines 36–45 draws the star 20 times by rotating it around the new origin point. Line 38 uses Graphics2D method rotate to rotate the next displayed shape. The argument specifies the rotation angle in radians (with 360° = 2p radians). Line 44 uses Graphics2D method fill to draw a filled version of the star.

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

Tutorial Index