-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffineTransformationen.java
180 lines (144 loc) · 6.46 KB
/
AffineTransformationen.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
public class AffineTransformationen extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private JTextField translateXInput;
private JTextField translateYInput;
private JTextField scaleXInput;
private JTextField scaleYInput;
private JTextField rotateAngleInput;
private JTextField skewXInput;
private JTextField skewYInput;
private boolean rectangleSelected = true; // on startup rechteck
private boolean circleSelected = false;
public AffineTransformationen() {
setTitle("Affine Transformationen");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
AffineTransform transform = new AffineTransform();
double translateX = Double.parseDouble(translateXInput.getText());
double translateY = Double.parseDouble(translateYInput.getText());
transform.translate(translateX, translateY);
double scaleX = Double.parseDouble(scaleXInput.getText());
double scaleY = Double.parseDouble(scaleYInput.getText());
transform.scale(scaleX, scaleY);
transform.rotate(Math.toRadians(Double.parseDouble(rotateAngleInput.getText())), centerX, centerY);
double skewX = Double.parseDouble(skewXInput.getText());
double skewY = Double.parseDouble(skewYInput.getText());
transform.shear(skewX, skewY);
if (rectangleSelected) {
// originales rechteck
int width = 100;
int height = 60;
int x = centerX - width / 2;
int y = centerY - height / 2;
g2d.setColor(Color.BLUE);
g2d.fillRect(x, y, width, height);
// transformiertes rechteck
Shape transformedRectangle = transform.createTransformedShape(new Rectangle(x, y, width, height));
g2d.setColor(Color.RED);
g2d.fill(transformedRectangle);
} else if (circleSelected) {
// original kreis
int diameter = 80;
int x = centerX - diameter / 2;
int y = centerY - diameter / 2;
g2d.setColor(Color.BLUE);
g2d.fillOval(x, y, diameter, diameter);
// transformierter kreis
Shape transformedCircle = transform
.createTransformedShape(new Ellipse2D.Double(x, y, diameter, diameter));
g2d.setColor(Color.RED);
g2d.fill(transformedCircle);
} else {
// original dreieck
Path2D.Double triangle = new Path2D.Double();
int size = 100;
triangle.moveTo(centerX - size / 2, centerY + size / 2);
triangle.lineTo(centerX + size / 2, centerY + size / 2);
triangle.lineTo(centerX, centerY - size / 2);
triangle.closePath();
g2d.setColor(Color.BLUE);
g2d.fill(triangle);
// transformiertes dreieck
Shape transformedTriangle = transform.createTransformedShape(triangle);
g2d.setColor(Color.RED);
g2d.fill(transformedTriangle);
}
}
};
translateXInput = new JTextField("0", 5);
translateYInput = new JTextField("0", 5);
scaleXInput = new JTextField("1.0", 5);
scaleYInput = new JTextField("1.0", 5);
rotateAngleInput = new JTextField("0.0", 5);
skewXInput = new JTextField("0", 5);
skewYInput = new JTextField("0", 5);
JButton translateButton = new JButton("Verschieben");
JButton scaleButton = new JButton("Skalieren");
JButton skewButton = new JButton("Scheren");
JButton rotateButton = new JButton("Rotieren");
JButton rectangleButton = new JButton("Rechteck");
JButton circleButton = new JButton("Kreis");
JButton triangleButton = new JButton("Dreieck");
translateButton.addActionListener(e -> panel.repaint());
scaleButton.addActionListener(e -> panel.repaint());
rotateButton.addActionListener(e -> panel.repaint());
skewButton.addActionListener(e -> panel.repaint());
rectangleButton.addActionListener(e -> {
rectangleSelected = true;
circleSelected = false;
panel.repaint();
});
circleButton.addActionListener(e -> {
rectangleSelected = false;
circleSelected = true;
panel.repaint();
});
triangleButton.addActionListener(e -> {
rectangleSelected = false;
circleSelected = false;
panel.repaint();
});
panel.add(new JLabel("X-Translation: "));
panel.add(translateXInput);
panel.add(new JLabel("Y-Translation: "));
panel.add(translateYInput);
panel.add(translateButton);
panel.add(new JLabel("Skalierungsfaktor X: "));
panel.add(scaleXInput);
panel.add(new JLabel("Skalierungsfaktor Y: "));
panel.add(scaleYInput);
panel.add(scaleButton);
panel.add(new JLabel("X-Scherungskoeffizient: "));
panel.add(skewXInput);
panel.add(new JLabel("Y-Scherungskoeffizient: "));
panel.add(skewYInput);
panel.add(skewButton);
panel.add(new JLabel("Rotationswinkel: "));
panel.add(rotateAngleInput);
panel.add(rotateButton);
panel.add(rectangleButton);
panel.add(circleButton);
panel.add(triangleButton);
add(panel);
panel.setLayout(new FlowLayout());
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(AffineTransformationen::new);
}
}