Completely Lost : ( Can anyone help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2005
Posts: 5
Reputation: OllieFalle is an unknown quantity at this point 
Solved Threads: 0
OllieFalle OllieFalle is offline Offline
Newbie Poster

Completely Lost : ( Can anyone help

 
0
  #1
Nov 7th, 2005
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();

}
}
:eek: :eek: :eek: :eek:
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: Completely Lost : ( Can anyone help

 
0
  #2
Nov 8th, 2005
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
  1. public void userInput ()
  2. {
  3. System.out.println("Enter a sequence of notes: ");
  4. String sent = Keyboard.readString();
  5. StringTokenizer st = new StringTokenizer(sent);
  6. String word;
  7. while (st.hasMoreTokens())
  8. {
  9. word = st.nextToken();
  10. }
  11. }
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?

  1. public void drawNote(Graphics g)
  2. {
  3. g.setColor(Color.black);
  4. g.setFont( new Font("MS PMincho", Font.PLAIN, 48) );
  5. g.drawString("" + '\u2669', 75, 275-10*notePosition);
  6. }
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
Last edited by alc6379; Jan 13th, 2006 at 12:03 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 5
Reputation: OllieFalle is an unknown quantity at this point 
Solved Threads: 0
OllieFalle OllieFalle is offline Offline
Newbie Poster

Re: Completely Lost : ( Can anyone help

 
0
  #3
Nov 9th, 2005
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 ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC