• Our Minecraft servers are offline but we will keep this forum online for any community communication. Site permissions for posting could change at a later date but will remain online.

piano program proto1

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();
}
}
}
 
Last edited:

Speech

Platinum
Joined
Aug 11, 2013
Messages
1,019
Reaction score
1,963
I agree completely man. Well written as well!
 
Joined
Sep 8, 2013
Messages
427
Reaction score
833
I agree completely man. Well written as well!
LOOOL I didnt think anyone would see this it's just that I wanted to show what I had so far for a program and it wouldn't let me send it over skype or over TS so I just put it on here xD
 

Speech

Platinum
Joined
Aug 11, 2013
Messages
1,019
Reaction score
1,963
LOOOL I didnt think anyone would see this it's just that I wanted to show what I had so far for a program and it wouldn't let me send it over skype or over TS so I just put it on here xD
lol I have no idea what it is or what it means xd
 
Joined
Sep 8, 2013
Messages
427
Reaction score
833
lol I have no idea what it is or what it means xd
LMAO its a program in Java, its my first step in the game that Im making. basically its a piano but all it does right now is light up a certain key depending on the key that is pressed.
 

Param

Quantum
Joined
Oct 25, 2014
Messages
842
Reaction score
520
Looks pretty decent, to much unnecessary code, could be shortened quite a bit . I just have a small pet peeve, please don't make every Boolean on a different line..... just put it on one line like this:

boolean a, bb, etc;
 
Joined
Sep 8, 2013
Messages
427
Reaction score
833
Looks pretty decent, to much unnecessary code, could be shortened quite a bit . I just have a small pet peeve, please don't make every Boolean on a different line..... just put it on one line like this:

boolean a, bb, etc;

Yeah ik there's some bug with my IDE when I do that. At school when I do that it runs fine but at home it says "This variable might not have been initialized" idk. Also since you know java I could use a bit of help: I made the audio file and re-edited the post. For some reason when I hit the key the sound plays but then the program freezes and crashes. Don't know whats going on you mind helping me out?

Also I'm planning to change a lot of this base code and shorten it and have different versions for different purposes. Right now I'm just making the piano in free play mode but later on I'll have it teach things like reading notes

Also Ceroria if anything is lit it isn't my coding capability LOOL
 

namespace

Tribute
Joined
Feb 20, 2016
Messages
23
Reaction score
10
importjava.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import java.io.IOException;
import javax.swing.*;
import java.io.File;
import javax.sound.sampled.*;


public class PianoGame extends JFrame{
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 {

private boolean entered;
private int firstTime;
boolean a;
boolean b;
boolean bb;
boolean c;
boolean db;
boolean d;
boolean eb;
boolean e1;
boolean f2;
boolean gb;
boolean g1;
boolean ab;
boolean a1;
boolean b1;
boolean bb1;
boolean c1;
boolean db1;
boolean d1;
boolean eb1;
boolean e11;
boolean f1;
boolean gb1;
boolean g11;
boolean ab1;
boolean a11;
boolean b11;
boolean c11;
boolean db11;
boolean eb11;


public PianoGameContent() {
entered = false;
firstTime = 0;
a = false;
b = false;
bb = false;
c = false;
db = false;
d = false;
eb = false;
e1 = false;
f2 = false;
gb = false;
g1 = false;
ab = false;
a1 = false;
bb1 = false;
b1 = false;
c1 = false;
db1 = false;
d1 = false;
eb1 = false;
e11 = false;
f1 = false;
gb1 = false;
g11 = false;
ab1 = false;
a11 = false;
b11 = false;
c11 = false;
db11 = false;
eb11 = false;
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
SoundHandler mg = new SoundHandler();
Font f = new Font("Serif", Font.BOLD, 50);
g.setFont(f);
if (a == true) {
g.setColor(Color.RED);
mg.play();
g.fillRect(50, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(50, 200, 50, 300);
}
if (b == true) {
g.setColor(Color.RED);
g.fillRect(100, 200, 50, 300);
mg.play2();
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.drawRect(100, 200, 50, 300);
}

if (c == true) {
g.setColor(Color.RED);
g.fillRect(150, 200, 50, 300);
mg.play3();
g.setColor(Color.BLACK);
} else {
g.setColor(Color.BLACK);
g.drawRect(150, 200, 50, 300);
}
if (d == true) {
g.setColor(Color.RED);
g.fillRect(200, 200, 50, 300);
mg.play4();
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(200, 200, 50, 300);
}
if (e1 == true) {
g.setColor(Color.RED);
g.fillRect(250, 200, 50, 300);
mg.play5();
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(250, 200, 50, 300);
}
if (f2 == true) {
g.setColor(Color.RED);
g.fillRect(300, 200, 50, 300);
mg.play6();
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(300, 200, 50, 300);
}


if (g1 == true) {
g.setColor(Color.RED);
g.fillRect(350, 200, 50, 300);
mg.play7();
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(350, 200, 50, 300);
}
if (a1 == true) {
g.setColor(Color.RED);
g.fillRect(400, 200, 50, 300);
mg.play8();
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(400, 200, 50, 300);
}
if (b1 == true) {
g.setColor(Color.RED);
g.fillRect(450, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(450, 200, 50, 300);
}


if (c1 == true) {
g.setColor(Color.RED);
g.fillRect(500, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(500, 200, 50, 300);
}

if (d1 == true) {
g.setColor(Color.RED);
g.fillRect(550, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(550, 200, 50, 300);
}

if (e11 == true) {
g.setColor(Color.RED);
g.fillRect(600, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(600, 200, 50, 300);
}

if (f1 == true) {
g.setColor(Color.RED);
g.fillRect(650, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(650, 200, 50, 300);
}

if (g11 == true) {
g.setColor(Color.RED);
g.fillRect(700, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(700, 200, 50, 300);
}
if (a11 == true) {
g.setColor(Color.RED);
g.fillRect(750, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(750, 200, 50, 300);
}
if (b11 == true) {
g.setColor(Color.RED);
g.fillRect(800, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(800, 200, 50, 300);
}
if (c11 == true) {
g.setColor(Color.RED);
g.fillRect(850, 200, 50, 300);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.drawRect(850, 200, 50, 300);
}
if (bb == true) {
g.setColor(Color.RED);
g.fillRect(85, 200, 30, 175);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.fillRect(85, 200, 30, 175);
}

if (db == true) {
g.setColor(Color.RED);
g.fillRect(135, 200, 30, 175);
g.setColor(Color.BLACK);

} else {
g.setColor(Color.BLACK);
g.fillRect(135, 200, 30, 175);
}
if (eb == 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 (gb == 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 (ab == 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 (bb1 == 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 (db1 == 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 (eb1 == 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 (gb1 == 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 (ab1 == 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 (db11 == 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 (eb11 == 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.setColor(Color.BLACK);
g.drawRect(100, 200, 50, 300);
g.drawRect(150, 200, 50, 300);
g.drawRect(200, 200, 50, 300);
g.drawRect(250, 200, 50, 300);
g.drawRect(300, 200, 50, 300);
g.drawRect(350, 200, 50, 300);
g.drawRect(400, 200, 50, 300);
g.drawRect(450, 200, 50, 300);
g.drawRect(500, 200, 50, 300);
g.drawRect(550, 200, 50, 300);
g.drawRect(600, 200, 50, 300);
g.drawRect(650, 200, 50, 300);
g.drawRect(700, 200, 50, 300);
g.drawRect(750, 200, 50, 300);
g.drawRect(800, 200, 50, 300);
g.drawRect(850, 200, 50, 300);
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1)
bb = true;
if (e.getKeyCode() == KeyEvent.VK_2)
db = true;
if (e.getKeyCode() == KeyEvent.VK_3)
eb = true;
if (e.getKeyCode() == KeyEvent.VK_4)
gb = true;
if (e.getKeyCode() == KeyEvent.VK_5)
ab = true;
if (e.getKeyCode() == KeyEvent.VK_6)
bb1 = true;
if (e.getKeyCode() == KeyEvent.VK_7)
db1 = true;
if (e.getKeyCode() == KeyEvent.VK_8)
eb1 = true;
if (e.getKeyCode() == KeyEvent.VK_9)
gb1 = true;
if (e.getKeyCode() == KeyEvent.VK_0)
ab1 = true;
if (e.getKeyCode() == KeyEvent.VK_UP)
db11 = true;
if (e.getKeyCode() == KeyEvent.VK_DOWN)
eb11 = true;
repaint();

}

public void keyReleased(KeyEvent e) {
a = false;
b = false;
bb = false;
c = false;
db = false;
d = false;
eb = false;
e1 = false;
f2 = false;
gb = false;
g1 = false;
ab = false;
a1 = false;
b1 = false;
bb1 = false;
c1 = false;
db1 = false;
d1 = false;
eb1 = false;
e11 = false;
f1 = false;
gb1 = false;
g11 = false;
ab1 = false;
a11 = false;
b11 = false;
c11 = false;
db11 = false;
eb11 = false;
repaint();
}

public void keyTyped(KeyEvent e) {
char x;
x = e.getKeyChar();
if (x == 'q')
a = true;
if (x == 'w')
b = true;
if (x == 'e')
c = true;
if (x == 'r')
d = true;
if (x == 't')
e1 = true;
if (x == 'y')
f2 = true;
if (x == 'u')
g1 = true;
if (x == 'i')
a1 = true;
if (x == 'o')
b1 = true;
if (x == 'p')
c1 = true;
if (x == 'a')
d1 = true;
if (x == 's')
e11 = true;
if (x == 'd')
f1 = true;
if (x == 'f')
g11 = true;
if (x == 'g')
a11 = true;
if (x == 'h')
b11 = true;
if (x == 'j')
c11 = true;
repaint();
}
}

class SoundHandler extends Thread{
public void play() {
try {
File csound = new File("src/c4.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play2() {
try {
File csound = new File("src/d4.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play3() {
try {
File csound = new File("src/e4.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play4() {
try {
File csound = new File("src/f4.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play5() {
try {
File csound = new File("src/g4.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play6() {
try {
File csound = new File("src/a5.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play7() {
try {
File csound = new File("src/b5.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void play8() {
try {
File csound = new File("src/c5.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(csound));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
A few things:

1) Declaring variables like that is pretty awful, no offense. Just use a boolean list and the list index can act as the key. Better yet, just a list of the keys currently pressed. Don't just do what Param said.

2) Use switch statements instead of if statements. If I were doing this though, I'd create a map of KeyEvents -> Key/List index and just get the corresponding Key/List index from the key pressed.

3) A huge amount of your Graphics method calls can be replaced with for loops.

4) The sound is continuously played whilst the key is pressed (Is this intended? Hard to tell without the sound files). If this isn't intended, only call play() when a key is pressed.

5) Instead of all the play() methods, just use one and pass in the sound file as an argument. They all do the same thing.

From a quick glance at your code, those are some things that really should be changed.
 
Joined
Sep 8, 2013
Messages
427
Reaction score
833
A few things:

1) Declaring variables like that is pretty awful, no offense. Just use a boolean list and the list index can act as the key. Better yet, just a list of the keys currently pressed. Don't just do what Param said.

2) Use switch statements instead of if statements. If I were doing this though, I'd create a map of KeyEvents -> Key/List index and just get the corresponding Key/List index from the key pressed.

3) A huge amount of your Graphics method calls can be replaced with for loops.

4) The sound is continuously played whilst the key is pressed (Is this intended? Hard to tell without the sound files). If this isn't intended, only call play() when a key is pressed.

5) Instead of all the play() methods, just use one and pass in the sound file as an argument. They all do the same thing.

From a quick glance at your code, those are some things that really should be changed.
1) My IDE won't let me do so :(

2) What's a switch statement, never learned that?

3)I'm probably going to use arrays but not loops because a lot of modification needs to be done and there are certain variables that just won't work with loops for the idea I have in mind, but I know what you're getting at

4) It's intended

5) Going to do that it's a lot better and saves a lot of time and space. I think that thread.sleep and getting the microsecond length is the problem here going to fix that

Thanks for the help, appreciated :) I'm new to programming so I'm not the best :p
 

namespace

Tribute
Joined
Feb 20, 2016
Messages
23
Reaction score
10
1) My IDE won't let me do so :(

2) What's a switch statement, never learned that?

3)I'm probably going to use arrays but not loops because a lot of modification needs to be done and there are certain variables that just won't work with loops for the idea I have in mind, but I know what you're getting at

4) It's intended

5) Going to do that it's a lot better and saves a lot of time and space. I think that thread.sleep and getting the microsecond length is the problem here going to fix that

Thanks for the help, appreciated :) I'm new to programming so I'm not the best :p
1) Sorry, but how can an IDE prevent you from initializing lists?

2) Rather than having a sequence of if(x == value) statements where x is constant, you can concatenate them into a switch statement. They're semantically different but function similarly, and switch statements will be a little faster in your case and will look a lot cleaner. This should be in your keyPressed() method, I should have been a bit clearer in my previous post.

3) You can use arrays in conjunction with loops. Have an array of the starting x/y co-ordinates of each of the rectangles you want to draw and loop over the array. I can't see how using arrays alone will reduce the number of Graphics function calls.
 
Last edited:
Joined
Sep 8, 2013
Messages
427
Reaction score
833

1) Sorry, but how can an IDE prevent you from initializing lists?

2) Rather than having a sequence of if(x == value) statements where x is constant, you can concatenate them into a switch statement. They're semantically different but function similarly, and switch statements will be a little faster in your case and will look a lot cleaner. This should be in your keyPressed() method, I should have been a bit clearer in my previous post.

3) You can use arrays in conjunction with loops. Have an array of the starting x/y co-ordinates of each of the rectangles you want to draw and loop over the array. I can't see how using arrays alone will reduce the number of Graphics function calls.

That's exactly the question I have. mb it's something with my pc or something but it always says "This variable might not have been initialized. Might re-download it or download another one/update JDK, because I can do it at school but not on my pc for some reason. I'll also what you said in number 2, I know know what you're getting at.

Using arrays will make it easier for other people to help me with my code and while I do plan to use loops I only know now for a fact that this will 100% work with my end product. While a loop very well may work, I just don't want to run into some stupid reason why I can't make what I want to work. Sounds weird but I'd have to write a whole bunch on why. Towards the end if it all goes correctly I'll replace all of my paintComponent method with a loop and also thanks for the help, really do appreciate it :)
 

Members online

No members online now.

Forum statistics

Threads
242,193
Messages
2,449,633
Members
523,972
Latest member
Atasci