swing - Java Program says that sound file is not there -
i coding piano in java , used have entire class where, when note pressed specific method in class called specific note. in order shorten code (still working on part), used integers of boolean
array made decide note. example, if boolean[1]
set true
, i'd call method integer , based on integer, string set name of sound file. problem having says file not there, , know fact is. can me out here?
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); (int = 1; < 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); (int = 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 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 gets reset 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()); } } }
Comments
Post a Comment