| Author |
Message |
|
|
Thanks for the help so far. Im still getting a bit stuck. Is there anything u can do to make this program work. I know i have to do something with the x co-ord of the note as it will print in the same place. Much appreciated.
Work so far:
import java.util.StringTokenizer;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Color;
/**
* Write a description of class MusicSheet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MusicSheet extends JFrame {
private int placement; // This indicated the postion of the notes in the y co-od.
public MusicSheet(){ // Builds the frame with a set size and colour and gives a title.
setTitle("This is Ollie's Music Sheet");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(600,400));
this.getContentPane().setBackground(Color.black);
setVisible(true);
}
public void drawStaff(Graphics g) { //draws staff lines.
g.setColor(Color.white);
for (int i=0; i<5; i++)
g.drawLine(100,220+30*1,400,150+30*i);
}
public void paint(Graphics g){ // Screen refresh to draw notes and staff
super.paint(g);
this.drawStaff(getContentPane().getGraphics());
this.drawNote(getContentPane().getGraphics());
}
public void drawNote(Graphics g,int notePosition){
g.setColor(Color.white);
g.setFont(new Font("MS PMincho", Font.PLAIN, 4));
g.drawString("" + '\u2669', 75, 275-10*(this.notePosition));
}
public int notePosition(String input){ // matches user input with charcter and returns value for y co-ord of note.
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;
String input;
while (st.hasMoreTokens()){
input=st.nextToken();
if (input.equals("a")) {
return a;
} else if (input.equals("b")) {
return b;
} else if (input.equals("c")) {
return c;
} else if (input.equals("d")) {
return d;
} else if (input.equals("e")) {
return e;
} else if (input.equals("f")) {
return f;
} else if (input.equals("g")) {
return g;
} else if (input.equals("A")) {
return A;
} else if (input.equals("B")) {
return B;
} else if (input.equals("C")) {
return C;
} else if (input.equals("D")) {
return D;
} else if (input.equals("E")) {
return E;
} else if (input.equals("F")) {
return F;
} else if (input.equals("G")) {
return G;
}
return (0); // indicates a word we couldn't find
}
}
public static void main(String[] args){
MusicSheet mySheet = new MusicSheet();
System.out.println("Enter a sequence of notes: ");
String character = Keyboard.readString();
StringTokenizer st = new StringTokenizer(character);
}
}
Here is ther keyboard class im using.
/** Keyboard.java
* Special class for simplifying reading of keyboard input
* Provides methods for reading each of the primitive types from one line of input only
* Also reads whole line as a String.
*
*
*
* Additionally, this class provides a helper function replace for replacing a character
* in a given position in a string with another.
*/
import java.io.*;
import java.lang.NumberFormatException;
public class Keyboard {
/**
* Read a string from the keyboard
* @return String the string entered (sans newline)
*/
public static String readString() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
String str = "";
try {
str = br.readLine();
}
catch (IOException ioe) { System.out.println(ioe); }
return str;
}
/**
* Read a line from the keyboard an interpret it as an int value
* @return int the int interpretation of the input string
*/
public static int readInt() { return Integer.parseInt(readString()); }
/**
* Read a line from the keyboard an interpret it as a byte value
* @return byte the byte interpretation of the input string
*/
public static byte readByte() { return Byte.parseByte(readString()); }
/**
* Read a line from the keyboard an interpret it as a short value
* @return short the short interpretation of the input string
*/
public static short readShort() { return Short.parseShort(readString()); }
/**
* Read a line from the keyboard an interpret it as a long value
* @return long the long interpretation of the input string
*/
public static long readLong() { return Long.parseLong(readString()); }
/**
* Read a line from the keyboard an interpret it as a float value
* @return float the float interpretation of the input string
*/
public static float readFloat() { return Float.parseFloat(readString()); }
/**
* Read a line from the keyboard an interpret it as a double value
* @return double the double interpretation of the input string
*/
public static double readDouble() { return Double.parseDouble(readString()); }
/**
* Read a line from the keyboard an interpret it as a char value
* @return char the char interpretation of the input string
*/
public static char readChar() { return readString().charAt(0) ; }
/**
* Read a line from the keyboard an interpret it as a boolean value
* @return boolean the boolean interpretation of the input string
*/
public static boolean readBoolean() { return Boolean.valueOf(readString()).booleanValue(); }
// Utitily method replace
// Nothing to do with keyboard input. Simplifies Assignment 1
/**
* Generates a new string by replacing the character in the given position of the string with the given character
*
* @param s the string to replace the character in
* @param i the position of the character to replace
* @param c the new character to use
* @return String a new string with the character in position i of s replaced by c
*/
public static String replace(String s, int i, char c) {
return ( s.substring(0,i) + c + s.substring(i+1) );
}
/**
* Determine whether or not a given string represents an integer value
*
* @param s the string
* @return boolean is it an integer
*/
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) { return false; }
}
}
Thanks
|
 |
|
|
[b] Can anyone help me ? Basically i am writing a program that creates a music sheet with notes from a string input. I am only using notes from a-g and A-G . The input must be a string then use a tozenizer to break down. I have down as much as i can do but now im soooo stuck.
If anyone has any hints or help it would be much appreciated.
below is what i have done so far but it doesnt compile. For the notes in the y co-od i have made some random numbers for now.
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, 4 );
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;
}
}
/**********************************************************
*
* ADD METHODS HERE IF DESIRED
*
**********************************************************/
// 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();
}
}
|
 |
|
|
|
|