// **** Sax Player applet ***** // ***** This applet by M. Gallant 7/02/96 ****** import java.awt.*; public class saxplay extends java.applet.Applet implements Runnable { Image offIm; Graphics offGr; Thread runner; Color fgnd , bgnd; Image sax; Image[] note = new Image[5] ; Image curIm ; int w,h, imRand, rate, notePlay; int xnote,ynote, xnotec, ynotec; //position to draw note. boolean initIm; MediaTracker tracker; public void init() { initIm = true ; bgnd = new Color(221,187,221) ; setBackground(bgnd) ; setForeground(bgnd) ; w = this.size().width; h = this.size().height; offIm = createImage(w, h); offGr = offIm.getGraphics(); offGr.setColor(bgnd) ; tracker = new MediaTracker(this) ; sax = getImage(getCodeBase(), "saxman5.gif") ; tracker.addImage(sax, 0) ; // track background separately. note[0] = getImage(getCodeBase(), "whinote.gif") ; note[1] = getImage(getCodeBase(), "blunote.gif") ; note[2] = getImage(getCodeBase(), "rednote.gif") ; note[3] = getImage(getCodeBase(), "yelnote.gif") ; note[4] = getImage(getCodeBase(), "grenote.gif") ; for(int i=0; i<=4; i++) tracker.addImage(note[i], 1) ; xnote=115; ynote=50; rate=1 ; } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void pause(int time) { try { Thread.sleep(time);} // wait for time milliseconds. catch (InterruptedException e) { } } public void run() { repaint() ; try{ tracker.waitForID(0) ; } catch (InterruptedException e) { return; } while (true) { curIm=sax ; rate = 1 +(int)(3.0*Math.random()) ; notePlay = (int)(4.99*Math.random()) ; ynotec=40; for(xnotec=105; xnotec<=135; xnotec+=rate){ //move that note. ynotec-=2*rate; xnote=xnotec + (int)(4*Math.sin(xnotec)) ; ynote=ynotec ; repaint() ; pause(100) ; } pause(200) ; } } public void update(Graphics g) { paint(g) ; } public void paint(Graphics g) { if ((tracker.statusID(1, true) == MediaTracker.COMPLETE)&&(curIm!=null)) { offGr.fillRect(0,0,w,h) ; offGr.drawImage(sax,0,0,this) ; offGr.drawImage(note[notePlay],xnote,ynote,this) ; g.drawImage(offIm,0,0,this) ; } else if (tracker.statusID(0, false) == MediaTracker.COMPLETE) { offGr.fillRect(0,0,w,h) ; offGr.drawImage(sax,0,0,this) ; g.drawImage(offIm,0,0,this) ; } else { g.setColor(bgnd) ; g.fillRect(0,0,w,h) ; } } public String getAppletInfo() { return "Copyright 1996 Michel Gallant"; } }