I have been working on a music notation software that I want to use to train my music students on sight-reading.
I want to know whether I am taking the right approach or if there is a better way.
I have managed to make the clef, notes and staff objects using unicodes and fonts that support music notation using code like this:
public static void spaceStaff(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Bravura", Font.PLAIN, 32);
g2.setFont(font);
g2.drawString("\uD834\uDD1A", note.spacing-2, staffDistance);
note.spacing = note.spacing + 16;
}
I then have a class called Surface that paints the notes onto a JPanel:
public class Surface extends JPanel {
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
staff.spaceStaff(g);
clef.drawGclef(g);//not given in example code above
note.drawCrotchet(g, note.B1);//not given in example code above
}
}
I am using this code to start the application and display the music notes:
public class SightreadHelper extends JFrame {
public SightreadHelper(){
initUI();
}
private void initUI() {
JButton button = new JButton("add notes"); //explanation for this button below
button.setSize(150, 75);
button.setVisible(true);
add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
repaint();//this doesn't work as I expect: explanation below
}
});
Surface srf = new Surface();
add(new Surface());
setSize(700, 1000);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SightreadHelper srh = new SightreadHelper();
srh.setVisible(true);
}
});
}
}
The button is supposed to add the notes at the end of the existing notes, but it doesn't.
Am I using the right procedure for doing what I want to do? Thanks a lot for your help. I saw this question but it didnt help me: Repaint Applets in JAVA without losing previous contents