import java.util.*; import java.applet.*; import java.awt.*; public class balance extends Applet implements Runnable { final int SHP_WIDTH=10; final double ANGLE_INCREMENT=.2; final double SCALES=2; final double ANGLE=9; final int SHP_HEIGHT=20; int LOW=1; int HIGH=6; int NUMBER=2; int INCREASE=4; boolean WON_LAST=false; boolean DEBUG_VALUES=false; Random randomizer=new Random(); int[] weights=new int[] {2,3,5,1}; public Button myCheck,myReset,myAns,myNew; boolean run_thread=false; boolean stop = false; int w=0; Shapes All=new Shapes(); Shapes suu=new Shapes(); Shape[] choicesArray=new Shape[4]; Shapes choices=new Shapes(); Shape[] ShapesUp=new Shape[0]; int WhichShape=-1; Image buffer; Graphics g; Point MouseAt=new Point(-1,-1); Point MouseIn=new Point(0,0); Point MouseStart=new Point(-1,-1); Scale s; Shape sq=new Shape(); Shape sq2=new Shape(); Shape sq3; Shape[] col=new Shape[10]; Shape[] uu=new Shape[5]; Point a; Point b; Thread t; double ScaleRotation=0; public void paint(Graphics g) { UpdateScene(); } public boolean action(Event event,Object arg) { if (event.target == myReset) { stop = true; while (run_thread == true) {} stop = false; ScaleRotation = 0; s.Clear(); All.Clear(); } else if (event.target == myCheck) { while (run_thread == true) {} boolean win = s.Check(); Messenger m = new Messenger("",0,320,26); Messenger i = new Messenger("",0,350,16); char x; x=65; if (win) { if (INCREASE > NUMBER) i.Message="Push new for next level"; m.Message="You WIN!"; WON_LAST=true; } else m.Message="Try Again"; ClearScene(); s.Show(); choices.Show(); m.Show(1); i.Show(1); UpdateScene(); return true; } else if (event.target == myAns) { s.DispAns(); } else if (event.target == myNew) { stop = true; while (run_thread == true) {} stop = false; ScaleRotation = 0; if (INCREASE > NUMBER && WON_LAST) WON_LAST=(++NUMBER)<0; s.NewProblem(RandomWeights(NUMBER,LOW,HIGH),RandomStarts(NUMBER,1)); All.Clear(); } ClearScene(); s.Show(); choices.Show(); UpdateScene(); run_thread=true; return true; } public void init() { int t1=0,t2=0; String s1,s2; s1 = getParameter("RANGE LOW"); s2 = getParameter("RANGE HIGH"); if (s1 != null) t1 = MakeNum(s1); if (s2 != null) t2 = MakeNum(s2); if (!(t1 < 1 || t2 < 1 || t1 > 10 || t2 > 10 || t1 > t2)) { LOW = t1; HIGH = t2; } else { LOW = 1; HIGH = 6; } t1 = 0; s1 = getParameter("SHAPES"); if (s1 != null) t1 = MakeNum(s1); if (!(t1 < 2 || t1 > 4)) NUMBER = t1; else NUMBER = 2; t1 = 0; s1 = getParameter("OPTIONAL INCREASE TO"); if (s1 != null) t1 = MakeNum(s1); if (!(t1 < 2 || t1 > 4)) INCREASE = t1; else INCREASE = 4; String temp; temp=getParameter("OPTIONAL DEBUG"); if (temp != null && temp.equals("TRUE")) DEBUG_VALUES = true; else DEBUG_VALUES = false; setLayout(null); sq=new Shape(new Point(100,100),new int[] {0,0,10,10},new int[] {0,-10,-10,0},false,Color.black,true,true,Color.orange,"", Color.black,new Point(10,-5),true,0,SCALES,15,5,1); sq2=new Shape(new Point(100,100),new int[] {0,5,10},new int[] {0,-10,0}, false,Color.black,true,true,Color.cyan,"",Color.black,new Point(10,-5),true,0,SCALES,15,6,2); All.setFocus(0); buffer=createImage(size().width,size().height); g=buffer.getGraphics(); choicesArray[0]=new Shape(new Point(20,300),new int[] {1,1,9,9},new int[] {-1,-9,-9,-1},false,Color.black,true,true,Color.green,"",Color.black,new Point(5,-20),true,0,SCALES,10,weights[0],1); choicesArray[1]=new Shape(new Point(20,350),numList(new int[] {4},5,1,5),numList(new int[] {-4},5,2,-4),false,Color.black,true,true,Color.magenta,"",Color.black,new Point(5,-20),true,0,SCALES,10,weights[1],2); choicesArray[2]=new Shape(new Point(20,400),new int[] {1,5,9},new int[] {-1,-9,-1},false,Color.black,true,true,Color.pink,"",Color.black,new Point(5,-20),true,0,SCALES,10,weights[2],3); choicesArray[3]=new Shape(new Point(20,400),numList(new int[] {2,4},8,1,5),numList(new int[] {2,4},8,2,-4),false,Color.black,true,true,new Color(253,159,66),"",Color.black,new Point(5,-20),true,0,SCALES,10,weights[3],4); choices.Add(choicesArray); s=new Scale(); s.Init(200,200,SCALES,choicesArray,new boolean[] {false,true,false,false},this,NUMBER); s.NewProblem(RandomWeights(NUMBER,LOW,HIGH),RandomStarts(NUMBER,1)); s.setTilt(ScaleRotation); ClearScene(); choices.Show(); s.Show(); UpdateScene(); } //Graphing helper functions public void ClearScene() { g.setColor(Color.white); g.fillRect(0,0,size().width,size().height); } public void UpdateScene() { Graphics b=this.getGraphics(); b.drawImage(buffer,0,0,this); } //Thread functions public void start() { if(t == null) { t = new Thread(this); t.start(); } } public void stop() { t = null; } public void run() { int sign1, sign2; Thread current = Thread.currentThread(); while(t == current) { if (run_thread) { sign1=Sign(s.getTilt()); sign2=Sign(ScaleRotation); while (!stop && !Equals(ScaleRotation,s.getTilt())) { try { Thread.sleep(10); } catch(InterruptedException e) {} if (!stop) { if (ScaleRotation == 0) s.setTilt(s.getTilt()-ANGLE_INCREMENT*sign1); else s.setTilt(s.getTilt()+ANGLE_INCREMENT*sign2); ClearScene(); s.ShowScale(); choices.Show(); All.Show(); s.ShowShapes(); UpdateScene(); } } run_thread=false; stop = false; } } ClearScene(); s.Show(); choices.Show(); UpdateScene(); } public boolean mouseDown(Event e,int x,int y) { choices.ChooseClosest(new Point(x,y)); All.ChooseClosest(new Point(x,y)); if (choices.getFocus()!=-1) if (choices.Inside(new Point(x,y))) { All.Add(new Shape(choices.ShapeByIndex())); MouseAt=new Point(x,y); MouseIn=new Point(All.getPos().X()-x,All.getPos().Y()-y); MouseStart=new Point(x,y); return true; } if (All.getFocus()!=-1) if (All.Inside(new Point(x,y))) { MouseAt=new Point(x,y); MouseIn=new Point(All.getPos().X()-x,All.getPos().Y()-y); MouseStart=new Point(x,y); return true; } MouseAt=new Point(-1,-1); return true; } public boolean mouseUp(Event e,int x,int y) { if (!Equals(MouseAt,new Point(-1,-1))) { stop = true; while (run_thread == true) {} stop = false; ClearScene(); Pair p; p=s.ShapeMotion(MouseStart,MouseAt,All.ShapeByIndex()); if (p.second>-1) { if (p.second<2) { All.Remove(All.getFocus()); } ScaleRotation = p.first; } s.Show(); choices.Show(); UpdateScene(); run_thread=true; } MouseAt=new Point(-1,-1); return true; } public boolean mouseDrag(Event e,int x,int y) { if (!Equals(MouseAt,new Point(-1,-1))) { All.setPos(new Point(MouseIn.X()+x,MouseIn.Y()+y)); MouseAt=new Point(x,y); if (!run_thread) { ClearScene(); s.ShowScale(); choices.Show(); All.Show(); s.ShowShapes(); UpdateScene(); } } return true; } public int[] RandomWeights(int n,int lower_limit, int upper_limit) { int[] dummy = new int[n]; if (DEBUG_VALUES) Debug("The weights are:"); for(int i=0;i0;i=(i+1) % total) if (dummy[i] == false) if (randomizer.nextDouble() < ((double)shown/(double)total)) dummy[i] = (--counter > -1); return dummy; } public int MakeNum(String s) { if (s.length() == 0) return 0; int dummy=0; char temp; int c=1; for(int i=s.length()-1;i>-1;i--) { temp = s.charAt(i); if (temp >= '0' && temp <= '9') { dummy = dummy + c*(temp-'0'); c = c*10; } } if (s.charAt(0) == '-') dummy=dummy*-1; return dummy; } public Pair MakePair(double x,double y) { return new Pair(x,y); } public int[] numList(int[] factors,int segments,int func,int off) { int[] temp=new int[segments]; for(int c=0;c0) return 1; if (n<0) return -1; return 0; } public boolean Equals(Point a,Point b) { return (Equals(a.X(),b.X()) & Equals(a.Y(),b.Y())); } public boolean Equals(double a,double b) { return (Math.abs(a-b)<.001); } public int[] Add(int i,int[] a) { int[] dummy; if (a.length==0) { dummy=new int[1]; dummy[0]=i; } else { dummy=new int[a.length+1]; for(int c=0;c i) dummy[c-1]=a[c]; else if (c < i) dummy[c]=a[c]; return dummy; } public int Find(int i,int[] a) { for(int c=0;c-1;c--) { dummy += (s.charAt(c)-'0')*level; level *= 10; } return dummy; } //Scale Class class Scale { public void NewProblem(int[] w,boolean[] sw) { for(int i=0;i= how_many) { myChoices.setVisible(i,false); myChoices.setEnabled(i,false); myText[i].setVisible(false); } myHowMany = how_many; } myCheck=new Button("Check"); myCheck.setForeground(Color.black); myCheck.setBackground(Color.lightGray); myCheck.setBounds(x+(int)(sc*20),y+(int)(sc*34),60,20); myAns =new Button("Answer"); myAns.setForeground(Color.black); myAns.setBackground(Color.lightGray); myAns.setBounds(x+(int)(sc*50),y+(int)(sc*34),60,20); myReset =new Button("Clear"); myReset.setForeground(Color.black); myReset.setBackground(Color.lightGray); myReset.setBounds(x+(int)(sc*20),y+(int)(sc*44),60,20); myNew =new Button("New"); myNew.setForeground(Color.black); myNew.setBackground(Color.lightGray); myNew.setBounds(x+(int)(sc*50),y+(int)(sc*44),60,20); o.add(myCheck); o.add(myAns); o.add(myReset); o.add(myNew); } public boolean Check() { String temp1,temp2; int c=0; for(int i=0;iW2) return -ANGLE; else if (W2>W1) return ANGLE; else return 0; } public double RemoveShape(Shape s) { if (In1.Find(s)!=-1) { In1.RemoveEntry(s); W1=W1-s.getWeight(); AllignShapes(In1,pan1); } else if (In2.Find(s)!=-1) { In2.RemoveEntry(s); W2=W2-s.getWeight(); AllignShapes(In2,pan2); } if (W1>W2) return -ANGLE; else if (W2>W1) return ANGLE; else return 0; } private void AllignShapes(Shapes i, Shape pan) { int[] typs=new int[0]; int typ; int dummy; double panx; double pany; panx=pan.PointOnPoly(2).X(); pany=pan.PointOnPoly(2).Y(); if (i.size() < 21) for (int c=0;c-1;i--) MyShapes[i].Show(); } public void Add(Shape s) { Shape[] dummy=new Shape[NOS+1]; for(int i=0;i s) dummy[c-1]=MyShapes[c]; else if (c < s) dummy[c]=MyShapes[c]; MyShapes=dummy; NOS--; } public int Find(Shape s) { for(int c=0;c DistToCent(p,i)) w=i; return w; } private int ClosestBackwards(Point p,boolean IsInside) { double temp,a=1000000; int w=-1; for(int i=NOS-1;i>-1;i--) if(((IsInside & MyShapes[i].Inside(p)) | !IsInside) & MyShapes[i].getEnabled()) if (a > DistToCent(p,i)) w=i; return w; } private double DistToCent(Point p,int i) { return Math.sqrt(Math.pow(p.X()-MyShapes[i].Position().X(),2) + Math.pow(p.Y()-MyShapes[i].Position().Y(),2)); } private Shape[] MyShapes; private int focus; private int NOS; } //Shape class class Shape { public Shape(Point p, int[] px,int[] py, boolean r,Color c,boolean enabled,boolean fill,Color FillC,String txt,Color TxtCol,Point TextPt,boolean Vis,int ru,double sc,int fs, int wei,int typ) { Type=typ; Weight=wei; FontSize=fs; Scale=sc; Visible=Vis; TextPos=TextPt; Filled=fill; FillColor=FillC; Text=txt; TextColor=TxtCol; Enabled=enabled; int a,b; Pos=p; a=px.length; b=py.length; if (a>b) NOP=b; else NOP=a; int[][] temp={px,py}; Poly=temp; PolyRotated=new int[2][NOP]; System.arraycopy(px,0,PolyRotated[0],0,NOP); System.arraycopy(py,0,PolyRotated[1],0,NOP); if (r) Rotation=((int)(Math.random()*8))*45; else Rotation=0; Col=c; } public Shape(Shape s) { Type=s.Type; Weight=s.Weight; FontSize=s.FontSize; Scale=s.Scale; Visible=s.Visible; TextPos=s.TextPos; Filled=s.Filled; FillColor=s.FillColor; Text=s.Text; TextColor=s.TextColor; Enabled=s.Enabled; int a,b; Pos=new Point(s.Pos); a=s.Poly[0].length; b=s.Poly[1].length; if (a>b) NOP=b; else NOP=a; int[][] temp=new int[2][NOP]; System.arraycopy(s.Poly[0],0,temp[0],0,NOP); System.arraycopy(s.Poly[1],0,temp[1],0,NOP); Poly=temp; PolyRotated=new int[2][NOP]; System.arraycopy(s.PolyRotated[0],0,PolyRotated[0],0,NOP); System.arraycopy(s.PolyRotated[1],0,PolyRotated[1],0,NOP); Rotation=s.Rotation; Col=s.Col; } public Point PointOnPoly(int i) { if (i>-1 && i