Java2D Shapes
Program
2 // Demonstrating some Java2D shapes
3 import javax.swing.*;
4 import java.awt.event.*;
5 import java.awt.*;
6 import java.awt.geom.*;
7 import java.awt.image.*;
89
public class Shapes extends JFrame {
10 public Shapes()
11 {
12 super( "Drawing 2D shapes" );
13
14 setSize( 425, 160 );
15 show();
16 }
17
18 public void paint( Graphics g )
19 {
20 // create 2D by casting g to Graphics2D
21 Graphics2D g2d = ( Graphics2D ) g;
22
23 // draw 2D ellipse filled with a blue-yellow gradient
24 g2d.setPaint(
25 new GradientPaint( 5, 30, // x1, y1
26 Color.blue, // initial Color
27 35, 100, // x2, y2
28 Color.yellow, // end Color
29 true ) ); // cyclic
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(
36 new Rectangle2D.Double( 80, 30, 65, 100 ) );
37
38 // draw 2D rounded rectangle with a buffered background
39 BufferedImage buffImage =
40 new BufferedImage(
41 10, 10, BufferedImage.TYPE_INT_RGB );
42
43 Graphics2D gg = buffImage.createGraphics();
44 gg.setColor( Color.yellow ); // draw in yellow
45 gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
46 gg.setColor( Color.black ); // draw in black
47 gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
48 gg.setColor( Color.blue ); // draw in blue
49 gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
50 gg.setColor( Color.red ); // draw in red
51 gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
53 // paint buffImage onto the JFrame
54 g2d.setPaint(
55 new TexturePaint(
56 buffImage, new Rectangle( 10, 10 ) ) );
57 g2d.fill(
58 new RoundRectangle2D.Double(
59 155, 30, 75, 100, 50, 50 ) );
60
61 // draw 2D pie-shaped arc in white
62 g2d.setPaint( Color.white );
63 g2d.setStroke( new BasicStroke( 6.0f ) );
64 g2d.draw(
65 new Arc2D.Double(
66 240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
67
68 // draw 2D lines in green and yellow
69 g2d.setPaint( Color.green );
70 g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
71
72 float dashes[] = { 10 };
73
74 g2d.setPaint( Color.yellow );
75 g2d.setStroke(
76 new BasicStroke( 4,
77 BasicStroke.CAP_ROUND,
78 BasicStroke.JOIN_ROUND,
79 10, dashes, 0 ) );
80 g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
81 }
82 public static void main( String args[] )
84 {
85 Shapes app = new Shapes();
86
87 app.addWindowListener(
88 new WindowAdapter() {
89 public void windowClosing( WindowEvent e )
90 {
91 System.exit( 0 );
92 }
93 }
94 );
95 }
96 }
Buy It Now
- Next, we present several java2D shapes from package java.awt.geom, including
Ellipse2D.Double, Rectangle2D.Double, RoundRectangle2D.Double,
Arc2D.Double and Line2D.Double. - Note the syntax of each class name. Each of these classes represents a shape with dimensions specified as double-precision floatingpoint
values. - There is a separate version of each represented with single-precision floatingpoint values (such as Ellipse2D.Float).
- In each case, Double is a static inner
class of the class to the left of the dot operator (e.g., Ellipse2D). - To use the static inner class, we simply qualify its name with the outer class name.
Program
2 // Demonstrating some Java2D shapes
3 import javax.swing.*;
4 import java.awt.event.*;
5 import java.awt.*;
6 import java.awt.geom.*;
7 import java.awt.image.*;
89
public class Shapes extends JFrame {
10 public Shapes()
11 {
12 super( "Drawing 2D shapes" );
13
14 setSize( 425, 160 );
15 show();
16 }
17
18 public void paint( Graphics g )
19 {
20 // create 2D by casting g to Graphics2D
21 Graphics2D g2d = ( Graphics2D ) g;
22
23 // draw 2D ellipse filled with a blue-yellow gradient
24 g2d.setPaint(
25 new GradientPaint( 5, 30, // x1, y1
26 Color.blue, // initial Color
27 35, 100, // x2, y2
28 Color.yellow, // end Color
29 true ) ); // cyclic
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(
36 new Rectangle2D.Double( 80, 30, 65, 100 ) );
37
38 // draw 2D rounded rectangle with a buffered background
39 BufferedImage buffImage =
40 new BufferedImage(
41 10, 10, BufferedImage.TYPE_INT_RGB );
42
43 Graphics2D gg = buffImage.createGraphics();
44 gg.setColor( Color.yellow ); // draw in yellow
45 gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
46 gg.setColor( Color.black ); // draw in black
47 gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
48 gg.setColor( Color.blue ); // draw in blue
49 gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
50 gg.setColor( Color.red ); // draw in red
51 gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
53 // paint buffImage onto the JFrame
54 g2d.setPaint(
55 new TexturePaint(
56 buffImage, new Rectangle( 10, 10 ) ) );
57 g2d.fill(
58 new RoundRectangle2D.Double(
59 155, 30, 75, 100, 50, 50 ) );
60
61 // draw 2D pie-shaped arc in white
62 g2d.setPaint( Color.white );
63 g2d.setStroke( new BasicStroke( 6.0f ) );
64 g2d.draw(
65 new Arc2D.Double(
66 240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
67
68 // draw 2D lines in green and yellow
69 g2d.setPaint( Color.green );
70 g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
71
72 float dashes[] = { 10 };
73
74 g2d.setPaint( Color.yellow );
75 g2d.setStroke(
76 new BasicStroke( 4,
77 BasicStroke.CAP_ROUND,
78 BasicStroke.JOIN_ROUND,
79 10, dashes, 0 ) );
80 g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
81 }
82 public static void main( String args[] )
84 {
85 Shapes app = new Shapes();
86
87 app.addWindowListener(
88 new WindowAdapter() {
89 public void windowClosing( WindowEvent e )
90 {
91 System.exit( 0 );
92 }
93 }
94 );
95 }
96 }
Buy It Now
0 comments:
Post a Comment