-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRectangleRotation.java
More file actions
174 lines (133 loc) · 4.99 KB
/
RectangleRotation.java
File metadata and controls
174 lines (133 loc) · 4.99 KB
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
package gui;
/**
* RUN:
* javac -cp .; gui/RectangleRotation.java && java -cp .; gui.RectangleRotation
* OUTPUT:
*
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import net.mindview.util.*;
public class RectangleRotation extends JFrame {
private static final int WIDTH = 640;
private static final int HEIHGT = 480;
private MainContentPane mainPane = new MainContentPane();
private JSlider sizeSlider = new JSlider(0, 255, 50);
private JSlider speedSlider = new JSlider(0, 200, 10);
public RectangleRotation()
{
mainPane.setOpaque(true); //content panes must be opaque
// setContentPane(mainPane);
add(mainPane);
sizeSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
mainPane.setSize(
((JSlider) e.getSource()).getValue()
);
}
});
speedSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
mainPane.setSpeed(
((JSlider) e.getSource()).getValue()
);
}
});
JPanel botomPane = new JPanel();
GroupLayout layout = new GroupLayout(botomPane);
botomPane.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(sizeSlider)
.addComponent(speedSlider)
)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(sizeSlider)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(speedSlider)
)
);
add(BorderLayout.SOUTH, botomPane);
}
public static void main(String[] args) {
SwingConsole.run(new RectangleRotation(), WIDTH, HEIHGT);
}
static class MainContentPane extends JPanel implements ActionListener {
private int rectSize = 30;
private int rotationSpeed = 0;
private int rotationAngle = 0;
private javax.swing.Timer timer;
public MainContentPane() {
// timer = new javax.swing.Timer(rotationSpeed, this);
// timer.setInitialDelay(100);
// timer.start();
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
drawRectangle(rectSize, rotationSpeed);
}
};
timer = new javax.swing.Timer(rotationSpeed, taskPerformer);
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//
Graphics2D g2 = (Graphics2D) g;
//
g2.setRenderingHints(new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON
));
g2.setRenderingHints(new RenderingHints(
RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY
));
Dimension d = getSize();
int w = d.width;
int h = d.height;
int xTopLeft = (w - rectSize)/2;
int yTopLeft = (h - rectSize)/2;
g2.setColor(Color.RED);
Shape rect = new Rectangle(xTopLeft, yTopLeft, rectSize, rectSize);
Rectangle rb = rect.getBounds();
Path2D.Double path = new Path2D.Double();
path.append(rect, false);
AffineTransform at = new AffineTransform();
at.setToIdentity();
// at.translate((rb.width/2), (rb.height/2));
at.rotate(Math.toRadians(rotationAngle), w/2, h/2);
path.transform(at);
g2.draw(path);
}
public void setSize(int newSize) {
// drawRectangle(newSize, rotationSpeed);
this.rectSize = newSize;
}
public void setSpeed(int newSpeed) {
// drawRectangle(rectSize, newSpeed);
this.rotationSpeed = newSpeed;
}
public void drawRectangle(int newSize, int newSpeed)
{
rectSize = newSize;
rotationSpeed = newSpeed;
this.rotationAngle++;
timer.setDelay(rotationSpeed);
repaint();
}
public void actionPerformed(ActionEvent e) {
drawRectangle(this.rectSize, this.rotationSpeed);
}
}
}