/* Erez's first attemt at building a java operated mechanical calculator */ import java.awt.*; import java.awt.Frame; import java.applet.Applet; import java.applet.AudioClip; import java.lang.Math; import java.lang.Boolean; import java.awt.image.*; //================================================================= public class felt extends Applet //================================================================= { public static Image bg_pic; public static Image down_pic; public static Image tmp_pic; public static Image full_pic; public static Image animate_bg; public static Image animate[]; Frame my_frame; AudioClip click_au, bell_au; public static Applet my_app; public static boolean crop = false; public static boolean First = true; public static boolean debug = false; public static boolean Zero = false; public static boolean Click = false; public static int displayVal = 0; public static int lastVal =9; public static int currX =0; public static int currY =0; public static final int frameNums =6; public static final int aniWidth = 36; public static final int aniHight = 142; public static final int aniGap = 40; //================================ void main(String argv[]){ init(); } //================================================================= public void init (){ //================================================================= MediaTracker tracker; animate = new Image[frameNums]; my_frame = new Frame(); my_frame.addNotify(); String param1 = getParameter("DebugF"); System.out.println("value of DebugF"+ param1 ); if (param1 != null) if (param1.equals("true")) { System.out.println("param1 == true" ); debug = true; } else debug = false; if (debug) System.out.println("before tracker"); showStatus("Loading images..."); tracker = new MediaTracker(this); System.out.println("before getImage felt-full-sharp.jpeg"); full_pic = getImage(getCodeBase(), "felt.jpeg"); //System.out.println("before getImage felt-ani2.jpeg"); //animate_bg = getImage(getCodeBase(), "felt-ani2.jpeg"); System.out.println("before addImage"); tracker.addImage(full_pic, 0); /* //tracker.addImage(animate_bg, 1); try { System.out.println("before waitForID"); tracker.waitForID(0); System.out.println("load bg_pic"); // tracker.waitForID(1); // System.out.println("load animate_bg"); } catch (InterruptedException e) { return; } */ if (debug) System.out.println("after wait"); bg_pic = createImage(new FilteredImageSource(full_pic.getSource(), new CropImageFilter(0,156,256,359))); ImageFilter imgfilter = new CropImageFilter(270,200,18,18); down_pic = createImage(new FilteredImageSource(bg_pic.getSource(),imgfilter)); loadAnimation(); //click_au = getAudioClip(getDocumentBase(), "KeyClick.au"); click_au = getAudioClip(getDocumentBase(), "Launcher.au"); //bell_au = getAudioClip(getDocumentBase(), "Cash_Register.au"); bell_au = getAudioClip(getDocumentBase(), "bell.au"); resize(260,360); First = true; my_app = this; } //================================================================= void loadAnimation() { //================================================================= for (int ctr = 0; ctr < frameNums; ctr++){ animate[ctr] = createImage(new FilteredImageSource(full_pic.getSource(), new CropImageFilter(ctr * aniGap + 1 ,0,aniWidth,aniHight))); } } //================================================================= void playAnimation(Graphics g) { //================================================================= if (debug) System.out.println("playAnimation========"); bell_au.play(); for (int ctr = 0; ctr < frameNums; ctr++){ g.drawImage(animate[ctr], 221, 23, this); try { Thread.sleep(100);} catch (InterruptedException e) { break; } } bell_au.play(); drawResult(0, g); for (int ctr = frameNums-1; ctr >= 0; ctr--){ g.drawImage(animate[ctr], 221, 23, this); try { Thread.sleep(100);} catch (InterruptedException e) { break; } } } //================================================================= public void paint( Graphics g ) { //================================================================= //if (debug) //System.out.println("from Paint"); if (First) { if (!g.drawImage(bg_pic, 10, 10, this)) { System.out.println("!drawImage"); try { Thread.sleep(1000);} catch (InterruptedException e) { } return; } drawResult(displayVal, g); // if (debug) System.out.println("first draw"); First = false; } if (Zero){ playAnimation(g); Zero = false; return; } if (Click){ g.drawImage(down_pic, currX-7, currY-7, this); //for (int x=1; x<2; x++ ) { // try { Thread.sleep(100);} // catch (InterruptedException e) { break; } // } g.drawImage(tmp_pic, currX-7, currY-7, this); Click = false; } if (lastVal != displayVal){ drawResult(displayVal, g); lastVal = displayVal; } } //------------------ public void update(Graphics g){ //------------------ if (debug) System.out.println("update"); paint(g); } //------------------ public int clickVal( int x, int y) { //------------------ if (debug) System.out.println("clickVal"); int xVal = 8 - ((x-42) / 20); if (debug) System.out.println("xVal = " + xVal); int yVal = 9 - ((y-75) / 20); if (debug) System.out.println("yVal = " + yVal); double xD = xVal; return ( (int)Math.pow(10,xVal) * yVal); } //------------------ public boolean mouseDown(Event evt, int x, int y) { //------------------ int retVal = 0; currX = x; currY = y; Zero = false; Click = false; if (debug) { System.out.println("mouseDown"); System.out.println(x); System.out.println(y); } // user has pressed the RESET keys if (inReset(x,y)){ displayVal = 0; // bell_au.play(); if (debug) System.out.println("RESET"); Zero = true; repaint(); return(true); } // click not in selectable area, do nothing if (!inKeys(x,y)) return(true); cropPicture(x, y); retVal = clickVal(x, y); if (retVal >= 0 ) { displayVal += retVal; if (debug) System.out.println("retVal = " + retVal); click_au.play(); Click = true; repaint(); } return(true); } //------------------ public int drawResult(int result, java.awt.Graphics g ) { //------------------ String val = String.valueOf(result); int len = val.length(); String one = new String(); int tmp_val, tmp_result; tmp_result = result; int limit = Math.min(9, len); for (int ctr = 0 ; ctr < limit; ctr++) { tmp_val = tmp_result - ((tmp_result / 10) *10) ; tmp_result = tmp_result / 10; one = String.valueOf(tmp_val); drawOneNumber(one, ctr, g ); } for (int ctr = len ; ctr < 9; ctr++) drawOneNumber("0", ctr, g ); return(0); } //------------------ public boolean handleEvent(Event evt){ //------------------ if (debug) System.out.println("EVENT !!!!! = " + evt.id); // redraw screen incase foucs was changed switch (evt.id) { case Event.MOUSE_ENTER: case Event.LOST_FOCUS: case Event.GOT_FOCUS: { First = true; System.out.println("EVENT restart"); repaint(); return(true); } case Event.MOUSE_MOVE: { if (inReset(evt.x, evt.y)) my_frame.setCursor(Frame.HAND_CURSOR); else if (inKeys(evt.x, evt.y)) my_frame.setCursor(Frame.HAND_CURSOR); else my_frame.setCursor(Frame.DEFAULT_CURSOR); return(true); } } return(super.handleEvent(evt)); } //------------------ boolean inReset(int x, int y){ //------------------ if ((x > 216) && (y > 26) && (x < 256) && (y < 118)) return(true); else return(false); } //------------------ boolean inKeys(int x, int y){ //------------------ if ((x > 64) && (y > 85) && (x < 215) && (y < 245)) return(true); else return(false); } //------------------ int drawOneNumber(String number, int pos, java.awt.Graphics g ) { //------------------ int xPos; int yPos; xPos = 200 - (pos * 20); //yPos = 383 + pos*2; yPos = 270 - (int)(pos * 0.75); g.fillRoundRect(xPos, yPos, 12, 12, 12, 12); g.setColor(Color.white); g.drawString(number, xPos + 3, yPos + 9); g.setColor(Color.black); return(0); } //------------------ int cropPicture(int x, int y) { //------------------ ImageFilter imgfilter = new CropImageFilter(x-17,y-17,18,18); tmp_pic = createImage(new FilteredImageSource(bg_pic.getSource(),imgfilter)); return(0); } /* //============================================================== Thread updater = null; public void start() { if (debug) System.out.println("START"); if (updater == null) { updater = new Thread(this); updater.start(); } } public void stop() { if (updater != null) { updater.stop(); updater = null; } } public void run() { int rotater = 0; while (Thread.currentThread() == updater) { // if (debug) // System.out.println("RUN loop"); // repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } } //============================================================== */ }