Java Reference
In-Depth Information
Table 9.11
Useful Methods of Graphics2D Objects
Method
Description
Rotates subsequently drawn items by the given angle in
radians with respect to the origin
rotate(angle)
Adjusts the size of any subsequently drawn items by the
given factors (1.0 means equal size)
scale(sx, sy)
Gives a slant to any subsequently drawn items
shear(shx, shy)
Shifts the origin by ( dx , dy ) in the current coordinate system
translate(dx, dy)
Table 9.11 lists some of Graphics2D 's extra methods.
The following program demonstrates Graphics2D . The rotate method's parame-
ter is an angle of rotation measured in radians instead of degrees. Rather than memo-
rizing the conversion between degrees and radians, we can use a static method from
the Math class called toRadians that converts a degree value into the equivalent
radian value:
1 // Draws a picture of rotating squares using Graphics2D.
2
3 import java.awt.*;
4
5 public class FancyPicture {
6 public static void main(String[] args) {
7 DrawingPanel panel = new DrawingPanel(250, 220);
8 Graphics2D g2 = panel.getGraphics();
9 g2.translate(100, 120);
10 g2.fillRect(-5, -5, 10, 10);
11
12 for ( int i = 0; i <= 12; i++) {
13 g2.setColor(Color.BLUE);
14 g2.fillRect(20, 20, 20, 20);
15
16 g2.setColor(Color.BLACK);
17 g2.drawString("" + i, 20, 20);
18
19 g2.rotate(Math.toRadians(30));
20 g2.scale(1.1, 1.1);
21 }
22 }
23 }
Figure 9.3 shows the program's output.
 
Search WWH ::




Custom Search