Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outline
28.1
28.2
28.3
28.4
28.5
28.6
28.7
28.8
28.9
Introduction
Graphics Contexts and Graphics Objects
Color Control
Font Control
Drawing Lines, Rectangles and Ovals
Drawing Arcs
Drawing Polygons and Polylines
The Java2D API
Java2D Shapes
28.1 Introduction
In this chapter
Draw 2D shapes
Colors
Fonts
x - horizontal axis
y - vertical axis
Class Component
In this chapter
Class Container
JColorChooser
// Demonstrating Colors
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
1. import
6
7
8
public ShowColors()
10
11
12
13
show();
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
28
29
g.setColor( Color.blue );
30
31
32
33
34
Color c = Color.magenta;
35
g.setColor( c );
36
37
38
39
Outline
1.2 Define paint
40
41
42
43
44
45
app.addWindowListener(
46
new WindowAdapter() {
47
48
49
System.exit( 0 );
50
51
52
53
}
);
}
54 } 2000 Prentice Hall, Inc. All rights reserved.
1.3 main
Outline
Program Output
getStyle()
getSize()
getName()
getFamily()
isPlain()
isBold()
isItalic()
getFont()
setFont(Font f)
leading
height
Xy1
ascent
baseline
descent
getAscent()
getDescent()
getLeading()
getHeight()
getFontMetrics()
getFontMetrics( Font f )
// Using fonts
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
6
7
public Fonts()
10
11
12
13
show();
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
Font Example
1. import
1.1 Constructor
1.2 paint
28
29
30
31
32
33
34
35
g.setColor( Color.red );
36
g.setFont(
37
38
39
g.getFont().getSize() +
40
41
42
43
44
45
46
47
app.addWindowListener(
48
new WindowAdapter() {
49
50
51
System.exit( 0 );
52
53
54
55
}
);
}
56 } 2000 Prentice Hall, Inc. All rights reserved.
Outline
1.2 paint
1.3 main
Outline
Program Output
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
7
8
9
10
11
12
13
14
show();
15
16
17
18
19
20
FontMetrics fm = g.getFontMetrics();
21
22
23
24
25
26
Outline
FontMetrics Example
1. import
1.1 Constructor
1.2 paint
27
28
fm = g.getFontMetrics( font );
29
g.setFont( font );
30
31
32
33
34
35
36
37
38
39
40
41
app.addWindowListener(
42
new WindowAdapter() {
43
44
45
System.exit( 0 );
46
47
48
49
);
}
Outline
1.2 paint
1.3 main
Outline
Program Output
(x, y)
drawRoundRect
parameters
arc height
arc width
height
width
(x, y)
drawOval parameters
height
width
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
g.setColor( Color.cyan );
g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
Outline
Lines, Rectangles, and
Ovals Example
1. import
1.1 Constructor
1.2 paint
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 }
g.setColor( Color.yellow );
g.draw3DRect( 5, 100, 90, 55, true );
g.fill3DRect( 100, 100, 90, 55, false );
g.setColor( Color.magenta );
g.drawOval( 195, 100, 90, 55 );
g.fillOval( 290, 100, 90, 55 );
}
Outline
1.2 paint
1.3 main
Outline
Program Output
// Drawing arcs
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
6
7
public DrawArcs()
10
11
12
13
show();
14
15
16
17
18
19
g.setColor( Color.yellow );
20
21
g.setColor( Color.black );
22
23
24
25
g.setColor( Color.yellow );
26
27
g.setColor( Color.black );
28
29
Outline
1. import
1.1 Constructor
1.2 paint
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 }
g.setColor( Color.yellow );
g.drawRect( 185, 35, 80, 80 );
g.setColor( Color.black );
g.drawArc( 185, 35, 80, 80, 0, -270 );
Outline
1.2 paint
1.3 main
Outline
Program Output
fillPolygon( myPolygon )
Draws specified solid polygon
myPolygon.addPoint( x, y )
// Drawing polygons
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1. import
1.1 Constructor
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public DrawPolygons()
{
super( "Drawing Polygons" );
setSize( 275, 230 );
show();
}
public void paint(
{
int xValues[] =
int yValues[] =
Polygon poly1 =
Graphics g )
{ 20, 40, 50, 30, 20, 15 };
{ 50, 50, 60, 80, 80, 60 };
new Polygon( xValues, yValues, 6 );
g.drawPolygon( poly1 );
int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 };
int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 };
g.drawPolyline( xValues2, yValues2, 7 );
int xValues3[] = { 120, 140, 150, 190 };
int yValues3[] = { 40, 70, 80, 60 };
2000 Prentice Hall, Inc. All rights reserved.
Outline
1.2 paint
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 }
new Polygon();
165, 135 );
175, 150 );
270, 200 );
200, 220 );
130, 180 );
g.fillPolygon( poly2 );
}
public static void main( String args[] )
{
DrawPolygons app = new DrawPolygons();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
Outline
1.2 paint
1.3 main
Outline
Program Output
setStroke( strokeObject )
Class BufferedImage
Can produce images in color or grayscale
Can create patterns by drawing into the BufferedImage
object
Class TexturePaint
Class BasicStroke
Method lineTo
Draws a line to next point in general path
Method closePath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Outline
1. import
1.1 Constructor
1.2 paint
1.3 Cast to
Graphics2D
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Outline
1.4 Notice use of
Java2D
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 }
Outline
1.4 Notice use of
Java2D
Outline
Program Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outline
Java2D Example
1. import
1.1 Constructor
1.2 paint
1.3 Graphics2D object
1.4 GeneralPath
object
1.5 Add points to
object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
2000 Prentice Hall, Inc. All rights reserved.
Outline
1.5 Add points to
object
1.6 translate
1.7 rotate
1.8 Randomize colors
2. main
Outline
Program Output