import java.awt.*; import java.applet.*; import java.net.URL; public class Anastasia extends java.applet.Applet { String s; Image[] symbol; String[] symfile = {"treble-staff-tr.GIF","bass-staff-tr.GIF", "barline-tr.gif", "wholenote-tr.gif", "halfnote-tr.gif", "qtrnote-tr.gif", "8thnote-tr.gif", "wh-half-rest-tr.gif", "qtrrest-tr.gif", "8threst-tr.gif", "dot-tr.gif"}; String[] symId = {"treble","bass","bar","wn","hn","qn","en","hr","qr","er","dot"}; // these offsets have to be determined experimentally for each GIF. They are the values added to the // X and Y coordinates relative to the Treble staff position to make the shape fit in the bottom space // of the treble staff ("F"), or to properly align the symbol in its default position. int[] xOffset = {0, 5, 30, 30, 30, 30, 30, 30, 30, 30, 43}; int[] yOffset = {0, 80, 27, 48, 27, 27, 27, 33, 27, 33, 52}; int numImages; public void init() { // load images numImages = symfile.length; symbol = new Image[numImages]; URL url = getCodeBase(); for (int ix=0; ix < numImages; ix++) { symbol[ ix ] = getImage (url, symfile[ix]); } MediaTracker mt = new MediaTracker (this); for (int ix=0; ix < numImages; ix++) { mt.addImage (symbol[ix], ix); } try { mt.waitForAll(); } catch (Exception e) { System.err.println(e); } // test to see if any files didn't load: for (int ix=0; ix < numImages; ix++) { if (symbol[ix].getHeight(this) < 0) { System.err.println ("Image file empty: "+symfile[ix]); } } } public void drawSym (Graphics g, String symbolId, int x, int y) { for (int ix=0; ix < numImages; ix++) { if (symbolId == symId[ix]) { g.drawImage(symbol[ix], x + xOffset[ix], y + yOffset[ix], this); } } } public void paint(Graphics g) { int trebleX = 10, trebleY = 10; g.setColor(Color.white); g.fillRect(0,0,800,300); g.setColor(Color.black); drawSym (g, "treble", trebleX, trebleY); drawSym (g, "bass", trebleX, trebleY); drawSym (g, "wn", trebleX+10, trebleY); drawSym (g, "hn", trebleX+30, trebleY); drawSym (g, "qn", trebleX+50, trebleY); drawSym (g, "en", trebleX+70, trebleY); drawSym (g, "hr", trebleX+90, trebleY-3); // to make a whole rest drawSym (g, "hr", trebleX+110, trebleY); drawSym (g, "qr", trebleX+130, trebleY); drawSym (g, "er", trebleX+150, trebleY); drawSym (g, "hn", trebleX+170, trebleY); drawSym (g, "dot",trebleX+170, trebleY); drawSym (g, "bar", trebleX+190, trebleY); } }