Java Reference
In-Depth Information
The application of Figs. 13.18-13.19 demonstrates drawing a variety of lines, rectan-
gles, three-dimensional rectangles, rounded rectangles and ovals. In Fig. 13.18, line 17
draws a red line, line 20 draws an empty blue rectangle and line 21 draws a filled blue rect-
angle. Methods fillRoundRect (line 24) and drawRoundRect (line 25) draw rectangles
with rounded corners. Their first two arguments specify the coordinates of the upper-left
corner of the bounding rectangle —the area in which the rounded rectangle will be drawn.
The upper-left corner coordinates are not the edge of the rounded rectangle, but the coor-
dinates where the edge would be if the rectangle had square corners. The third and fourth
arguments specify the width and height of the rectangle. The last two arguments deter-
mine the horizontal and vertical diameters of the arc (i.e., the arc width and arc height)
used to represent the corners.
Figure 13.20 labels the arc width, arc height, width and height of a rounded rectangle.
Using the same value for the arc width and arc height produces a quarter-circle at each
1
// Fig. 13.18: LinesRectsOvalsJPanel.java
2
// Drawing lines, rectangles and ovals.
3
import java.awt.Color;
4
import java.awt.Graphics;
5
import javax.swing.JPanel;
6
7
public class LinesRectsOvalsJPanel extends JPanel
8
{
9
// display various lines, rectangles and ovals
10
@Override
11
public void paintComponent(Graphics g)
12
{
13
super.paintComponent(g);
14
this .setBackground( Color.WHITE );
15
16
g.setColor( Color.RED );
17
g.drawLine( 5 , 30 , 380 , 30 );
18
19
g.setColor( Color.BLUE );
20
g.drawRect( 5 , 40 , 90 , 55 );
g.fillRect( 100 , 40 , 90 , 55 );
21
22
23
g.setColor( Color.CYAN );
24
g.fillRoundRect( 195 , 40 , 90 , 55 , 50 , 50 );
g.drawRoundRect( 290 , 40 , 90 , 55 , 20 , 20 );
25
26
27
g.setColor( Color.GREEN );
28
g.draw3DRect( 5 , 100 , 90 , 55 , true );
g.fill3DRect( 100 , 100 , 90 , 55 , false );
29
30
31
g.setColor( Color.MAGENTA) ;
32
g.drawOval( 195 , 100 , 90 , 55 );
g.fillOval( 290 , 100 , 90 , 55 );
33
34
}
35
} // end class LinesRectsOvalsJPanel
Fig. 13.18 | Drawing lines, rectangles and ovals.
 
Search WWH ::




Custom Search