質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -111,27 +111,203 @@
|
|
111
111
|
### 該当のソースコード
|
112
112
|
|
113
113
|
```java
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
114
|
+
package music2;
|
115
|
+
|
116
|
+
import java.awt.Color;
|
117
|
+
import java.awt.Graphics;
|
118
|
+
import java.awt.Graphics2D;
|
119
|
+
import java.awt.event.ActionEvent;
|
120
|
+
import java.awt.event.ActionListener;
|
121
|
+
import java.awt.event.MouseAdapter;
|
122
|
+
import java.awt.event.MouseEvent;
|
123
|
+
import java.awt.geom.Ellipse2D;
|
124
|
+
import java.io.IOException;
|
125
|
+
import java.net.URL;
|
126
|
+
|
127
|
+
import javax.sound.sampled.AudioInputStream;
|
128
|
+
import javax.sound.sampled.AudioSystem;
|
129
|
+
import javax.sound.sampled.Clip;
|
130
|
+
import javax.sound.sampled.FloatControl;
|
131
|
+
import javax.sound.sampled.LineEvent;
|
132
|
+
import javax.sound.sampled.LineListener;
|
133
|
+
import javax.sound.sampled.LineUnavailableException;
|
134
|
+
import javax.sound.sampled.UnsupportedAudioFileException;
|
135
|
+
import javax.swing.JFrame;
|
136
|
+
import javax.swing.JLabel;
|
137
|
+
import javax.swing.JPanel;
|
138
|
+
import javax.swing.JSlider;
|
139
|
+
import javax.swing.Timer;
|
140
|
+
import javax.swing.event.ChangeEvent;
|
141
|
+
import javax.swing.event.ChangeListener;
|
142
|
+
|
143
|
+
public class TestFrame extends JFrame implements ChangeListener, LineListener {
|
144
|
+
public static void main(String[] args) {
|
145
|
+
new TestFrame().setVisible(true);
|
146
|
+
}
|
147
|
+
|
148
|
+
private JSlider slider;
|
149
|
+
private JLabel label;
|
150
|
+
private CirclePanel circlePanel;
|
151
|
+
private Clip clip;
|
152
|
+
private FloatControl gainControl;
|
153
|
+
|
154
|
+
TestFrame() {
|
155
|
+
super("アプリ");
|
156
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
157
|
+
setSize(800, 800);
|
158
|
+
|
159
|
+
circlePanel = new CirclePanel();
|
160
|
+
setContentPane(circlePanel);
|
161
|
+
|
162
|
+
label = new JLabel("test");
|
163
|
+
label.setBounds(300, 10, 100, 100);
|
164
|
+
add(label);
|
165
|
+
|
166
|
+
JLabel label2 = new JLabel("テスト");
|
167
|
+
label2.setBounds(300, 500, 100, 100);
|
168
|
+
add(label2);
|
169
|
+
|
170
|
+
slider = new JSlider(50, 150, 100);
|
171
|
+
slider.addChangeListener(this);
|
172
|
+
JPanel p = new JPanel();
|
173
|
+
p.add(slider);
|
174
|
+
p.setBounds(300, 300, 200, 50);
|
175
|
+
add(p);
|
176
|
+
|
177
|
+
try {
|
178
|
+
loadAudioFile("ogawa.wav");
|
179
|
+
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
180
|
+
e.printStackTrace();
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
private void loadAudioFile(String filePath) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
|
185
|
+
URL url = getClass().getResource(filePath);
|
186
|
+
if (url != null) {
|
187
|
+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
|
188
|
+
clip = AudioSystem.getClip();
|
189
|
+
clip.open(audioInputStream);
|
190
|
+
gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
|
191
|
+
clip.addLineListener(this);
|
192
|
+
System.err.println("音声ファイルが見つかった: " + filePath);
|
193
|
+
} else {
|
194
|
+
System.err.println("音声ファイルが見つかりません: " + filePath);
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
public void stateChanged(ChangeEvent e) {
|
199
|
+
label.setText("値:" + slider.getValue());
|
200
|
+
circlePanel.setDiameter(slider.getValue());
|
201
|
+
float value = (float) slider.getValue() / 100f; // 0.5 ~ 1.5の範囲に正規化
|
202
|
+
gainControl.setValue(20f * (float) Math.log10(value)); // 音量の調整
|
203
|
+
}
|
204
|
+
|
205
|
+
@Override
|
206
|
+
public void update(LineEvent event) {
|
207
|
+
LineEvent.Type type = event.getType();
|
208
|
+
if (type == LineEvent.Type.STOP) {
|
209
|
+
circlePanel.setStart(false);
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
private class CirclePanel extends JPanel {
|
214
|
+
private final int X = 350, Y = 250;
|
215
|
+
private int diameterOfCircle = 100;
|
216
|
+
private Ellipse2D ellipse;
|
217
|
+
private boolean start = false;
|
218
|
+
private Color ellipseColor = Color.GRAY; // 円の色を保持する変数
|
219
|
+
private Timer shakeTimer;
|
220
|
+
private int shakeOffset = 0;
|
221
|
+
|
222
|
+
private class EllipseClickListener extends MouseAdapter {
|
223
|
+
@Override
|
224
|
+
public void mouseClicked(MouseEvent e) {
|
225
|
+
if (ellipse.contains(e.getPoint())) {
|
226
|
+
System.out.println(e.getPoint() + "=図形内");
|
227
|
+
if (!start) {
|
228
|
+
start = true;
|
229
|
+
clip.start();
|
230
|
+
setEllipseColor(Color.CYAN); // 円の色を水色に変更
|
231
|
+
startShaking();
|
232
|
+
System.out.println(start);
|
233
|
+
} else {
|
234
|
+
start = false;
|
235
|
+
clip.stop();
|
236
|
+
setEllipseColor(Color.LIGHT_GRAY);
|
237
|
+
stopShaking();
|
238
|
+
System.out.println(start);
|
239
|
+
}
|
240
|
+
} else {
|
241
|
+
System.out.println(e.getPoint() + "=図形外");
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
void setEllipseColor(Color color) {
|
247
|
+
this.ellipseColor = color;
|
248
|
+
repaint();
|
249
|
+
}
|
250
|
+
|
251
|
+
CirclePanel() {
|
252
|
+
super(null);
|
253
|
+
setBackground(Color.WHITE);
|
254
|
+
addMouseListener(new EllipseClickListener());
|
255
|
+
initShakeTimer();
|
256
|
+
setEllipseColor(Color.LIGHT_GRAY);
|
257
|
+
}
|
258
|
+
|
259
|
+
void setDiameter(int diameter) {
|
260
|
+
this.diameterOfCircle = diameter;
|
261
|
+
repaint();
|
262
|
+
}
|
263
|
+
|
264
|
+
void setStart(boolean start) {
|
265
|
+
this.start = start;
|
266
|
+
}
|
267
|
+
|
268
|
+
private void initShakeTimer() {
|
269
|
+
shakeTimer = new Timer(50, new ActionListener() {
|
270
|
+
@Override
|
271
|
+
public void actionPerformed(ActionEvent e) {
|
272
|
+
shakeOffset = (shakeOffset == 0) ? 5 : 0;
|
273
|
+
repaint();
|
274
|
+
}
|
275
|
+
});
|
276
|
+
}
|
277
|
+
|
278
|
+
private void startShaking() {
|
279
|
+
shakeTimer.start();
|
280
|
+
}
|
281
|
+
|
282
|
+
private void stopShaking() {
|
283
|
+
shakeTimer.stop();
|
284
|
+
shakeOffset = 0;
|
285
|
+
repaint();
|
286
|
+
}
|
287
|
+
|
288
|
+
@Override
|
289
|
+
protected void paintComponent(Graphics g) {
|
290
|
+
super.paintComponent(g);
|
291
|
+
Graphics2D g2 = (Graphics2D) g;
|
292
|
+
g2.setColor(ellipseColor); // 円の色を設定
|
293
|
+
ellipse = new Ellipse2D.Double(X - diameterOfCircle / 2 + shakeOffset, Y - diameterOfCircle / 2, diameterOfCircle, diameterOfCircle);
|
294
|
+
g2.fill(ellipse);
|
295
|
+
|
296
|
+
// ぼうっと光った楕円形の水玉模様を描画
|
297
|
+
int spotWidth = diameterOfCircle / 3; // 横幅は外側の円の1/3
|
298
|
+
int spotHeight = spotWidth / 2; // 縦幅は横幅の半分
|
299
|
+
int spotX = X - diameterOfCircle / 4 + shakeOffset; // 円の中心から1/4の距離に配置
|
300
|
+
int spotY = Y - diameterOfCircle / 4;
|
301
|
+
Ellipse2D spot = new Ellipse2D.Double(spotX, spotY, spotWidth, spotHeight);
|
302
|
+
|
303
|
+
// 水玉模様にぼけ効果を追加
|
304
|
+
g2.setColor(new Color(255, 255, 255, 128)); // 半透明の白色
|
305
|
+
g2.fill(spot);
|
306
|
+
g2.setColor(new Color(255, 255, 255, 255)); // 完全な白色
|
307
|
+
g2.fillOval(spotX + spotWidth / 4, spotY, spotWidth / 2, spotHeight);
|
308
|
+
}
|
309
|
+
}
|
310
|
+
}
|
135
311
|
```
|
136
312
|
|
137
313
|
### 試したこと
|
1
開発環境について記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -138,5 +138,6 @@
|
|
138
138
|
|
139
139
|
試行錯誤しましたが、できません。java swing 水玉 アニメーションなどでGoogle検索しても出てきません。
|
140
140
|
|
141
|
+
### 開発環境
|
142
|
+
Eclipse使用しています。
|
141
143
|
|
142
|
-
|