// // // // // // // // import java.awt.*; import java.applet.*; public class Balance extends Applet implements Runnable { int width,height,degree = 0; int whichshape; int cl = 0, cr = 0, tl = 0, tr = 0, sl = 0, sr = 0; Point clp, crp, tlp, trp, slp, srp; int cw,tw,sw; int history; final int CL = 00, TL = 01, SL = 02, CC = 10, TC = 11, SC = 12, CR = 20, TR = 21, SR = 22; int wins = 0, tries = 0, wrong = 0, weighs = 0; int ix = 0,iy = 0; String status = ""; int statusPlacement = 0; boolean statusTry = false; // Tells whether status is for weight information, false, // or for telling whether or not answer is correct, true boolean play = false; boolean dead = false; double angle = 0; final int A = 25; final int H = 175; Image buffer; Graphics b; Thread t; Button tryout, clear, newgame; // String tryonoff; int dispshape; int CW, TW, SW; boolean tryonoff, weighsonoff; TextField cirt,trit,squt; // Cursor cpoint, cgrab; Font ft_small,ft_medium,ft_large; Shape Obj; AudioClip soundStatus; String winSoundName[] = {"whoopy.au", "cowbell.au", "applause.au", "whistle.au"}; String wrongSoundName[] = {"loser.au", "clang.au", "tireskid.au"}; String winstat[] = { "That's right!", "Good Job! :-)", "Way to go!", "Excellent!", "Great! :^)", "Ladies and Gentlemen, We have a winner!" // "The force is strong with this one!", // "OB-Wan has taught you well!" }; int winstatPlacement[] = {180, 180, 200, 200, 200, 5}; String wrongstat[] = { "Sorry, try again.", "Nope. :-(", "Keep trying." // "Close, but no cigar.", // "Your powers have grown weak, old man." }; int wrongstatPlacement[] = {170, 200, 190}; public void init() { width = size().width; height = size().height; buffer = createImage(width,height); b = buffer.getGraphics(); Obj = new Shape(); setLayout(null); tryout = new Button("Try"); clear = new Button("Clear"); newgame = new Button("New Game"); tryout.setBounds(212,362,75,25); clear.setBounds(350,330,75,25); newgame.setBounds(350,362,75,25); // tryonoff = getParameter("TRY"); String tmpst = getParameter("TRY"); if(tmpst != null && tmpst.equals("DISABLED")) tryonoff = false; else tryonoff = true; tmpst = getParameter("WEIGHS"); if(tmpst != null && tmpst.equals("ENABLED")) weighsonoff = true; else weighsonoff = false; String dispshapet = getParameter("DISPLAYSHAPE"); if(dispshapet == null) dispshape = -1; else if(dispshapet.equals("CIRCLE")) dispshape = 0; else if(dispshapet.equals("TRIANGLE")) dispshape = 1; else if(dispshapet.equals("SQUARE")) dispshape = 2; else dispshape = -1; String CWT,TWT,SWT; CWT = getParameter("Circle"); TWT = getParameter("Triangle"); SWT = getParameter("Square"); if(CWT != null && Integer.parseInt(CWT) > 0 && Integer.parseInt(CWT) <= 5) CW = Integer.parseInt(CWT); else CW = -1; if(TWT != null && Integer.parseInt(TWT) > 0 && Integer.parseInt(TWT) <= 5) TW = Integer.parseInt(TWT); else TW = -1; if(SWT != null && Integer.parseInt(SWT) > 0 && Integer.parseInt(SWT) <= 5) SW = Integer.parseInt(SWT); else SW = -1; cirt = new TextField(1); trit = new TextField(1); squt = new TextField(1); cirt.setBounds(200,330,25,30); trit.setBounds(237,330,25,30); squt.setBounds(275,330,25,30); add(tryout); add(clear); add(newgame); add(cirt); add(trit); add(squt); ft_small = new Font("Times New Roman", Font.BOLD, 12); ft_medium = new Font("Times New Roman", Font.BOLD, 20); ft_large = new Font("Times New Roman", Font.BOLD, 25); //Toolkit tk = Toolkit.getDefaultToolkit(); //Image img = tk.getImage("point.gif"); //cpoint = tk.createCustomCursor(img, new Point(10,15), "black point"); //img = tk.getImage("grab.gif"); //cgrab = tk.createCustomCursor(img, new Point(10,15), "black grab"); clp = new Point(0,0); crp = new Point(0,0); tlp = new Point(0,0); trp = new Point(0,0); slp = new Point(0,0); srp = new Point(0,0); newGame(); } public void start() { if(t == null) { t = new Thread(this); t.start(); } } public void stop() { t = null; } public void run() { Thread current = Thread.currentThread(); int mux = 0; while(t == current) { if(!dead) { int tdeg = weigh(); if(degree != tdeg) { if(tdeg > degree) mux = 1; else mux = -1; do { try { Thread.sleep(100); } catch(InterruptedException e) {} degree += mux; angle = deg2rad(degree); repaint(); } while(degree != tdeg); } } repaint(); } } public void paint(Graphics g) { b.setColor(Color.white); // Clear the panel b.fillRect(0,0,width,height); b.setColor(Color.darkGray); // Make the balance base int px[] = {250,200,300}; int py[] = {100,275,275}; b.fillPolygon(px,py,px.length); drawBalance(b); b.setColor(Color.red); // Draw the center shapes b.fillOval(200,300,25,25); b.setColor(Color.blue); px[0] = 250; py[0] = 300; px[1] = 237; py[1] = 325; px[2] = 262; py[2] = 325; b.fillPolygon(px,py,px.length); b.setColor(Color.green); b.fillRect(275,300,25,25); b.setColor(Color.black); b.setFont(ft_small); if(weighsonoff) { b.drawString("Weighs:",50,300); b.drawString(""+weighs,112,300); } b.drawString("Wins: ",50,325); b.drawString(""+wins,112,325); b.drawString("Tries: ",50,350); b.drawString(""+tries,112,350); b.drawString("Wrong: ",50,375); b.drawString(""+wrong,112,375); b.setFont(ft_large); if(!cirt.isVisible()) b.drawString(""+cw,206,353); if(!trit.isVisible()) b.drawString(""+tw,244,353); if(!squt.isVisible()) b.drawString(""+sw,281,353); b.setFont(ft_small); b.setColor(Color.red); if (statusTry == false) b.drawString(status,140,25); else if (play == true) { b.setFont(ft_large); b.setColor(Color.red); b.drawString(status, statusPlacement, 150); soundStatus.play(); } else { b.setFont(ft_large); b.drawString(status, statusPlacement, 150); } b.setFont(ft_small); play = false; Obj.show(b); g.drawImage(buffer,0,0,this); // Render the double buffer } public void drawBalance(Graphics g) { int beamx[] = new int[4]; int beamy[] = new int[4]; beamx[0] = (int)Math.round(H*Math.cos(angle) + 250); // Calculate beam points beamx[1] = (int)Math.round(-1*H*Math.cos(angle) + 250); beamx[2] = (int)Math.round(-1*(H+A*Math.tan(angle))*Math.cos(angle) + 250); beamx[3] = (int)Math.round((H-A*Math.tan(angle))*Math.cos(angle) + 250); beamy[0] = (int)Math.round(-1*H*Math.sin(angle) + 100); beamy[1] = (int)Math.round(H*Math.sin(angle) + 100); beamy[2] = (int)Math.round((H+A*Math.tan(angle))*Math.sin(angle) - A/Math.cos(angle) + 100); beamy[3] = (int)Math.round(-1*(A/Math.cos(angle)+(H-A*Math.tan(angle))*Math.sin(angle)) + 100); g.setColor(Color.blue); // Draw the beam g.fillPolygon(beamx,beamy,beamx.length); g.setColor(Color.darkGray); // Draw the baskets g.drawLine(beamx[0],beamy[0],beamx[0],beamy[0]+25); g.drawLine(beamx[1],beamy[1],beamx[1],beamy[1]+25); int px[] = {beamx[0], beamx[0]-50, beamx[0]+50}; int py[] = {beamy[0]+25, beamy[0]+125, beamy[0]+125}; g.drawPolygon(px,py,px.length); px[0] = beamx[1]; py[0] = beamy[1]+25; px[1] = beamx[1]-50; py[1] = beamy[1]+125; px[2] = beamx[1]+50; py[2] = beamy[1]+125; g.drawPolygon(px,py,px.length); g.setFont(ft_medium); // Draw the shapes and numbers crp.move(beamx[0]-38, beamy[0]+100); clp.move(beamx[1]-38, beamy[1]+100); trp.move(beamx[0]-13, beamy[0]+100); tlp.move(beamx[1]-13, beamy[1]+100); srp.move(beamx[0]+13, beamy[0]+100); slp.move(beamx[1]+13, beamy[1]+100); if(cr != 0) { g.setColor(Color.red); g.fillOval(crp.x, crp.y,24,24); g.setColor(Color.white); g.drawString(""+cr,beamx[0] - 32, beamy[0] + 120); } if(cl != 0) { g.setColor(Color.red); g.fillOval(clp.x, clp.y,24,24); g.setColor(Color.white); g.drawString(""+cl,beamx[1] - 32, beamy[1] + 120); } if(tr != 0) { g.setColor(Color.blue); px[0] = beamx[0]; py[0] = beamy[0] + 100; px[1] = beamx[0] - 13; py[1] = beamy[0] + 124; px[2] = beamx[0] + 12; py[2] = beamy[0] + 124; g.fillPolygon(px,py,px.length); g.setColor(Color.white); g.drawString(""+tr,beamx[0]-5,beamy[0]+120); } if(tl != 0) { g.setColor(Color.blue); px[0] = beamx[1]; py[0] = beamy[1] + 100; px[1] = beamx[1] - 13; py[1] = beamy[1] + 124; px[2] = beamx[1] + 12; py[2] = beamy[1] + 124; g.fillPolygon(px,py,px.length); g.setColor(Color.white); g.drawString(""+tl,beamx[1]-5,beamy[1]+120); } if(sr != 0) { g.setColor(Color.green); g.fillRect(srp.x,srp.y,24,24); g.setColor(Color.white); g.drawString(""+sr,beamx[0]+19,beamy[0]+120); } if(sl != 0) { g.setColor(Color.green); g.fillRect(slp.x,slp.y,24,24); g.setColor(Color.white); g.drawString(""+sl,beamx[1]+19,beamy[1]+120); } } public void update(Graphics g) { paint(g); } public double deg2rad(double degree) // Convert degrees to radians { return degree * Math.PI / 180; } public boolean mouseDown(Event evt, int x, int y) { /* System.out.println("x = "+x+", y = "+y); System.out.println("cr: "+crp.x+", "+crp.y); System.out.println("cl: "+clp.x+", "+clp.y); System.out.println("tr: "+trp.x+", "+trp.y); System.out.println("tl: "+tlp.x+", "+tlp.y); System.out.println("sr: "+srp.x+", "+srp.y); System.out.println("sl: "+slp.x+", "+slp.y); */ // setCursor(cgrab); statusTry = false; play = false; setStatus(); if(!dead) { if(y >= 300 && y <= 325) { if(x >= 200 && x <= 225) { ix = x - 200; iy = y - 300; history = CC; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.CIRCLE); Obj.setVisible(true); repaint(); } else if(x >= 237 && x <= 263) { ix = x - 237; iy = y - 300; history = TC; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.TRIANGLE); Obj.setVisible(true); repaint(); } else if(x >= 275 && x <= 300) { ix = x - 275; iy = y - 300; history = SC; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.SQUARE); Obj.setVisible(true); repaint(); } } else if(y >= crp.y && y <= crp.y + 25) { if(x >= crp.x && x <= crp.x +25 && cr != 0) { cr--; ix = x - crp.x; iy = y - crp.y; history = CR; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.CIRCLE); Obj.setVisible(true); repaint(); } else if(x >= trp.x && x <= trp.x + 25 && tr != 0) { tr--; ix = x - trp.x; iy = y - trp.y; history = TR; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.TRIANGLE); Obj.setVisible(true); repaint(); } else if(x >= srp.x && x <= srp.x + 25 && sr != 0) { sr--; ix = x - srp.x; iy = y - srp.y; history = SR; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.SQUARE); Obj.setVisible(true); repaint(); } } if(y >= clp.y && y <= clp.y + 25) { if(x >= clp.x && x <= clp.x + 25 && cl != 0) { cl--; ix = x - clp.x; iy = y - clp.y; history = CL; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.CIRCLE); Obj.setVisible(true); repaint(); } else if(x >= tlp.x && x <= tlp.x + 25 && tl != 0) { tl--; ix = x - tlp.x; iy = y - tlp.y; history = TL; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.TRIANGLE); Obj.setVisible(true); repaint(); } else if(x >= slp.x && x <= slp.x + 25 && sl != 0) { sl--; ix = x - slp.x; iy = y - slp.y; history = SL; Obj.setX(x - ix); Obj.setY(y - iy); Obj.setObject(Obj.SQUARE); Obj.setVisible(true); repaint(); } } } return true; } public boolean mouseUp(Event evt, int x, int y) { // setCursor(cpoint); if(Obj.getVisible()) { Obj.setVisible(false); if(x >= 0 && x <= 150 && y >= 0 && y <= 275) { int object = Obj.getObject(); switch(object) { case 0: if(cl < 9) cl++; if(history != CL) weighs++; break; case 1: if(tl < 9) tl++; if(history != TL) weighs++; break; case 2: if(sl < 9) sl++; if(history != SL) weighs++; break; default: System.out.println("Invalid graphic object value."); break; } } else if(x >= 350 && x <=500 && y >= 0 && y <= 275) { int object = Obj.getObject(); switch(object) { case 0: if(cr < 9) cr++; if(history != CR) weighs++; break; case 1: if(tr < 9) tr++; if(history != TR) weighs++; break; case 2: if(sr < 9) sr++; if(history != SR) weighs++; break; default: System.out.println("Invalid graphic object value."); break; } } else if(history != CC && history != TC && history != SC) weighs++; repaint(); } return true; } public boolean mouseDrag(Event evt, int x, int y) { if(Obj.getVisible()) { Obj.setX(x - ix); Obj.setY(y - iy); repaint(); } return true; } public boolean mouseMove(Event evt, int x, int y) { // setCursor(cpoint); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); return true; } public boolean action(Event evt, Object arg) { if(arg.equals("Clear")) resetGame(); else if(arg.equals("New Game")) newGame(); else if(arg.equals("Try")) tryGame(); return true; } public void resetGame() { if(weighs != 0 && (cl + tl + sl + cr + tr + sr) > 0) weighs++; cl = 0; cr = 0; tl = 0; tr = 0; sl = 0; sr = 0; clp.move(0,0); crp.move(0,0); tlp.move(0,0); trp.move(0,0); slp.move(0,0); srp.move(0,0); statusTry = false; play = false; repaint(); } public void newGame() { // String CW,TW,SW; dead = false; statusTry = false; play = false; weighs = 0; /* if(tryonoff != null && tryonoff.equals("DISABLED")) tryout.setEnabled(false); else tryout.setEnabled(true); */ if(!tryonoff) tryout.setEnabled(false); else tryout.setEnabled(true); clear.setEnabled(true); resetGame(); /* CW = getParameter("Circle"); TW = getParameter("Triangle"); SW = getParameter("Square"); if(CW == null) cw = rand(1,5); else cw = Integer.parseInt(CW); if(TW == null) tw = rand(1,5); else tw = Integer.parseInt(TW); if(SW == null) sw = rand(1,5); else sw = Integer.parseInt(SW); */ if(CW == -1) cw = rand(1,5); else cw = CW; if(TW == -1) tw = rand(1,5); else tw = TW; if(SW == -1) sw = rand(1,5); else sw = SW; /* System.out.println(""); System.out.println("cw = " + cw); System.out.println("tw = " + tw); System.out.println("sw = " + sw); */ cirt.setEnabled(true); trit.setEnabled(true); squt.setEnabled(true); cirt.setVisible(true); trit.setVisible(true); squt.setVisible(true); cirt.setText(""); trit.setText(""); squt.setText(""); if(dispshape < 0) whichshape = rand(0,2); else whichshape = dispshape; setStatus(); } public void tryGame() { if(!cirt.getText().equals("") && !trit.getText().equals("") && !squt.getText().equals("")) { tries++; if(Integer.parseInt(cirt.getText()) == cw && Integer.parseInt(trit.getText()) == tw && Integer.parseInt(squt.getText()) == sw) { wins++; dead = true; tryout.setEnabled(false); clear.setEnabled(false); cirt.setEnabled(false); trit.setEnabled(false); squt.setEnabled(false); int random = rand(0, winstat.length -1); status = winstat[random]; statusPlacement = winstatPlacement[random]; statusTry = true; soundStatus = getAudioClip(getCodeBase(), winSoundName[rand(0, winSoundName.length-1)]); play = true; } else { wrong++; int random = rand(0, wrongstat.length-1); status = wrongstat[random]; statusPlacement = wrongstatPlacement[random]; statusTry = true; soundStatus = getAudioClip(getCodeBase(), wrongSoundName[rand(0, wrongSoundName.length-1)]); play = true; } } else status = "You must enter a value for both unknowns."; } public int rand(int min, int max) { return (int) (Math.random() * (max - min + 1)) + min; } public int weigh() { int tmp = 0; int diff = (cw*cl + tw*tl + sw*sl) - (cw*cr + tw*tr + sw*sr); if(diff < 0) tmp = -16; else if(diff == 0) tmp = 0; else if(diff > 0) tmp = 16; return tmp; } public void setStatus() { switch(whichshape) { case 0: // cirt.setEnabled(false); cirt.setVisible(false); cirt.setText(""+cw); status = "You know the Circle weighs "+cw+" unit(s)."; break; case 1: // trit.setEnabled(false); trit.setVisible(false); trit.setText(""+tw); status = "You know the Triangle weighs "+tw+" unit(s)."; break; case 2: // squt.setEnabled(false); squt.setVisible(false); squt.setText(""+sw); status = "You know the Square weighs "+sw+" unit(s)."; break; default: System.out.println("Random number out of bounds."); break; } } } class Shape { int object,x, y; boolean visible; public final int CIRCLE = 0; public final int TRIANGLE = 1; public final int SQUARE = 2; public void Shape() { x = 0; y = 0; visible = false; object = 0; } public void Shape(int x, int y, int object, boolean visible) { this.x = x; this.y = y; this.visible = visible; this.object = object; } public void show(Graphics g) { if(visible) { switch(object) { case 0: g.setColor(Color.red); g.fillOval(x,y,25,25); break; case 1: g.setColor(Color.blue); int px[] = { x + 12, x, x + 25 }; int py[] = { y, y + 25, y + 25 }; g.fillPolygon(px, py, px.length); break; case 2: g.setColor(Color.green); g.fillRect(x,y,25,25); break; default: System.out.println("Invalad graphic object value."); break; } } } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } public void setVisible(boolean visible) { this.visible = visible; } public boolean getVisible() { return visible; } public void setObject(int object) { this.object = object; } public int getObject() { return object; } }