Newer
Older
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
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* CS 349 Java Code Examples
*
* CompositionOrder Demo of different concatenation orders of matrix transforms.
* Click the window to change the order.
*
*/
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.*;
import java.awt.geom.*;
import java.lang.Math.*;
import java.awt.event.*;
// create the window and run the demo
public class TransformHittest {
public static void main(String[] args) {
// create the window
Canvas canvas = new Canvas();
JFrame f = new JFrame("CompositionOrder"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 700); // window size
f.setContentPane(canvas); // add canvas to jframe
f.setBackground(Color.WHITE);
f.setVisible(true); // show the window
}
}
class Canvas extends JComponent {
Point M = new Point(); // mouse point
AffineTransform AT1;
AffineTransform AT2;
// the house shape (model position is centred at top left corner)
private Polygon shape = new Polygon(new int[] { -50, 50, 50, 0, -50},
new int[] { 75, 75, -25, -75, -25}, 5);
// a larger font for displaying the concatenation order
private Font font = new Font("SansSerif", Font.PLAIN, 30);
// the concatenation order
private int order = 0;
Canvas() {
// create transformation matrices
AT1 = new AffineTransform();
AT1.translate(350, 100);
AT1.rotate(Math.toRadians(30));
AT1.scale(1, 1);
// create another transformation matrices
AT2 = new AffineTransform();
AT2.translate(200, 300);
AT2.rotate(Math.toRadians(30));
AT2.scale(2, 2);
this.addMouseMotionListener(new MouseAdapter(){
public void mouseMoved(MouseEvent e){
M.x = e.getX();
M.y = e.getY();
repaint();
}
});
System.out.println("click to change transformation composition order");
}
// custom graphics drawing
public void paintComponent(Graphics g) {
super.paintComponent(g); // JPanel paint
Graphics2D g2 = (Graphics2D)g;
// save the current transform matrix
AffineTransform ATG = g2.getTransform();
// draw the original shape in "model" coordinates
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(3));
g2.drawPolygon(shape.xpoints, shape.ypoints, shape.npoints);
// Transformed Shape 1
g2.setTransform(AT1); // Use Transform Matrix AT1 for shape1
g2.setColor(Color.RED);
g2.drawPolygon(shape.xpoints, shape.ypoints, shape.npoints);
// hit test
Point MT = new Point();
try{
AffineTransform IAT1 = AT1.createInverse();
IAT1.transform(M, MT);
if (shape.contains(MT.x, MT.y))
g2.setColor(Color.RED);
else
g2.setColor(Color.WHITE);
} catch (NoninvertibleTransformException e){
// error
}
g2.fillPolygon(shape);
// Transformed Shape 1
g2.setTransform(AT2); // Use Transform Matrix AT2 for shape2
g2.setColor(Color.BLUE);
g2.drawPolygon(shape.xpoints, shape.ypoints, shape.npoints);
// hit test
try{
AffineTransform IAT2 = AT2.createInverse();
IAT2.transform(M, MT);
if (shape.contains(MT.x, MT.y))
g2.setColor(Color.BLUE);
else
g2.setColor(Color.WHITE);
} catch (NoninvertibleTransformException e){
// error
}
g2.fillPolygon(shape);
// reset to transform
g2.setTransform(ATG);
}
}