1 Program 4 Instructions, CS101, Prof. Loftin
Write a Java applet which draws 50 parallel horizontal line segments
whose endpoints all border a circle of radius 250. To be more
specific, let i be a loop variable which goes from 0 to 49, and
consider y=10i+5. Then the points
|
(250 - | Ö
|
2502 -(y-250)2
|
, y) and (250 + | Ö
|
2502 - (y-250)2
|
, y) |
|
are
both on the circle of radius 250 centered at (250,250). The line
segment between these two points is horizontal, since both points
have the same y coordinate. As i varies from 0 to 49, 50
parallel line segments are drawn, spaced 10 pixels apart. Here is a link to my solution.
Hints: Use a for loop and the square root function from the
Math class. Note the drawLine method from the
Graphics class takes int values. So any quantities involving
square roots will have to be cast from type double to type
int.
2 Program requirements
Turn in your Java applet code named Program4.java to
Blackboard. Be sure to import the correct library files, and
that your code works as an applet (as opposed to an application).
3 Viewing your applet on the web
First, you should create a directory called public_html in
your pegasus account. To do this, type mkdir public_html at
the pegasus prompt.
To test your applet on the web, put a file called
testProgram4.html in the public_html directory of your
pegasus account. In other words, on pegasus, type cd
public_html to get into the public_html directory. Then
type emacs testProgram4.html to edit the file. The file
testProgram4.html should contain the following html code:
<html>
<head>
<title>Testing Program4 Java applet</title>
</head>
<body>
<applet code="Program4.class" width="500" height="500">
</applet>
</body>
</html>
To test your Program4.java code, make sure your file
Program4.java is in your public_html directory, then compile
it there (using javac) to make a Program4.class file.
Then you can send your web browser to
http://www.pegasus.rutgers.edu/~loginname/testProgram4.html
to see your Java applet. (Here loginname should be replaced with
your pegasus login name.)
When you make updates to your applet code, you may need to close and
re-open your browser to make the modifications show up.
Since you will have to access and create different directories on
pegasus, it may be a good idea to try the UNIX tutorial here.
4 Extra Credit
Note the above code draws 50 horizontal line segments whose
endpoints are on the circle of radius 250 and center (250,250).
Make the following modifications to the applet code:
- Draw a circle (using the drawOval method) of radius 125
and center (250,250). (So this new circle has the same center as
but half the radius of the larger circle.)
- Make your 50 horizontal line segments only go from the boundary
of the larger circle to the boundary of the smaller circle. (So the
middle 25 or so line segments will be split into two pieces, between
the two circles.)
- Put about 25 vertical line segments, spaced 10 pixels apart,
into the smaller circle so that their endpoints touch the edge of
the smaller circle.
To apply for the extra credit portion, still just turn in a
Program4.java file for your applet into Blackboard. Here is a link to my solution.
File translated from
TEX
by
TTH,
version 3.85.
On 15 Feb 2009, 21:10.