/* unitCircle - a java bean to display a unit circle... */ import java.awt.*; import java.applet.*; //under jdk 1.1 I could extend component, but in 1.0 I need to //extend canvas to get that effect... //public class unitCircle extends Canvas { //public class unitCircle extends Component { //public class unitCircle extends Container { public class unitCircle extends Panel implements Runnable { Thread runner = null; Color circleCol = Color.black; Color hypCol = Color.black; Color sinCol = Color.red; Color cosCol = Color.blue; int circleDelay = 0; int x,y =10; int mouseX, mouseY=0; int width,height = 50; int originX = width/2; int originY = height/2; int offsetX, offsetY = 0; int onCircleX, onCircleY = 0; float hyp=0; float angle=0; //angle in Radians float angleD=0; //angle in Degrees int cartX=0, cartY=0; double angleSin=0, angleCos=0; //Runnable stuff public void start(){ if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop(){ if (runner != null) { runner.stop(); runner = null; } } public void run(){ if (circleDelay != 0){ if (circleDelay < 2) { //do it just as quick as your little bits can run setAngleD(angleD+1); } else { while (true) { try { Thread.sleep(circleDelay); } catch (InterruptedException e){} setAngleD(angleD+1); } } } //else we are in 'normal' event driven mode...I hope } ///////////////////////////////////////////////////////////// //Constructors unitCircle(){ //default constructor System.out.println("Made a new unitCircle"); this.locate(x,y); this.resize(width,height); originX=width/2; originY=height/2; setAngleD(45); repaint(); } unitCircle(int x, int y, int w, int h){ System.out.println("Made a new unitCircle"); //this.locate(x,y); //try making it a square width=Math.min(w, h); height=width; //width=w; //height=h; originX=w/2; originY=h/2; //this.setBounds(x-width,y-height,width,height); //JDK 1.1 //this.setBounds(x,y,w,h); //JDK 1.1 this.move(x,y); this.resize(w,h); //setLayout(new GridLayout(1,1)); setLayout(null); setAngleD((float) 45); repaint(); } unitCircle(int x, int y, int w, int h, int d){ System.out.println("Made a new unitCircle"); //this.locate(x,y); circleDelay = d; if (circleDelay < 2) {circleDelay = 1;} //try making it a square width=Math.min(w, h); height=width; //width=w; //height=h; originX=w/2; originY=h/2; //this.setBounds(x-width,y-height,width,height); //JDK 1.1 //this.setBounds(x,y,w,h); //JDK 1.1 this.move(x,y); this.resize(w,h); //setLayout(new GridLayout(1,1)); setLayout(null); setAngleD((float) 45); repaint(); } //end of constructors ///////////////////////////////////////////////////////////// public void init() { } public boolean handleEvent(Event evt) { // // System.out.println("In unitCircle handle event x,y "+evt.x+" "+evt.y+" originX,originY="+originX+", "+originY); // System.out.println("Target is: "+evt.target); // System.out.println("ID: "+evt.id); //System.out.println("Mouse_Down: "+evt.MOUSE_DOWN); //System.out.println("Mouse_Drag: "+evt.MOUSE_DRAG); if (evt.id == evt.MOUSE_DOWN || evt.id == evt.MOUSE_UP || evt.id == evt.MOUSE_DRAG){ // System.out.println("In unitCircle handle event x,y "+evt.x+" "+evt.y+" originX,originY="+originX+", "+originY); mouseX=evt.x; mouseY=evt.y; angleUpdate(); } return false; //return super.handleEvent(evt); } public void angleUpdate() { //Grab position, do stuff from that... //convert mouse position to cartesian coordinates. cartX = mouseX-originX; cartY = originY-mouseY; //calculate angle hyp = (float) Math.sqrt(cartX*cartX + cartY*cartY); angle = (float) (Math.asin(cartY/hyp)); angleD = (float) Math.abs(angle*(180/Math.PI)); //radians to degrees //adjust angle according to quadrant //quadrant 1 needs no adjustment if (cartX <= 0 && cartY >= 0) angleD= (90-angleD)+ 90; //quadrant 2 else if (cartX <= 0 && cartY <= 0) angleD= (angleD)+180; //quadrant 3 else if (cartX >= 0 && cartY <= 0) angleD= (90-angleD)+270; //quadrant 4 angleD=Math.round(angleD*100)/100; angle=(float)(angleD/(180/Math.PI)); //convert back to Radians //these two onCircleN=lines are KEY...absolute key! //if you want to make the radius line go backwards, or anything else wierd, //then just mess with these lines //Original //onCircleX=(int)(Math.cos(angle)*25)+originX; //onCircleY=(int)(-Math.sin(angle)*25)+originY; onCircleX=(int)(Math.cos(angle)*originX)+originX; onCircleY=(int)(-Math.sin(angle)*originY)+originY; //System.out.println("onCircleX/Y "+onCircleX+" "+onCircleY); //System.out.println("mouseX/Y "+mouseX+" "+mouseY); repaint(); //this is what makes it happen... } public void setAngle(float a){ angle=a; angleD = (float) Math.abs(angle*(180/Math.PI)); //radians to degrees onCircleX=(int)(Math.cos(angle)*originX)+originX; onCircleY=(int)(-Math.sin(angle)*originY)+originY; repaint(); } public void setAngleD(float a){ angle=(float) (a*Math.PI/180); angleD = a; // (float) Math.abs(angle*(180/Math.PI)); //radians to degrees onCircleX=(int)(Math.cos(angle)*originX)+originX; onCircleY=(int)(-Math.sin(angle)*originY)+originY; repaint(); } public float getAngle(){ return angle; } public float getAngleD(){ return angleD; } public void paint(Graphics g){ int x,y,w,h = 0; Dimension size = this.size(); //getSize in JDK 1.1 g.setColor(Color.black); //x=this.getBounds().x; //y=this.getBounds().y; w=this.getBounds().width-1; h=this.getBounds().height-1; //w=width; //h=height; //System.out.println(x+" "+y+" "+w+" "+h); //g.drawRect(x+2,y+2,w-10,h-10); ////////////////////////////////////////////////////////////// //a frame /* g.setColor(Color.lightGray); g.fillRect(x,y,w,h); g.setColor(Color.black); g.drawRect(x,y,w,h); g.setColor(Color.red); g.drawRect(x+1,y+1,w-2,h-2); */ /* g.setColor(Color.lightGray); g.drawRect(x,y,w,h); ////////////////////////////////////////////////////////////// //the circle g.setColor(circleCol); g.drawOval(x,y,w,h); */ g.setColor(Color.lightGray); g.drawRect(0,0,w,h); ////////////////////////////////////////////////////////////// //the circle g.setColor(circleCol); // g.drawOval(0,0,w-1,h-1); g.drawOval(0,0,w,h); ////////////////////////////////////////////////////////////// //draw the triangle g.setColor(hypCol); g.setColor(Color.black); g.drawLine(originX,originY,onCircleX,onCircleY); // g.drawLine(originX,originY,onCircleX,onCircleY); //radius line/hypotenuse line g.setColor(sinCol); //System.out.println("originX "+originX+" originY "+originY +"onCircleX "+onCircleX+" onCircleY "+onCircleY); g.setColor(Color.red); g.drawLine(originX,originY,onCircleX,originY); // g.drawLine(onCircleX,onCircleY,onCircleX,offsetY); //vertical g.setColor(cosCol); g.setColor(Color.blue); g.drawLine(onCircleX,originY,onCircleX,onCircleY); //x axis } }