RishSA|Imaboss2414
Career
- Joined
- Sep 8, 2013
- Messages
- 427
- Reaction score
- 833
//Rishabh DRAFT#1
//4/17/16
//Final Game Project (PianoGame.Java)
//To make a program that teaches the user how to read sheet music and potentially spark an interest through the process
//This program will use many imports including sound as well as timers and array untilitys.
import java.awt.*;
import java.awt.event.*; //import java envents, timer sound file, io and color
import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import javax.sound.sampled.*;
public class PianoGame extends JFrame{ //JFrame class header
public PianoGame(){
super("PianoGame");
setSize(2000,1000);
setLocation(0,0);
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 header
boolean[] notes = new boolean[30]; //make a boolean array
public PianoGameContent() {
boolean[] notes = new boolean[30]; //initialize and set everything to false
Arrays.fill(notes, Boolean.FALSE);
addKeyListener(this); //add key listeners
setFocusable(true);
requestFocusInWindow();
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //super.paintcomponent
Font f = new Font("Serif", Font.BOLD, 50); //draw Piano
g.setColor(Color.BLACK);
g.setFont(f);
g.drawString("SheetReader101", 300, 150);
for (int i = 1; i < 18; i++) { //for loop to draw white keys and call the sound method
if (notes == true) { //if statement for true
g.setColor(Color.RED); //set color to red and fill the rectangle and then call the sound method
g.fillRect(50 * i, 200, 50, 300);
play(i);
} else { //else statement for false
g.setColor(Color.WHITE);
g.fillRect(50 * i, 200, 50, 300);
}
}
g.setColor(Color.BLACK);
for (int i = 1; i < 18; i++) //re-draw so the outline remains (for loop)
g.drawRect(50 * i, 200, 50, 300);
if (notes[18] == true) { // several if statements for black keys (no distinguishible black key pattern so loops wont work here)
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);
play(20);
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);
play(21);
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);
play(22);
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);
play(23);
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);
play(24);
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);
play(25);
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);
play(26);
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);
play(27);
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);
play(28);
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);
play(29);
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); //re-draw the outline for the black keys so that the colors dont look borderless
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; //use a character and key char along with which booleans have been pressed to see what returns as true/what note has been pressed
x = e.getKeyChar();
if (e.getKeyCode() == KeyEvent.VK_1) //have the number ones up top and these will be the black keys. have it so that if the keycode is equal to a key (that will be decided in the future) is pressed than the certain key will be activated
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;
System.out.println("hi");
repaint();
}
public void keyReleased(KeyEvent e) { //when released everything gets reset to false
Arrays.fill(notes, Boolean.FALSE);
repaint();
play(30);
}
public void keyTyped(KeyEvent e) {
}
public void play(int decide) { //Use loop and array in sync with this method. depdning on the number that is passed to this, the string is set do a different value.
String x = "src/c4.wav"; //base string is set to middle c
if (decide == 1)
x = "src/c4.wav";
if (decide == 2)
x = "src/d4.wav";
if (decide == 3)
x = "src/e4.wav";
if (decide == 4)
x = "src/f4.wav";
if (decide == 5)
x = "src/g4.wav";
if (decide == 6)
x = "src/a4.wav";
if (decide == 7)
x = "src/b4.wav";
if (decide == 8)
x = "src/c5.wav";
if (decide == 9)
x = "src/d5.wav";
if (decide == 10)
x = "src/e5.wav";
if (decide == 11)
x = "src/f5.wav";
if (decide == 12)
x = "src/g5.wav";
if (decide == 13)
x = "src/a5.wav";
if (decide == 14)
x = "src/b5.wav";
if (decide == 15)
x = "src/c6.wav";
if (decide == 16)
x = "src/d6.wav";
if (decide == 17)
x = "src/e6.wav";
if (decide == 18)
x = "src/db4.wav";
if (decide == 19)
x = "src/eb4.wav";
if (decide == 20)
x = "src/gb4.wav";
if (decide == 21)
x = "src/ab4.wav";
if (decide == 22)
x = "src/bb4.wav";
if (decide == 23)
x = "src/db5.wav";
if (decide == 24)
x = "src/eb5.wav";
if (decide == 25)
x = "src/gb5.wav";
if (decide == 26)
x = "src/ab5.wav";
if (decide == 27)
x = "src/bb5.wav";
if (decide == 28)
x = "src/db6.wav";
if (decide == 29)
x = "src/eb6.wav";
try { //try-catch for the sound file
File csound = new File(x); //use the string file that was decided on in the previous part of the method as the parameter for the new file
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
if (decide == 30)
clip.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//4/17/16
//Final Game Project (PianoGame.Java)
//To make a program that teaches the user how to read sheet music and potentially spark an interest through the process
//This program will use many imports including sound as well as timers and array untilitys.
import java.awt.*;
import java.awt.event.*; //import java envents, timer sound file, io and color
import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import javax.sound.sampled.*;
public class PianoGame extends JFrame{ //JFrame class header
public PianoGame(){
super("PianoGame");
setSize(2000,1000);
setLocation(0,0);
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 header
boolean[] notes = new boolean[30]; //make a boolean array
public PianoGameContent() {
boolean[] notes = new boolean[30]; //initialize and set everything to false
Arrays.fill(notes, Boolean.FALSE);
addKeyListener(this); //add key listeners
setFocusable(true);
requestFocusInWindow();
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //super.paintcomponent
Font f = new Font("Serif", Font.BOLD, 50); //draw Piano
g.setColor(Color.BLACK);
g.setFont(f);
g.drawString("SheetReader101", 300, 150);
for (int i = 1; i < 18; i++) { //for loop to draw white keys and call the sound method
if (notes == true) { //if statement for true
g.setColor(Color.RED); //set color to red and fill the rectangle and then call the sound method
g.fillRect(50 * i, 200, 50, 300);
play(i);
} else { //else statement for false
g.setColor(Color.WHITE);
g.fillRect(50 * i, 200, 50, 300);
}
}
g.setColor(Color.BLACK);
for (int i = 1; i < 18; i++) //re-draw so the outline remains (for loop)
g.drawRect(50 * i, 200, 50, 300);
if (notes[18] == true) { // several if statements for black keys (no distinguishible black key pattern so loops wont work here)
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);
play(20);
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);
play(21);
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);
play(22);
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);
play(23);
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);
play(24);
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);
play(25);
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);
play(26);
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);
play(27);
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);
play(28);
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);
play(29);
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); //re-draw the outline for the black keys so that the colors dont look borderless
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; //use a character and key char along with which booleans have been pressed to see what returns as true/what note has been pressed
x = e.getKeyChar();
if (e.getKeyCode() == KeyEvent.VK_1) //have the number ones up top and these will be the black keys. have it so that if the keycode is equal to a key (that will be decided in the future) is pressed than the certain key will be activated
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;
System.out.println("hi");
repaint();
}
public void keyReleased(KeyEvent e) { //when released everything gets reset to false
Arrays.fill(notes, Boolean.FALSE);
repaint();
play(30);
}
public void keyTyped(KeyEvent e) {
}
public void play(int decide) { //Use loop and array in sync with this method. depdning on the number that is passed to this, the string is set do a different value.
String x = "src/c4.wav"; //base string is set to middle c
if (decide == 1)
x = "src/c4.wav";
if (decide == 2)
x = "src/d4.wav";
if (decide == 3)
x = "src/e4.wav";
if (decide == 4)
x = "src/f4.wav";
if (decide == 5)
x = "src/g4.wav";
if (decide == 6)
x = "src/a4.wav";
if (decide == 7)
x = "src/b4.wav";
if (decide == 8)
x = "src/c5.wav";
if (decide == 9)
x = "src/d5.wav";
if (decide == 10)
x = "src/e5.wav";
if (decide == 11)
x = "src/f5.wav";
if (decide == 12)
x = "src/g5.wav";
if (decide == 13)
x = "src/a5.wav";
if (decide == 14)
x = "src/b5.wav";
if (decide == 15)
x = "src/c6.wav";
if (decide == 16)
x = "src/d6.wav";
if (decide == 17)
x = "src/e6.wav";
if (decide == 18)
x = "src/db4.wav";
if (decide == 19)
x = "src/eb4.wav";
if (decide == 20)
x = "src/gb4.wav";
if (decide == 21)
x = "src/ab4.wav";
if (decide == 22)
x = "src/bb4.wav";
if (decide == 23)
x = "src/db5.wav";
if (decide == 24)
x = "src/eb5.wav";
if (decide == 25)
x = "src/gb5.wav";
if (decide == 26)
x = "src/ab5.wav";
if (decide == 27)
x = "src/bb5.wav";
if (decide == 28)
x = "src/db6.wav";
if (decide == 29)
x = "src/eb6.wav";
try { //try-catch for the sound file
File csound = new File(x); //use the string file that was decided on in the previous part of the method as the parameter for the new file
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
if (decide == 30)
clip.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Last edited: