Small, probably stupid, Java question

2Xtreme21

Diamond Member
Jun 13, 2004
7,044
0
0
Alright so my final project for my AP Computer Science course is done! Well, for the most part. It essentially creates a piano keyboard and using MouseListeners, allows one to click on a key and it plays the tone. Now I thought tonight, why not spruce it up by adding an option to playback a song? Simple enough, I guess. Simply have the program store each tone played (ArrayList of 'tones'), and have a play-back button that calls a method to play each tone again. Yet, alas, I can't get it do what I want it to do.

I'm unable to let the method know that does all of the action that an ActionEvent occurred.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GUI extends JFrame implements MouseListener, ActionListener{

// un-important variables.
public ArrayList song = new ArrayList();
JButton playButton = new JButton();

public GUI(String title)
{
super(title);

// -------

//Add play button
getContentPane().add(playButton);
playButton.setPreferredSize(new Dimension(25,25));
playButton.setLocation(0,0);

playButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
// HOW DO I LET THE "allTheAction" METHOD KNOW
// THAT I CLICKED?
}

// keyRef is a reference point I use for determining how far away the keys are from
// a place clicked (essentially the user needs to create his own constant point to play).

private void allTheAction(Point point, Point keyRef)
{

if(noteCount > 0){
Tone myTone = new Tone(point, keyRef);
noteCount++;


if (myTone.play() == true)
{
song.add(myTone);
if(/* THE ACTION IS PERFORMED */)
myTone.playBack(song);
}
}
else
noteCount++;


}


// TONE CLASS

import java.awt.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.util.*;

public class Tone extends JFrame
{
// Variables

public Tone(Point xy, Point keyRef) {
// Constructor
}


public boolean play(){

// All the code to make the notes play.


// If a note is played, then it's going to return true
// so that the note is added to the ArrayList song in the GUI class.

if(inWhite == true || inBlack == true)
return true;
else
return false;

}


/**
* This method will be passed the ArrayList song and play each tone
* back once per time it was initially played.
*
* @param song The passed ArrayList including all of the tones played.
*/

public void playBack(ArrayList song)
{

for(int n = 0; n <= song.size(); n++)
{
((Tone) song.get(n)).play();
}

}

// -----------------------

As you can kinda see with my snipets, I want to get the action from the JButton and some way let the allTheAction (yeah I know, I'll rename it later.. it's not due til Monday :p) method know that the button was clicked. Once I have that, I can simply put it in to that If statement and then it'll run through the playback.

Thanks in advance, and sorry for the long, yucky post.

Hmm, I was just looking at the last bit of code I have where the playBack method calls the play method. Since the play method is a boolean, I'll need it to return something. I guess I can just fill up an array of boolean variables that does nothing or something like that.

boolean doAbsolutelyNothing[] = new boolean[9999]

doAbsolutelyNothing[n] = ((Tone) song.get(n)).play();

:p *shrugs*
 
Jun 6, 2005
194
0
0
im not completely sure i'm following what you're asking, because i was under the impression that you already implemented the actionPerformed method which you have to code because you're implementing ActionListener interface, but i guess you haven't?

well basically the mouse/keyboard click is read as the ActionEvent e object which is declared as a parameter in your actionPerformed method. and all you really have to do is to dereference it to an Object type.

so the code would look something down this line

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == playButton) {
allTheAction();
}
...
}

i dont know how much of a help this will be since your project is due today, but you still have a few hours left