-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.java
More file actions
177 lines (156 loc) · 5.64 KB
/
Tools.java
File metadata and controls
177 lines (156 loc) · 5.64 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
175
176
177
import java.net.*;
import javax.sound.sampled.*;
import java.io.IOException;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
//! You can put all your tool-code in here
public class Tools {
// Declare variables
public static Clip clip;
public static Clip click_SE;
/*
* Play another bgm (for example):
* Tools.stopBGM();
* Tools.playGameBGM();
* Play click sound effect
* Tools.basicClick_SE();
*/
public static void playMainBGM() {
try {
// get Clip
clip = AudioSystem.getClip();
// open the audio input stream
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new URL("file:./src/audio/main-page-bgm.wav"));
clip.open(inputStream);
// get the volume control
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// countrol the volume
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float volume = (max - min) * 0.68f + min;
volumeControl.setValue(volume);
// start playing the clip and loop continuously
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (LineUnavailableException e) {
e.printStackTrace(); // Handle LineUnavailableException
} catch (MalformedURLException e) {
e.printStackTrace(); // Handle MalformedURLException
} catch (UnsupportedAudioFileException e) {
e.printStackTrace(); // Handle UnsupportedAudioFileException
} catch (IOException e) {
e.printStackTrace(); // Handle IOException
}
}
public static void playGameBGM() {
try {
clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new URL("file:./src/audio/in-game-bgm.wav"));
clip.open(inputStream);
// get the volume control
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// countrol the volume
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float volume = (max - min) * 0.69f + min;
volumeControl.setValue(volume);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void stopBGM() {
// Check if the clip is not null and is currently running
if (clip != null && clip.isRunning()) {
clip.stop(); // Stop the clip
clip.close(); // Close the clip
}
}
public static void basicClick_SE() {
try {
click_SE = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(new URL("file:./src/audio/basic-mouse-click-SE.wav"));
click_SE.open(inputStream);
// get the volume control
FloatControl volumeControl = (FloatControl) click_SE.getControl(FloatControl.Type.MASTER_GAIN);
// countrol the volume
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float volume = (max - min) * 0.7f + min;
volumeControl.setValue(volume);
click_SE.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void shootClick_SE() {
try {
click_SE = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(new URL("file:./src/audio/guile-mouse-click-SE.wav"));
click_SE.open(inputStream);
// get the volume control
FloatControl volumeControl = (FloatControl) click_SE.getControl(FloatControl.Type.MASTER_GAIN);
// countrol the volume
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float volume = (max - min) * 0.7f + min;
volumeControl.setValue(volume);
click_SE.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void adjustVolume(float adjustValue) {
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float newVolume = volume.getValue() + adjustValue;
float minVolume = volume.getMinimum();
float maxVolume = volume.getMaximum();
if (newVolume < minVolume) {
newVolume = minVolume;
} else if (newVolume > maxVolume) {
newVolume = maxVolume;
}
volume.setValue(newVolume);
} else {
System.out.println("Volume control not supported");
}
}
public static void drawText(String text, Font font, Graphics2D g2d, int x, int y, int border_width){
g2d.setFont(font);
// 描邊文本(黑色)
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(border_width)); // 設置描邊寬度
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout tl = new TextLayout(text, font, frc);
Shape shape = tl.getOutline(null);
g2d.translate(x, y);
g2d.draw(shape);
// 填充文本(白色)
g2d.setColor(Color.WHITE);
g2d.fill(shape); // 填充文本
g2d.translate(-x, -y);
}
}