General Physics 102 lab
Lens/back.gif
Lens/lens.gif
Lens/Lens.html
Thin Lens (converging/diverging lens/mirrors)
[url=http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=704.0]Equation for thin lens[/url]: $\\\\frac{1}{p}+\\\\frac{1}{q}=\\\\frac{1}{f}$ where [b]p[/b] is the distance between object and lens, [b]q[/b] is the distance between image and lens, and [b]f[/b] is the focus length of lens.| blue lines | trace of light path |
| green lines | backward tracing for virtual image |
| small red lines | distances f and 2*f away from the center of lens. |
| 1st textfield | current mouse position (x , y) relative to lens |
| white vertical line | reference line (you can click and drag it left/right) |
| yellow line | light path for paraxial ray assumption(mirror only) |
- Select Lens for thin lens effect(default)
- select mirror for concave/convex mirror effect
- default: assume paraxial ray (you can turn it off)
- press toggle button +/- to change between converging/diverginglens
- Move the object
- click near tip of the object, and drag it to where you like it and release the mouse button.
- Move the lens/mirror:
- click near center of the lens, and drag it to left or right.
- if click with left mouse button, only lens will move.
- if click with right mouse button, object will move with lens.
- Change the focus length of the lens/mirror
- click near the top/bottom of the lens/mirrir, then drag the mouse . Try it!
- if you adjust the size of window, parameters reset to default values.
- you can open more then one window to compare different cases, close the created window to Quit. Press
| [youtube=E2EmowDS0U4] | [youtube=HIZiiqGcwCc] |
| [youtube=brTzn9VoGks] | [youtube=PlG6ps3i9bo] |
This page is translated from Thin Lens (converging/diverging lens/mirrors) by JoeBova ()
Lens/lens2.gif
Lens/LensWindow.class
synchronized class LensWindow extends java.awt.Frame { private static boolean working; boolean is_applet; boolean is_lens; int yOffset; String[] STR; java.awt.TextField textP; java.awt.TextField textQ; java.awt.TextField textF; java.awt.TextField textM; java.awt.Checkbox cThin; java.awt.Button type; java.awt.Dimension area; int xc; int yc; double scale; boolean thinMirror; boolean objectmove; boolean rightClick; boolean moveLine; int xp; int yp; String msg; java.awt.Dimension offDimension; java.awt.Image offImage; java.awt.Graphics g; int ox; int oy; int side; double ix; double iy; double magnify; boolean inrange; boolean normal; int lxc; int lyc; int lh; double lr; double lf; int lwidth; boolean moving; boolean sizing; int[] X; int[] Y; int cnt; int x0; int y0; int ww; int hh; public void start(); public boolean handleEvent(java.awt.Event); void init(); public boolean action(java.awt.Event, Object); public void reset(); public boolean mouseDown(java.awt.Event, int, int); public boolean mouseDrag(java.awt.Event, int, int); public boolean mouseUp(java.awt.Event, int, int); public boolean mouseMove(java.awt.Event, int, int); private void writeXY(int, int); public boolean mouseExit(java.awt.Event, int, int); public void textInput(int, double); public void paint(java.awt.Graphics); public void update(java.awt.Graphics); private void drawGrid(java.awt.Graphics); private void writeText(java.awt.TextField, double); public void drawRay(java.awt.Graphics); String d2String(double); private void drawit(java.awt.Graphics, int, int, int); public void linit(int, int, int, double); boolean lmouseDown(int, int); boolean lmouseUp(int, int); boolean lmouseDrag(int, int); void lmove(int, int); void lshow(); void LensWindow(String, boolean, String[]); static void <clinit>(); }
Lens/thinLens.class
public synchronized class thinLens extends java.applet.Applet { String buttonText; String windowTitle; int windowWidth; int windowHeight; LensWindow m; int windowCount; java.awt.Color bgColor; String rts; String[] STR; public void init(); private void go(); public boolean action(java.awt.Event, Object); public static void main(String[]); private void begin(); public void thinLens(); }
Lens/thinLens.java
Lens/thinLens.java
// Modification history
// May 31,1997 fixed bug when object located at focus point
// May 22,1997 add paraxial ray option for mirror
// Feb 21,1997 major modification, almost rewritten the whole code
// try without frame => very slow for netscape
// Nov. 3, 1996 impletemt double buffering
//-----------------------------------------------------
// written by Fu-Kwun Hwang
// I hope that you enjoy this applet
// Suggestions? E-mail to [email protected]
//-----------------------------------------------------
import
java
.
awt
.
*
;
public
class
thinLens
extends
java
.
applet
.
Applet
{
String
buttonText
=
"start"
;
String
windowTitle
=
"Thin Lens demonstration by Fu-Kwun Hwang(1996)"
;
int
windowWidth
=
600
;
int
windowHeight
=
350
;
LensWindow
m
;
int
windowCount
=
0
;
Color
bgColor
=
new
Color
(
0xC8
,
0xDF
,
0xD0
);
String
rts
,
STR
[]
=
{
"Reset"
,
"p"
,
"q"
,
"f"
,
"m"
,
"lens"
,
"mirror"
,
"Paraxial"
};
public
void
init
()
{
setBackground
(
bgColor
);
for
(
int
i
=
0
;
i
<
STR
.
length
;
i
++
){
if
((
rts
=
getParameter
(
STR
[
i
]))
!=
null
)
STR
[
i
]
=
new
String
(
rts
);
}
String
str
;
// get parameters
if
((
str
=
getParameter
(
"buttonText"
))
!=
null
)
buttonText
=
str
;
if
((
str
=
getParameter
(
"windowTitle"
))
!=
null
)
windowTitle
=
str
;
if
((
str
=
getParameter
(
"windowWidth"
))
!=
null
)
windowWidth
=
Integer
.
parseInt
(
str
);
if
((
str
=
getParameter
(
"windowHeight"
))
!=
null
)
windowHeight
=
Integer
.
parseInt
(
str
);
if
((
str
=
getParameter
(
"autoStart"
))
!=
null
){
m
=
new
LensWindow
(
windowTitle
,
true
,
STR
);
go
();
}
add
(
new
Button
(
buttonText
));
}
private
void
go
(){
m
.
resize
(
windowWidth
,
windowHeight
);
m
.
show
();
m
.
start
();
}
public
boolean
action
(
Event
e
,
Object
arg
)
{
if
(
e
.
target
instanceof
Button
&&
((
String
)
arg
).
equals
(
buttonText
))
{
m
=
new
LensWindow
(
String
.
valueOf
(
++
windowCount
)
+
":"
+
windowTitle
,
true
,
STR
);
}
go
();
return
true
;
}
//allow the applet to also run as an application.
public
static
void
main
(
String
args
[])
{
new
thinLens
().
begin
();
}
private
void
begin
()
{
m
=
new
LensWindow
(
windowTitle
,
false
,
STR
);
go
();
}
}
class
LensWindow
extends
Frame
{
boolean
is_applet
;
boolean
is_lens
=
true
;
int
yOffset
=
30
;
String
STR
[];
LensWindow
(
String
title
,
boolean
isapp
,
String
[]
s
){
super
(
title
);
is_applet
=
isapp
;
setBackground
(
Color
.
lightGray
);
STR
=
s
;
init
();
}
public
void
start
(){
reset
();
repaint
();
// lMirror.hide();
// cThin.hide();
}
public
boolean
handleEvent
(
Event
e
)
{
if
(
e
.
id
==
Event
.
WINDOW_DESTROY
)
{
hide
();
removeAll
();
dispose
();
if
(
!
is_applet
)
System
.
exit
(
0
);
}
return
super
.
handleEvent
(
e
);
}
TextField
textP
,
textQ
,
textF
,
textM
;
//,textR;
// Label lMirror;
Checkbox
cThin
;
Button
type
;
Dimension
area
;
int
xc
,
yc
;
double
scale
=
10.
;
void
init
(){
Panel
p
=
new
Panel
();
// p.add(textR=new TextField(" X , Y",6));
p
.
add
(
new
Label
(
STR
[
1
]));
p
.
add
(
textP
=
new
TextField
(
"10."
,
2
));
p
.
add
(
new
Label
(
STR
[
2
]));
p
.
add
(
textQ
=
new
TextField
(
"10."
,
2
));
p
.
add
(
new
Label
(
STR
[
3
]));
p
.
add
(
type
=
new
Button
(
"+"
));
p
.
add
(
textF
=
new
TextField
(
"5."
,
2
));
p
.
add
(
new
Label
(
STR
[
4
]));
p
.
add
(
textM
=
new
TextField
(
"1."
,
2
));
Choice
c
;
p
.
add
(
c
=
new
Choice
());
c
.
addItem
(
STR
[
5
]);
c
.
addItem
(
STR
[
6
]);
p
.
add
(
new
Button
(
STR
[
0
]));
// p.add(lMirror=new Label());
p
.
add
(
cThin
=
new
Checkbox
(
STR
[
7
]));
cThin
.
setState
(
true
);
cThin
.
show
();
thinMirror
=
cThin
.
getState
();
add
(
"North"
,
p
);
show
();
}
boolean
thinMirror
=
true
;
public
boolean
action
(
Event
ev
,
Object
arg
)
{
if
(
ev
.
target
instanceof
Button
)
{
String
label
=
(
String
)
arg
;
if
(
label
.
equals
(
STR
[
0
]))
reset
();
else
{
if
(
label
.
equals
(
"+"
))
type
.
setLabel
(
"-"
);
else
if
(
label
.
equals
(
"-"
))
type
.
setLabel
(
"+"
);
lf
*=-
1.
;
repaint
();
}
}
else
if
(
ev
.
target
instanceof
TextField
){
String
label
=
(
String
)
arg
;
double
value
;
value
=
Double
.
valueOf
(
label
).
doubleValue
();
if
(
ev
.
target
==
textP
)
textInput
(
1
,
value
);
else
if
(
ev
.
target
==
textQ
)
textInput
(
2
,
value
);
else
if
(
ev
.
target
==
textF
)
textInput
(
3
,
value
);
else
if
(
ev
.
target
==
textM
)
textInput
(
4
,
value
);
repaint
();
}
else
if
(
ev
.
target
instanceof
Choice
){
String
label
=
(
String
)
arg
;
if
(
label
.
equals
(
STR
[
5
])){
is_lens
=
true
;
// lMirror.hide();
// cThin.hide();
}
else
{
is_lens
=
false
;
// lMirror.show();
cThin
.
show
();
show
();
}
lshow
();
repaint
();
}
else
if
(
ev
.
target
instanceof
Checkbox
){
thinMirror
=
cThin
.
getState
();
lshow
();
repaint
();
}
return
true
;
}
public
void
reset
()
{
area
=
size
();
area
.
height
-=
yOffset
;
xc
=
area
.
width
/
2
;
yc
=
area
.
height
/
2
;
double
ff
=
10.
*
scale
;
type
.
setLabel
(
"+"
);
textF
.
setText
(
Double
.
toString
(
ff
));
linit
(
xc
,
yc
,(
int
)(
0.7
*
yc
),
ff
);
xc
-=
(
int
)(
2.
*
lf
);
// initial object position
yc
=
yc
*
3
/
4
;
repaint
();
}
boolean
objectmove
=
false
,
rightClick
=
false
,
moveLine
=
false
;
int
xp
,
yp
;
// vertical reference line
public
boolean
mouseDown
(
Event
e
,
int
x
,
int
y
){
y
-=
yOffset
;
if
(
Math
.
abs
(
x
-
xp
)
<
5
)
moveLine
=
true
;
else
if
(
!
lmouseDown
(
x
,
y
)){
if
(
Math
.
abs
(
x
-
xc
)
<
5
){
xc
=
x
;
yc
=
y
;
repaint
();
objectmove
=
true
;
}
}
if
(
e
.
modifiers
==
Event
.
META_MASK
)
//"Right Click, ";
rightClick
=
true
;
return
true
;
}
public
boolean
mouseDrag
(
Event
e
,
int
x
,
int
y
){
y
-=
yOffset
;
if
(
working
)
return
true
;
if
(
lmouseDrag
(
x
,
y
)){
if
(
rightClick
)
xc
=
lxc
-
ox
;
// also move objects
repaint
();
// change lens
}
else
if
(
objectmove
){
// move object
if
(
!
is_lens
&&
x
>
lxc
||
lh
<
Math
.
abs
(
y
-
lyc
)
)
return
true
;
xc
=
x
;
yc
=
y
;
if
(
rightClick
)
lxc
=
xc
+
side
*
ox
;
// also move lens
writeXY
(
x
,
y
);
repaint
();
}
else
if
(
moveLine
){
xp
=
x
;
yp
=
y
;
writeXY
(
x
,
y
);
repaint
();
}
return
true
;
}
public
boolean
mouseUp
(
Event
e
,
int
x
,
int
y
){
y
-=
yOffset
;
lmouseUp
(
x
,
y
);
objectmove
=
false
;
rightClick
=
false
;
moveLine
=
false
;
repaint
();
return
true
;
}
public
boolean
mouseMove
(
Event
evt
,
int
x
,
int
y
){
y
-=
yOffset
;
if
(
working
)
return
true
;
if
(
!
objectmove
)
writeXY
(
x
,
y
);
repaint
();
return
true
;
}
String
msg
=
" X , Y"
;
private
void
writeXY
(
int
x
,
int
y
){
//textR.setText(
msg
=
Double
.
toString
((
int
)(
10.
*
(
lxc
-
x
)
/
scale
)
/
10.
)
+
","
+
Double
.
toString
((
int
)(
10.
*
(
lyc
-
y
)
/
scale
)
/
10.
);
}
public
boolean
mouseExit
(
Event
evt
,
int
x
,
int
y
){
y
-=
yOffset
;
// textR.setText(" X , Y");
msg
=
" X , Y"
;
repaint
();
return
true
;
}
public
void
textInput
(
int
type
,
double
value
){
switch
(
type
)
{
case
1
:
// P
xc
=
lxc
-
(
int
)(
value
*
scale
);
break
;
case
2
:
// Q
xc
=
lxc
-
(
int
)(
1.
/
(
1.
/
lf
-
1.
/
(
scale
*
value
)));
break
;
case
3
:
// F
lf
=
value
*
scale
;
break
;
case
4
:
// M
xc
=
lxc
-
(
int
)(
Math
.
abs
((
1.
-
1.
/
value
)
*
lf
));