/Users/lyon/j4p/src/j2d/graphics/AffineExample.java

1    package j2d.graphics; 
2     
3    import gui.ClosableJFrame; 
4     
5    import java.awt.*; 
6    import java.awt.geom.AffineTransform; 
7    import java.awt.geom.GeneralPath; 
8    import java.awt.geom.Point2D; 
9     
10   public class AffineExample extends ClosableJFrame { 
11       public static void main(String args[]) { 
12           new AffineExample(); 
13       } 
14    
15       AffineExample() { 
16           super("AffineFrame"); 
17           setSize(200, 200); 
18           show(); 
19       } 
20    
21       public void paint(Graphics g) { 
22           Graphics2D g2 = (Graphics2D) g; 
23           Point2D location = new Point2D.Float(20, 30); 
24           Point2D location1 = new Point2D.Float(40, 50); 
25           drawArrow(g2, 2, 2, location, theta); 
26           theta = theta + 0.1; 
27       } 
28    
29       private void drawArrow(Graphics2D g2, 
30                              float sx, float sy, 
31                              Point2D location, 
32                              double angle) { 
33           GeneralPath gp = getArrow(); 
34           AffineTransform at = new AffineTransform(); 
35           at.setToTranslation(location.getX(), location.getY()); 
36           at.scale(sx, sy); 
37           g2.setTransform(at); 
38           at.rotate(angle * Math.PI / 180, 10, 10); 
39           g2.setTransform(at); 
40           g2.draw(gp); 
41           //g2.setXORMode(getBackground()); 
42           //g2.draw(gp); 
43           //g2.setXORMode(getForeground()); 
44       } 
45    
46       double theta = 30; 
47    
48       private GeneralPath getArrow() { 
49           GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
50           gp.moveTo(0f, -15f); 
51           gp.lineTo(10f, 5f); 
52           gp.lineTo(5f, 5f); 
53           gp.lineTo(5f, 15f); 
54           gp.lineTo(-5, 15f); 
55           gp.lineTo(-5f, 5f); 
56           gp.lineTo(-10f, 5f); 
57           gp.closePath(); 
58           return gp; 
59       } 
60   } 
61