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)

Fig. 12.29 Java 2D shapes.
   1  // Fig. 12.29: ShapesJPanel.java
2 // Demonstrating some Java 2D shapes.
3
import java.awt.Color;
4
import java.awt.Graphics;
5
import java.awt.BasicStroke;
6
import java.awt.GradientPaint;
7
import java.awt.TexturePaint;
8
import java.awt.Rectangle;
9
import java.awt.Graphics2D;
10
import java.awt.geom.Ellipse2D;
11
import java.awt.geom.Rectangle2D;
12
import java.awt.geom.RoundRectangle2D;
13
import java.awt.geom.Arc2D;
14
import java.awt.geom.Line2D;
15
import java.awt.image.BufferedImage;
16
import javax.swing.JPanel;
17

18
public class ShapesJPanel extends JPanel
19
{
20
// draw shapes with Java 2D API
21
public void paintComponent( Graphics g )
22
{
23
super.paintComponent( g ); // call superclass's paintComponent
24

25
Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D
26

27
// draw 2D ellipse filled with a blue-yellow gradient
28
g2d.setPaint( new GradientPaint( 5, 30, Color.BLUE, 35, 100,
29
Color.YELLOW, true ) );
30
g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) );
31

32
// draw 2D rectangle in red
33
g2d.setPaint( Color.RED );
34
g2d.setStroke( new BasicStroke( 10.0f ) );
35
g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) );
36

37
// draw 2D rounded rectangle with a buffered background
38
BufferedImage buffImage = new BufferedImage( 10, 10,
39
BufferedImage.TYPE_INT_RGB );
40

41
// obtain Graphics2D from bufferImage and draw on it
42
Graphics2D gg = buffImage.createGraphics();
43
gg.setColor( Color.YELLOW ); // draw in yellow
44
gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
45
gg.setColor( Color.BLACK ); // draw in black
46
gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
47
gg.setColor( Color.BLUE ); // draw in blue
48
gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
49
gg.setColor( Color.RED ); // draw in red
50
gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
51

52
// paint buffImage onto the JFrame
53
g2d.setPaint( new TexturePaint( buffImage,
54
new Rectangle( 10, 10 ) ) );
55
g2d.fill(
56
new RoundRectangle2D.Double( 155, 30, 75, 100, 50, 50 ) );
57

58
// draw 2D pie-shaped arc in white
59
g2d.setPaint( Color.WHITE );
60
g2d.setStroke( new BasicStroke( 6.0f ) );
61
g2d.draw(
62
new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
63

64
// draw 2D lines in green and yellow
65
g2d.setPaint( Color.GREEN );
66
g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
67

68
// draw 2D line using stroke
69
float dashes[] = { 10 }; // specify dash pattern
70
g2d.setPaint( Color.YELLOW );
71
g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND,
72
BasicStroke.JOIN_ROUND, 10, dashes, 0 ) );
73
g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
74
} // end method paintComponent
75
} // end class ShapesJPanel

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

Tutorial Index