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

1    package j2d.graphics; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5    import java.awt.geom.AffineTransform; 
6     
7     
8    class HousePanel extends JPanel { 
9        public double rx = 0, ry = 0, scale = 0, theta = 0; 
10    
11       public HousePanel() { 
12       } 
13    
14       public void symbolValues() { 
15           java.awt.Dimension d = getSize(); 
16           scale = Math.random(); 
17           rx = Math.random() * d.width; 
18           ry = Math.random() * d.height; 
19           theta = Math.random() * 360; 
20    
21       } 
22    
23    
24       public void drawHouse(Graphics2D g) { 
25           g.setColor(Color.RED); 
26           g.drawRect(150, 150, 200, 200); 
27           g.setColor(Color.BLACK); 
28           g.drawRect(230, 275, 40, 75); 
29           g.setColor(Color.green); 
30           g.drawRect(175, 175, 50, 50); 
31           g.drawRect(275, 175, 50, 50); 
32           g.setColor(Color.blue); 
33           g.drawLine(150, 150, 250, 50); 
34           g.drawLine(250, 50, 350, 150); 
35       } 
36    
37       public void paint(Graphics g) { 
38           setBackground(Color.white); 
39           if (g instanceof Graphics2D) { 
40               System.out.println("g=" + g.getClass().getName()); 
41           } 
42           Graphics2D g2d = (Graphics2D) g; 
43           AffineTransform at = new AffineTransform(); 
44           for (int i = 0; i < 50; i++) { 
45               symbolValues(); 
46               at.setToTranslation(rx, ry); 
47               at.scale(scale, scale); 
48               at.rotate(theta * Math.PI / 180); 
49               g2d.setTransform(at); 
50               drawHouse(g2d); 
51           } 
52       } 
53   } 
54    
55   public class JHouseApp { 
56       public static void main(String args[]) { 
57           JFrame hf = new JFrame("AffineFrame"); 
58           hf.setSize(200, 200); 
59           hf.setBackground(Color.white); 
60           hf.getContentPane().add( 
61                   new HousePanel()); 
62           hf.show(); 
63       } 
64   } 
65