Skip to content

Commit b68cfe5

Browse files
committed
音乐播放,缩放控制和横线数量控制,判定线设置,note放置预览
1 parent f323447 commit b68cfe5

File tree

12 files changed

+182
-87
lines changed

12 files changed

+182
-87
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/res/Pluto.wav
2+
/top/alumopper/PMEditor/DrawTrail.java
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

top/alumopper/PMEditor/Editor.java

+4-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package top.alumopper.PMEditor;
22

3+
import javax.swing.*;
34
import java.awt.*;
45
import java.awt.event.*;
56
import java.io.File;
@@ -9,48 +10,18 @@ public class Editor implements KeyListener {
910

1011

1112
public static void main(String[] args) throws IOException {
12-
Frame fr = new Frame("PMEditor");
13+
Frame fr = new JFrame("PMEditor");
1314
fr.setSize(900,600);
1415
fr.setBackground(Color.black);
15-
EditorPanel ep = new EditorPanel();
16+
EditorPanel ep = new EditorPanel(fr);
1617
fr.add(ep);
1718
fr.setVisible(true);
1819
fr.addWindowListener(
19-
new WindowListener() {
20-
@Override
21-
public void windowOpened(WindowEvent e) {
22-
23-
}
24-
20+
new WindowAdapter() {
2521
@Override
2622
public void windowClosing(WindowEvent e) {
2723
System.exit(0);
2824
}
29-
30-
@Override
31-
public void windowClosed(WindowEvent e) {
32-
33-
}
34-
35-
@Override
36-
public void windowIconified(WindowEvent e) {
37-
38-
}
39-
40-
@Override
41-
public void windowDeiconified(WindowEvent e) {
42-
43-
}
44-
45-
@Override
46-
public void windowActivated(WindowEvent e) {
47-
48-
}
49-
50-
@Override
51-
public void windowDeactivated(WindowEvent e) {
52-
53-
}
5425
}
5526
);
5627
new Thread(ep).run();
+101-35
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,127 @@
11
package top.alumopper.PMEditor;
22

33
import javax.media.Control;
4+
import javax.media.Player;
45
import javax.media.Time;
56
import javax.media.bean.playerbean.MediaPlayer;
67
import javax.swing.*;
78
import java.awt.*;
89
import java.awt.event.*;
10+
import java.awt.image.BufferedImage;
911
import java.io.File;
1012
import java.io.IOException;
1113

12-
public class EditorPanel extends Panel implements Runnable,MouseListener {
14+
public class EditorPanel extends Panel implements Runnable {
15+
16+
public Frame fr;
17+
private Scrollbar t;
1318

1419
private NotePanel np; //note显示区域
20+
private InfoPanel ip; //信息显示区域
1521
public ChartReader cr; //读谱器
1622
public double time; //时间
23+
public int curLine; //当前的判定线
24+
25+
public boolean pressShift = false;
26+
public boolean pressCtrl = false;
27+
public boolean pressSpace = false;
1728

18-
public EditorPanel() throws IOException {
29+
public EditorPanel(Frame fr) throws IOException {
30+
this.fr = fr;
31+
this.setBackground(Color.black);
32+
curLine = 0;
1933
setLayout(null);
2034
cr = new ChartReader("res/chart.json");
2135
np = new NotePanel(this);
36+
ip = new InfoPanel(this);
2237
//进度条
23-
Scrollbar t = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,101);
24-
t.setLocation(50,15);
25-
t.setSize(700,15);
38+
t = new Scrollbar(Scrollbar.VERTICAL,0,1,0,101);
39+
t.setLocation(50,50);
40+
t.setSize(20,500);
2641
t.addAdjustmentListener(e -> {
2742
//进度条被拖动
28-
time = t.getValue()* cr.songTime/100;
43+
time = (100-t.getValue())* cr.songTime/100;
2944
});
3045
this.add(t);
3146
this.addMouseWheelListener(e -> {
3247
//滚轮
33-
//设置时间
34-
time += -1*e.getWheelRotation()* np.eachTime/4;
35-
if(time < 0.0) time = 0;
36-
if(time > cr.songTime) time = cr.songTime;
37-
//进度条
38-
t.setValue((int)(time/cr.songTime*100));
48+
if(!pressCtrl && !pressShift){
49+
//设置时间
50+
time += -1*e.getWheelRotation()* np.eachTime/4;
51+
if(time < 0.0) time = 0;
52+
if(time > cr.songTime) time = cr.songTime;
53+
//进度条
54+
t.setValue(100-(int)(time/cr.songTime*100));
55+
}else if(pressCtrl && !pressShift){
56+
//调整缩放
57+
np.scale += -1*e.getWheelRotation();
58+
if(np.scale < 1) np.scale = 1;
59+
if(np.scale > 16) np.scale = 16;
60+
}else if(pressShift){
61+
//调整横线数量
62+
np.lines += -1*e.getWheelRotation();
63+
if(np.lines < 1) np.lines = 1;
64+
if(np.lines > 16) np.lines = 16;
65+
}
66+
});
67+
this.addKeyListener(new KeyAdapter() {
68+
@Override
69+
public void keyTyped(KeyEvent e) {
70+
super.keyTyped(e);
71+
System.out.println(e);
72+
}
73+
});
74+
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
75+
new KeyEventDispatcher() {
76+
@Override
77+
public boolean dispatchKeyEvent(KeyEvent e) {
78+
pressCtrl = e.isControlDown();
79+
pressShift = e.isShiftDown();
80+
if(e.getKeyCode() == KeyEvent.VK_SPACE){
81+
if(pressSpace){
82+
pressSpace = false;
83+
//播放
84+
if(cr.song.songPlayer.getState() != Player.Started){
85+
cr.song.songPlayer.setMediaTime(new Time(time));
86+
cr.song.songPlayer.start();
87+
}else{
88+
cr.song.songPlayer.stop();
89+
}
90+
}else{
91+
pressSpace = true;
92+
}
93+
}
94+
return false;
95+
}
96+
}
97+
);
98+
this.addMouseListener(new MouseAdapter() {
99+
@Override
100+
public void mouseClicked(MouseEvent e) {
101+
Point clickPoint = e.getPoint();
102+
if(pointInRect(clickPoint,755,55,825,80)){
103+
//点击了文本框内
104+
ip.tf.setVisible(true);
105+
}else{
106+
//点击了文本框外面
107+
ip.tf.setVisible(false);
108+
int qwq = curLine;
109+
try{
110+
curLine = Integer.parseInt(ip.tf.getText());
111+
}catch (NumberFormatException awa){
112+
curLine = qwq;
113+
ip.tf.setText(String.valueOf(curLine));
114+
}
115+
}
116+
}
39117
});
40118
time = 0;
41119
}
42120

43121
public void paint(Graphics g){
44122
//note展示区域
45123
np.draw((Graphics2D) g);
124+
ip.draw((Graphics2D) g);
46125
}
47126

48127
public void repaint(){
@@ -56,6 +135,10 @@ public void repaint(){
56135
}
57136

58137
public void loop(){
138+
if(cr.song.songPlayer.getState() == Player.Started){
139+
time = cr.song.songPlayer.getMediaTime().getSeconds();
140+
t.setValue(100-(int)(time/cr.songTime*100));
141+
}
59142
repaint();
60143
}
61144

@@ -66,28 +149,11 @@ public void run() {
66149
}
67150
}
68151

69-
@Override
70-
public void mouseClicked(MouseEvent e) {
71-
72-
}
73-
74-
@Override
75-
public void mousePressed(MouseEvent e) {
76-
77-
}
78-
79-
@Override
80-
public void mouseReleased(MouseEvent e) {
81-
82-
}
83-
84-
@Override
85-
public void mouseEntered(MouseEvent e) {
86-
87-
}
88-
89-
@Override
90-
public void mouseExited(MouseEvent e) {
91-
152+
public static boolean pointInRect(Point p,int x1,int y1,int x2,int y2){
153+
if(x1 <= p.x && p.x <= x2 && y1 <= p.y && p.y <= y2){
154+
return true;
155+
}else {
156+
return false;
157+
}
92158
}
93159
}

top/alumopper/PMEditor/InfoPanel.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package top.alumopper.PMEditor;
2+
3+
import java.awt.*;
4+
import java.awt.event.TextEvent;
5+
import java.awt.event.TextListener;
6+
7+
public class InfoPanel {
8+
9+
public EditorPanel ep;
10+
public TextField tf;
11+
12+
public InfoPanel(EditorPanel ep){
13+
this.ep = ep;
14+
tf = new TextField();
15+
tf.setFont(new Font("TsangerYuMo W02",Font.PLAIN,15));
16+
tf.setBackground(Color.black);
17+
tf.setForeground(Color.white);
18+
tf.setLocation(775,55);
19+
tf.setSize(50,25);
20+
tf.setText("0");
21+
tf.setVisible(false);
22+
ep.add(tf);
23+
}
24+
25+
public void draw(Graphics2D g){
26+
//判定线数
27+
g.setColor(Color.WHITE);
28+
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
29+
g.setFont(new Font("TsangerYuMo W02",Font.PLAIN,20));
30+
g.drawString("判定线:",700,75);
31+
if(!tf.isVisible()){
32+
g.setFont(new Font("TsangerYuMo W02",Font.PLAIN,15));
33+
g.drawString(String.valueOf(ep.curLine),775,73);
34+
}
35+
}
36+
}

top/alumopper/PMEditor/NotePanel.java

+30-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.awt.*;
44
import java.util.ArrayList;
55

6-
public class NotePanel {
6+
public class NotePanel extends Component {
77
public int lines; //横线数量
88
public double scale; //缩放大小(有多少横线间隔)
99
public ArrayList<Tap> taps; //tap音符
@@ -24,41 +24,53 @@ public void draw(Graphics2D g){
2424
//边框
2525
g.setColor(Color.white);
2626
for (int i = 0; i < 10; i++) {
27-
g.drawLine(50+40*i,50,50+40*i,550);
27+
g.drawLine(80+40*i,50,80+40*i,550);
2828
}
29-
g.drawLine(50,50,410,50);
30-
g.setStroke(new BasicStroke(3));
31-
g.setColor(Color.YELLOW);
32-
g.drawLine(50,550,410,550);
29+
g.drawLine(80,50,440,50);
3330
//高亮主轨道
3431
g.setColor(new Color(208, 220, 255, 69));
35-
g.fillRect(90,50,40,500);
36-
g.fillRect(170,50,40,500);
37-
g.fillRect(250,50,40,500);
38-
g.fillRect(330,50,40,500);
32+
g.fillRect(120,50,40,500);
33+
g.fillRect(200,50,40,500);
34+
g.fillRect(280,50,40,500);
35+
g.fillRect(360,50,40,500);
3936
//进度条
4037
//g.setStroke(new BasicStroke(2));
4138
g.setColor(Color.WHITE);
4239
//drawProgressBar(g);
4340
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
4441
g.setFont(new Font("TsangerYuMo W02",Font.PLAIN,15));
45-
g.drawString(String.format("%.2f/%.2f",ep.time,ep.cr.song.songPlayer.getDuration().getSeconds()),775,30);
46-
//粗横线
42+
g.drawString(String.format("%.2f/%.2f",ep.time,ep.cr.song.songPlayer.getDuration().getSeconds()),10,40);
43+
//横线
4744
double delLine = -ep.time%eachTime/eachTime;
4845
double delPixel = 500/scale;
49-
for(double i = 0;i < 6;i += 1.0/lines){
50-
if(delLine+i > 0 ){
51-
if(i == (int)i){
46+
for(double i = 0;i < scale+1;i += 1.0/lines){
47+
if(delLine+i >= 0 && delLine+i <= scale ){
48+
if(lines == 3){
49+
System.out.println();
50+
}
51+
if(Math.round(i*100)/100.0 == Math.round(i)){
5252
g.setStroke(new BasicStroke(3));
53-
g.setColor(new Color(191,98,10,255));
54-
g.drawLine(50,(int)(550-(delLine+i)*delPixel),410,(int)(550-(delLine+i)*delPixel));
53+
g.setColor(new Color(19,198,10,255));
54+
g.drawLine(80,(int)(550-(delLine+i)*delPixel),440,(int)(550-(delLine+i)*delPixel));
5555
}else{
5656
g.setStroke(new BasicStroke(1));
5757
g.setColor(new Color(11,45,14,255));
58-
g.drawLine(50,(int)(550-(delLine+i)*delPixel),410,(int)(550-(delLine+i)*delPixel));
58+
g.drawLine(80,(int)(550-(delLine+i)*delPixel),440,(int)(550-(delLine+i)*delPixel));
5959
}
6060
}
6161
}
62+
g.setStroke(new BasicStroke(3));
63+
g.setColor(Color.YELLOW);
64+
g.drawLine(80,550,440,550);
65+
//鼠标相对位置
66+
Point mousePos = MouseInfo.getPointerInfo().getLocation();
67+
mousePos.x -= ep.fr.getLocationOnScreen().x;
68+
mousePos.y -= ep.fr.getLocationOnScreen().y + 30;
69+
//如果在范围内,就绘制按键
70+
if(EditorPanel.pointInRect(mousePos,80,50,440,550)){
71+
g.setColor(new Color(90, 210, 229, 131));
72+
g.fillRect(mousePos.x-20,mousePos.y-4,40,8);
73+
}
6274
}
6375

6476
private void drawProgressBar(Graphics2D g){

top/alumopper/PMEditor/Test.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package top.alumopper.PMEditor;
22

33
import java.awt.*;
4+
import java.awt.event.KeyAdapter;
5+
import java.awt.event.KeyEvent;
46

57
public class Test {
68
public static void main(String[] args) {
@@ -16,7 +18,7 @@ public static void main(String[] args) {
1618

1719
}
1820

19-
class MyPanel extends Panel {
21+
class MyPanel extends Panel {
2022
private int x;
2123
private int y;
2224
private int diameter;
@@ -25,6 +27,13 @@ public MyPanel() {
2527
x = 50;
2628
y = 50;
2729
diameter = 100;
30+
this.addKeyListener(new KeyAdapter() {
31+
@Override
32+
public void keyTyped(KeyEvent e) {
33+
super.keyTyped(e);
34+
System.out.println(e);
35+
}
36+
});
2837
}
2938

3039
public void paint(Graphics g) {

0 commit comments

Comments
 (0)