Experiments in Java


Experiments for Session G1: Graphics and Applets

Name: ________________
ID:_______________

Experiment G1.1: A colored circle applet

Required files:

Step 1. Make copies of CircleApplet.java and circle.html. Compile CircleApplet. Then run appletviewer on circle.html. Note what you see.


 
 
 

Step 2. Open circle.html from within your internet browser (e.g., Netscape) and note what you see. Is there a significant difference between the two versions?


 
 
 

Step 3. Modify CircleApplet so that it draws a second red circle whose center is ten spaces to the right and five spaces down from the center of the first circle. Recompile your code and then reload the applet. Were you able to successfully position the circle? Which one is on top? Why?


 
 
 

Experiment G1.2: Experimenting with colors

Required files:

Step 1. Using your CircleApplet class, try to figure out how much red, how much green, and how much blue you need to create a satisfactory purple. Presumably, it is better to create a large number of circles and fill each with a different color. Once you have determined the color, write it here.


 
 
 

Step 2. Using your CircleApplet class, try to make a sequence of shades of orange such as one might see in a custom paint shop. What color values did you use?


 
 
 

Experiment G1.3: Drawing vs. filling

Required files:

Before you begin, if you have not already done so, make copies of these two files.

Step 1. Update CircleApplet so that it fills two circles of different colors, but in the same size and at the same position. For example,

  public void paint(Graphics paintBrush) {
    paintBrush.setColor(Color.red);
    paintBrush.fillOval(10,10,40,40);
    paintBrush.setColor(Color.black);
    paintBrush.fillOval(10,10,40,40);
  } // paint(Graphics)

What do you expect to see?


 
 
 

After entering your answer, compile and view the applet. You may also wish to refer to the notes on this step.

Step 2. Change the first call to fillOval to drawOval. For example,

    paintBrush.setColor(Color.red);
    paintBrush.drawOval(10,10,40,40);
    paintBrush.setColor(Color.black);
    paintBrush.fillOval(10,10,40,40);

What do you expect to see?


 
 
 

After entering your answer, compile and view the applet. You may also wish to refer to the notes on this step.

Step 3. Change the calls to drawOval and fillOval to corresponding calls to drawRect and fillRect. For example,

    paintBrush.setColor(Color.red);
    paintBrush.drawRect(10,10,40,40);
    paintBrush.setColor(Color.black);
    paintBrush.fillRect(10,10,40,40);

In this example, do you expect to see any red drawn? If so, where?


 
 
 

Step 4. What Java commands might one use to draw a black circle with diameter 40 and a one-pixel red border all around the circle? You should draw the black portion with fillOval and the red portion with drawOval.


 
 
 
 
 
 

Confirm your answer by creating a corresponding applet and drawing the picture.

Step 5. Does it matter whether the red or black component in the previous drawing is drawn first?


 
 
 

Step 6. Can you draw a similar picture using only fillOval and no calls to drawOval? If so, how? If not, why not?


 
 
 
 
 
 

Experiment G1.4: Getting boundaries

Required files:

Step 1. Make a copy of FullCircleApplet.java. Compile FullCircleApplet. Modify circle.html to load this new applet instead of CircleApplet. Then load the applet with appletviewer. What do you see? Is that what you expected?


 
 
 

Step 2. Modify circle.html so that the width of the applet is 100 and the height is 10. Do not recompile FullCircleApplet. Load the applet with appletviewer. What do you see? Is that what you expected?


 
 
 

Step 3. Load circle.html with appletviewer. If you click on one of the edges of the applet window, you should be able to resize the window. Does the circle resize when the applet size changes? What, if anything, does this suggest?


 
 
 

Step 4. If we were to draw four circles of radius 10 in the four corners of the window, what would their positions and width and height be? Once you've determined (and written down) these positions, extend FullCircleApplet.java to draw these four small circles in the corners. If you miscomputed some positions or sizes indicate which ones and analyze why.


 
 
 

Experiment G1.5: Repainting

Required files:

Step 1. Make a copy of MovingCircleApplet.java. Compile MovingCircleApplet. Modify circle.html to load this new applet instead of CircleApplet. Then load the applet with appletviewer. What do you see? Is that what you expected?


 
 
 

Step 2. Resize the window. What happens to the circle? Is that what you expected? Why or why not?


 
 
 

Step 3. Move another window over the circle. Then move it away. What happens to the circle? Is this what you expected? Why or why not?


 
 
 

Step 4. Move another window over the appletviewer window, but not over the circle. Then move it away. What happens to the circle? Is this what you expected? Why or why not?


 
 
 

Step 5. Shrink and then expand the window. What happens to the circle? Is this what you expected? Why or why not?


 
 
 

Step 6. See if you can determine other ways to make the circle move. List them here.


 
 
 

Experiment G1.6: Setting parameters

Required files:

Step 1. Make a copy of ColoredTextApplet.java and coloredtext.html. Compile ColoredTextApplet and run appletviewer on ColoredText

What do you see? Is that what you expected?


 
 
 

Step 2. Using your determination of the color purple from a previous experiment, modify coloredtext.html so that it displays your name in purple.

Step 3. Update ColoredTextApplet.java so that it changes the color each time it redraws. To support this, you will need to make fields for the red, green, and blue components of the current color. Each time you redraw, change the component fields and create a new color from those fields. Increment each component by ten at each redraw. What happens when one of the three component colors gets a value over 255? After noting any problems, fix your class so that none of the components gets a value outside of 0 to 255.


 
 
 

Step 4. Remove the param tag that sets the red value. What do you expect to happen? What does happen?


 
 
 

Step 5. Remove all of the param tags. What do you expect to happen? What does happen?


 
 
 


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 22:37:16 1999.

This page generated on Tue Oct 26 15:37:42 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu