Hi everyone, Just thought i would post my program im doing at the moment as im completely stuck. Basically the program takes a user input as a string, uses the tokenizer to break down. The individual strings which will be single characters will be used to draw notes on a music staff using unicode. Can anyone help with this code as im really getting annoyed now. Thanks

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


/**
* A program that uses a frame for drawing musical notes on a staff
*
* @author PAS
* @version (18/10/05)
*/


public class FrameSkeleton extends JFrame {


private int howMany;        // this is a class instance variable that affects the picture that is drawn
private int notePosition;


public FrameSkeleton() {
setTitle("Musical Staff");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize(new Dimension(600,400));


this.getContentPane().setBackground(Color.black);


setVisible(true);
}


public void paint(Graphics g) {
super.paint(g);
this.drawStaff(getContentPane().getGraphics());
}


// This draws the a note using position vertical to guide placement
public void drawNote(Graphics g) {


g.setColor(Color.black);
g.setFont( new Font("MS PMincho", Font.PLAIN, 48)   );
g.drawString("" + '\u2669', 75, 275-10*notePosition);
}


public void drawStaff(Graphics g) {


g.setColor(Color.white);
for (int i = 0; i < this.howMany; i++)
g.drawLine(25,125+30*i,570,125+30*i);


}


public void userInput (){
System.out.println("Enter a sequence of notes: ");
String sent = Keyboard.readString();
StringTokenizer st = new StringTokenizer(sent);
String word;
while (st.hasMoreTokens()){
word = st.nextToken();
}
}


int notePosition(){
this.userInput();


int a=30;
int b=40;
int c=50;
int d=60;
int e=70;
int f=80;
int g=90;
int A=100;
int B=110;
int C=120;
int D=130;
int E=140;
int F=150;
int G=160;



for (int i=0; i<=sent.length();i++) {


if (word.equals("a")){
word = st.nextToken();
return a;


}else if (word.equals("b")){
word = st.nextToken();
return b;


}else if (word.equals("c")){
word = st.nextToken();
return c;


}else if (word.equals("d")){
word = st.nextToken();
return d;


}else if (word.equals("e")){
word = st.nextToken();
return e;


}else if (word.equals("f")){
word = st.nextToken();
return f ;


}else if (word.equals("g")){
word = st.nextToken();
return g;


}else if (word.equals("A")){
word = st.nextToken();
return A;


}else if (word.equals("B")){
word = st.nextToken();
return B;


}else if (word.equals("C")){
word = st.nextToken();
return C;


}else if (word.equals("D")){
word = st.nextToken();
return D;


}else if (word.equals("E")){
word = st.nextToken();
return E;


}else if (word.equals("F")){
word = st.nextToken();
return F;


}else if (word.equals("G")){
word = st.nextToken();
return G;


}
}


:eek:


// This refreshes the screen and draws the staff and note
public void paint(Graphics g) {
super.paint(g);
this.drawStaff(getContentPane().getGraphics());
this.drawNote(getContentPane().getGraphics());
}



/* Example update method below */
//public void setHowMany(int h) {
// this.howMany = h;
// this.repaint();
//}


public static void main(String [] args) {


FrameSkeleton myFrame = new FrameSkeleton();
myFrame.setHowMany(5);


System.out.println("Enter a sequence of notes: ");
String sent = Keyboard.readString();


FrameSkeleton input = new FrameSkeleton();
input.notePosition();
FrameSkeleton create = new FrameSkeleton();
create.drawNote();


}
}

OllieFalle,

Here are a few things that seem incorrect to me (correct me if I am wrong):

1. Your method called userInput shown below has problems

public void userInput ()
{
	System.out.println("Enter a sequence of notes: ");
	String sent = Keyboard.readString();
	StringTokenizer st = new StringTokenizer(sent);
	String word; 
	while (st.hasMoreTokens())
	{
		word = st.nextToken();
	}
}

Notice that it reads through the tokens and stores each in the variable word. Therefore, at the end, word holds the last token. All the other tokens were discarded.

What you should do is tokenize and store the results in an ArrayList.

2. Your drawNote method always draws the same character at the same x position. Is this what you want?

public void drawNote(Graphics g) 
{
	g.setColor(Color.black);
	g.setFont( new Font("MS PMincho", Font.PLAIN, 48) );
	g.drawString("" + '\u2669', 75, 275-10*notePosition);
}

3. Your notePosition method seems to have a compile error. I don't see a StringTokenizer declared in that method or a instance variable. The only place it is created is in the userInput method but you can't access it from the notePosition method.

What do you want this method to do?

:?: For more help, removed link to homework pay site

Thanks for the tip. Yeh i see that i have made a mistake with the x postion of the note. What i want to do is draw the note at a new position each time. Do u have any ideas for this ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.