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
| import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class TestRotate {
public static class ShapeAndColor {
private final Shape shape;
private final Color color;
public ShapeAndColor(Shape shape, Color color) {
super();
this.shape = shape;
this.color = color;
}
public Shape getShape() {
return shape;
}
public Color getColor() {
return color;
}
}
public static double po=0.0;
public static class RotatingShapesPanel extends JComponent {
private List<ShapeAndColor> shapes;
private double rotation = 0.0;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform translate = AffineTransform.getTranslateInstance(-getWidth()/2, -getHeight()/2);
AffineTransform rotate = AffineTransform.getRotateInstance(rotation);
AffineTransform t = AffineTransform.getTranslateInstance(getWidth()/2 , getHeight()/2);
t.concatenate(rotate);
t.concatenate(translate);
g2d.setTransform(t);
AffineTransform scale = AffineTransform.getScaleInstance(getWidth(), getHeight());
for (ShapeAndColor shape : shapes) {
Area area = new Area(shape.getShape());
g2d.setColor(shape.getColor());
area.transform(scale);
g2d.fill(area);
}
}
public void setShapes(List<ShapeAndColor> shapes) {
this.shapes = shapes;
repaint();
}
public double getRotation() {
return rotation;
}
public void setRotation(double rotation) {
this.rotation = rotation;
repaint();
}
}
protected void initUI(final boolean useBorderLayout) {
JFrame frame = new JFrame();
JFrame btnFrm = new JFrame();
double op;
frame.add(new TestPane());
frame.getContentPane().setLayout(new GridLayout(1,1));
frame.setSize(1500, 2000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(new Color(50,150,200));
btnFrm.getContentPane().setLayout(new GridLayout(3,1));
btnFrm.setSize(100, 300);
btnFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnFrm.getContentPane().setBackground(new Color(200,150,50));
JButton leftBtn = new JButton("против часовой стрелки");
leftBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ActionEvent) {
po=-0.02;
}
});
btnFrm.add(leftBtn);
JButton rightBtn = new JButton("по часовой стрелки:");
rightBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ActionEvent) {
po=0.02 ;
}
});
btnFrm.add(rightBtn);
JButton stopBtn = new JButton("STOP!!!");
stopBtn.addActionListener((ActionEvent) -> {
po=0.0;
});
btnFrm.add(stopBtn);
List<ShapeAndColor> shapes = new ArrayList<>();
Random r = new Random();
double x = 0.4;
double y =0.4;
double p1 = 0.1;
double p2 =0.1;
Rectangle2D.Double shape = new Rectangle2D.Double(x, y, p1, p2);
Color color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
shapes.add(new ShapeAndColor(shape, color));
final RotatingShapesPanel panel = new RotatingShapesPanel();
panel.setShapes(shapes);
frame.add(panel);
frame.setSize(500, 500);
frame.setVisible(true);
btnFrm.setVisible(true);
Timer t = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double rotation = panel.getRotation() + po;
if (rotation > Math.PI * 2) {
rotation -= Math.PI * 2;
}
panel.setRotation(rotation);
}
});
t.setRepeats(true);
t.setDelay(3);
t.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestRotate().initUI(true);
}
});
}
class TestPane extends JPanel {
TriangleShape triangleShape;
Polygon poly;
public TestPane() {
triangleShape = new TriangleShape(new Point2D.Double(150, 50),new Point2D.Double(150, 150), new Point2D.Double(50, 150));
poly = new Polygon(new int[] { 150, 200, 100 }, new int[] { 100, 200, 200 }, 3);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fill(poly);
g2d.dispose();
}
}
class TriangleShape extends Path2D.Double {
public TriangleShape(Point2D... points) {
moveTo(points[0].getX(), points[0].getY());
lineTo(points[1].getX(), points[1].getY());
lineTo(points[2].getX(), points[2].getY());
closePath();
}
}
} |