/* 24 September 2001 by Chris Thiel, OFMCap refrac is a demonstration of manipulating the incident angle of light that is refracted in various materials to learn about the use of the refractive index ver 2.1 ( 2 Mar 2006) Changed order of calculations to reduce error, added labels to certain substances ver 2.0 (26 Feb 2006) Added different materials */ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class refrac extends Applet { TextField angle = new TextField ("Type Here"); double iAng, rAng, indx1, indx2, topIndx, bottomIndx; boolean sMoving=false; Rectangle slider; Choice inputAngle=new Choice(); Choice top=new Choice();//Top medium Choice bottom= new Choice();//Bottom medium double refIndx [ ]; Color mColor []; String material="ABCDEFGHIJKLMNOP"; private double deg(double theta) { return theta*180.0D/Math.PI; } private double rad(double theta) { return theta*Math.PI/180.0D; } public void init() { String names[]; names = new String[14]; names[0]="vacuum"; names[1]="water"; names[2]="Ethyl Alcohol"; names[3]="Carbon disulfide"; names[4]="Air 1 atm, 20C"; names[5]="Methylene iodide"; names[6]="Fused Quartz"; names[7]="Glass, crown"; names[8]="Glass, dense flint"; names[9]="Sodium chloride"; names[10]="K"; //"Diamond"; names[11]="L"; //"Cubic Zirconia"; names[12]="ice"; names[13]="sapphire"; //setLayout(new BorderLayout()); for (int i=0;i<14;i++){ top.addItem("Top Material: "+names[i]); bottom.addItem("Bottom Material: "+names[i]); } add(top); add(bottom); slider = new Rectangle (470, 50, 20, 270); angle.setText("60"); inputAngle.addItem("incident angle"); inputAngle.addItem("refraction angle"); add(inputAngle); //add(inputAngle, BorderLayout.NORTH); add(angle); //add(angle, BorderLayout.NORTH); topIndx = 1.00D; bottomIndx = 1.33D; refIndx = new double[14]; refIndx[0]=1.0D; //vacuum refIndx[1]=1.33D; //water refIndx[2]=1.36D; //Ethyl Alcohol refIndx[3]=1.63D; //Carbon disulfide refIndx[4]=1.0003D; //Air 1 atm, 20C refIndx[5]=1.74D; //Methylene iodide refIndx[6]=1.46D; //Fused Quartz refIndx[7]=1.52D; //Glass, crown refIndx[8]=1.66D; //Glass, dense flint refIndx[9]=1.54D; //Sodium chloride refIndx[10]=2.419D; //Diamond refIndx[11]=1.54D; //Cubic Zirconia refIndx[12]=1.309D; //ice refIndx[13]=1.76D; //sapphire mColor = new Color[14]; mColor[0]=Color.white; //vacuum mColor[1]=new Color(0x1080FF); //water mColor[2]=new Color(0xFFFFEB); //Ethyl Alcohol mColor[3]=new Color(0xFBFBCB); //Carbon disulfide mColor[4]=new Color(0xCFEEED); //Air 1 atm, 20C mColor[5]=new Color(0xEFDF7B); //Methylene iodide mColor[6]=new Color(0xEFFF7B); //Fused Quartz mColor[7]=new Color(0xBFCF7B); //Glass, crown mColor[8]=new Color(0xEF9F7B); //Glass, dense flint mColor[9]=new Color(0xBF6F7B); //Sodium chloride mColor[10]=new Color(0xEEEE9B); //Diamond mColor[11]=new Color(0xFFEE7B); //Cubic Zirconia mColor[12]=new Color(0xAADDFF); //ice mColor[13]=new Color(0x8ABBFF); //sapphire repaint(); } public void paint( Graphics g ) { g.setColor(mColor[bottom.getSelectedIndex()]); g.fillRect ( 0, 200, 500, 200); g.setColor(mColor[top.getSelectedIndex()]); g.fillRect ( 0, 0, 500, 200); g.setColor(Color.black); g.drawLine(250,40,250,400);// refence line g.drawLine(0,200,500,200); //g.drawString( "Air " ,30,30); // Snell's Law: sin (rAngle) = (index1/index2)sin(iAng) bottomIndx=refIndx[bottom.getSelectedIndex()]; topIndx=refIndx[top.getSelectedIndex()]; if(inputAngle.getSelectedIndex()==0){ rAng = deg(Math.asin((topIndx/bottomIndx)*Math.sin(rad(iAng)))); } else { //iAng = deg(bottomIndx*Math.asin(Math.sin(rad(rAng))/topIndx)); iAng = deg(Math.asin((bottomIndx/topIndx)*Math.sin(rad(rAng)))); } g.drawString( " "+iAng+(char)(186), 225, 130 ); g.drawString( " "+rAng+(char)(186), 275, 330 ); //angle ray g.setColor(Color.black); g.drawLine(250,200,250-(int)(400*Math.sin(rad(iAng))),200-(int)(400*Math.cos(rad(iAng)))); //refracted ray g.setColor(Color.red); g.drawArc(210,160,80,80,90,(int)(iAng)); g.drawArc(210,160,80,80,270,(int)(rAng)); g.setColor(Color.black); g.drawLine(250,200,250+(int)(400*Math.sin(rad(rAng))),200+(int)(400*Math.cos(rad(rAng)))); //slider g.setColor(new Color(0xAAAAAA)); g.fillRect (slider.x, slider.y, slider.width, slider.height); g.setColor(Color.black); g.drawLine(slider.x+2,(int)(50+3*iAng),slider.x+16,(int)(50+3*iAng)); } /*** * Choice event methods */ public boolean action(Event e, Object arg) { if (e.target instanceof Choice) { if(inputAngle.getSelectedIndex()==0){ iAng=Double.valueOf(angle.getText()).doubleValue() ; } else { rAng=Double.valueOf(angle.getText()).doubleValue() ; } repaint(); return true; } else { return false;} } /* * Mouse methods */ public boolean mouseDown(java.awt.Event evt, int x, int y) { if ( slider.contains(x,y) ) { if(inputAngle.getSelectedIndex()==0){ iAng = (double)((y-50)/3); angle.setText(Double.toString(iAng));} else { rAng = (double)((y-50)/3); angle.setText(Double.toString(rAng)); } } else { if(inputAngle.getSelectedIndex()==0){ iAng=Double.valueOf(angle.getText()).doubleValue() ; } else { rAng=Double.valueOf(angle.getText()).doubleValue() ; } } repaint(); return true; } public boolean mouseDrag(java.awt.Event evt, int x, int y) { if (slider.contains(x,y)) { double a = (double)((y-50)/3); angle.setText(Double.toString(a)); if(inputAngle.getSelectedIndex()==0){ iAng=a; } else { rAng =a; } repaint(); } return true; } }