我目前正在使用Java编写钢琴,我曾经有一个完整的类,当按下一个音符时,该类中的特定方法被称为特定音符。为了缩短我的代码(仍然在那部分工作),我使用了我做的boolean
数组的整数来决定注释。例如,如果boolean[1]
设置为true
,我将使用整数调用该方法,并根据该整数,将字符串设置为声音文件的名称。我遇到的问题是它经常说文件不存在,而且我知道这是事实。有人可以帮帮我吗?
import java.awt.event.*; //imports
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import javax.sound.sampled.*;
public class PianoGame extends JFrame{ //JFrame class
public PianoGame(){
super("PianoGame");
setSize(1000,600);
setLocation(400,50);
setResizable(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
PianoGameContent pgb = new PianoGameContent();
setContentPane(pgb);
setVisible(true);
}
public static void main(String[]args){
PianoGame pg = new PianoGame();
}
}
class PianoGameContent extends JPanel implements KeyListener { //JPanel Class
boolean[] notes = new boolean [30];
File [] snotes = new File [30];
public PianoGameContent() {
boolean[] notes = new boolean [30];
Arrays.fill(notes, Boolean.FALSE);
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Font f = new Font("Serif", Font.BOLD, 50); //draw Piano
g.setFont(f);
for (int i = 1; i < 18; i++){
if(notes[i] == true){
g.setColor(Color.RED);
g.fillRect(50*i, 200, 50, 300);
g.setColor(Color.BLACK);
play(i);
}
else{
g.setColor(Color.WHITE);
g.fillRect(50*i, 200, 50, 300);
g.setColor(Color.BLACK);
}
}
g.setColor(Color.BLACK);
for (int i = 1; i<18; i++)
g.drawRect(50*i, 200, 50, 300);
if (notes[18] == true) {
g.setColor(Color.RED);
g.fillRect(85, 200, 30, 175);
play(18);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(85, 200, 30, 175);
}
if (notes[19] == true) {
g.setColor(Color.RED);
g.fillRect(135, 200, 30, 175);
play(19);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(135, 200, 30, 175);
}
if (notes[20] == true) {
g.setColor(Color.RED);
g.fillRect(235, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(235, 200, 30, 175);
}
if (notes[21] == true) {
g.setColor(Color.RED);
g.fillRect(285, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(285, 200, 30, 175);
}
if (notes[22] == true) {
g.setColor(Color.RED);
g.fillRect(335, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(335, 200, 30, 175);
}
if (notes[23] == true) {
g.setColor(Color.RED);
g.fillRect(435, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(435, 200, 30, 175);
}
if (notes[24] == true) {
g.setColor(Color.RED);
g.fillRect(485, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(485, 200, 30, 175);
}
if (notes[25] == true) {
g.setColor(Color.RED);
g.fillRect(585, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(585, 200, 30, 175);
}
if (notes[26] == true) {
g.setColor(Color.RED);
g.fillRect(635, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(635, 200, 30, 175);
}
if (notes[27] == true) {
g.setColor(Color.RED);
g.fillRect(685, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(685, 200, 30, 175);
}
if (notes[28] == true) {
g.setColor(Color.RED);
g.fillRect(785, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(785, 200, 30, 175);
}
if (notes[29] == true) {
g.setColor(Color.RED);
g.fillRect(835, 200, 30, 175);
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.fillRect(835, 200, 30, 175);
}
g.drawRect(85, 200, 30, 175);
g.drawRect(135, 200, 30, 175);
g.drawRect(235, 200, 30, 175);
g.drawRect(285, 200, 30, 175);
g.drawRect(335, 200, 30, 175);
g.drawRect(435, 200, 30, 175);
g.drawRect(485, 200, 30, 175);
g.drawRect(585, 200, 30, 175);
g.drawRect(635, 200, 30, 175);
g.drawRect(685, 200, 30, 175);
g.drawRect(785, 200, 30, 175);
g.drawRect(835, 200, 30, 175);
}
public void keyPressed(KeyEvent e) { //keypress sets assigned variable to true
char x;
x = e.getKeyChar();
if (e.getKeyCode() == KeyEvent.VK_1)
notes [18] = true;
if (e.getKeyCode() == KeyEvent.VK_2)
notes [19] = true;
if (e.getKeyCode() == KeyEvent.VK_3)
notes [20] = true;
if (e.getKeyCode() == KeyEvent.VK_4)
notes [21] = true;
if (e.getKeyCode() == KeyEvent.VK_5)
notes [22] = true;
if (e.getKeyCode() == KeyEvent.VK_6)
notes [23] = true;
if (e.getKeyCode() == KeyEvent.VK_7)
notes [24] = true;
if (e.getKeyCode() == KeyEvent.VK_8)
notes [25] = true;
if (e.getKeyCode() == KeyEvent.VK_9)
notes [26] = true;
if (e.getKeyCode() == KeyEvent.VK_0)
notes [27] = true;
if (x == 'q')
notes [1] = true;
if (x == 'w')
notes [2]= true;
if (x == 'e')
notes [3] = true;
if (x == 'r')
notes [4] = true;
if (x == 't')
notes [5] = true;
if (x == 'y')
notes [6] = true;
if (x == 'u')
notes [7] = true;
if (x == 'i')
notes[8] = true;
if (x == 'o')
notes[9] = true;
if (x == 'p')
notes[10] = true;
if (x == 'a')
notes [11] = true;
if (x == 's')
notes [12] = true;
if (x == 'd')
notes [13] = true;
if (x == 'f')
notes [14] = true;
if (x == 'g')
notes [15] = true;
if (x == 'h')
notes [16] = true;
if (x == 'j')
notes [17] = true;
if (x == 'k')
notes [28]= true;
if (x == 'l')
notes [29] = true;
repaint();
}
public void keyReleased(KeyEvent e) { //when released everything gets reset to false
Arrays.fill(notes, Boolean.FALSE);
repaint();
}
public void keyTyped(KeyEvent e) {
}
public void play(int decide){
String x = "c4.wav";
if (decide == 1)
x = "c4.wav";
if (decide == 2)
x = "d4.wav";
if (decide == 3)
x = "e4.wav";
if (decide == 4)
x = "f4.wav";
if (decide == 5)
x = "g4.wav";
if (decide == 6)
x = "a4.wav";
if (decide == 7)
x = "b4.wav";
if (decide == 8)
x = "c5.wav";
if (decide == 9)
x = "d5.wav";
if (decide == 10)
x = "e5.wav";
if (decide == 11)
x = "f5.wav";
if (decide == 12)
x = "g5.wav";
if (decide == 13)
x = "a5.wav";
if (decide == 14)
x = "b5.wav";
if (decide == 15)
x = "c6.wav";
if (decide == 16)
x = "d6.wav";
if (decide == 17)
x = "e6.wav";
if (decide == 18)
x = "db4.wav";
if (decide == 19)
x = "eb4.wav";
try {
File csound = new File(x);
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}