Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Applets For the following excercises, print screen in addition to the code.

ID: 3800470 • Letter: J

Question

Java Applets

For the following excercises, print screen in addition to the code.

1. Use the Java API at http://docs.oracle.com to read about Java’s Graphics class. Learn how to use the methods drawRect() and fillRect(), then write an applet which displays a square containing a circle. Make the diameter of the circle and the length of the square’s side equal, so that the circle just touches each side of the square. Use one color to fill the circle, and another color for the part of the square not covered by the circle.

2.  Write an applet that displays a bulls-eye pattern with a circle in the middle and five (5) concentric circles around it. Fill the circles with alternating colors of your choice.

3. Write an applet that displays a colorful flower made of several petals and a different-colored center.

4. Write an applet that displays a crescent moon. Fill the crescent (lit) part of the moon with a light color, and the shaded part of the moon with a dark color.

5. Write an applet that displays a series of pictures of a person with arms, legs, and a head. Use a happy face for the head and ovals for the body, arms, and legs. Draw a sequence of figures that appear one after the other from left to right. Make the figures assume a running position and have each face have a different expression and color.

6. Design and write your own applet using a series of shapes using loops, colors, and (most importantly) your imagination.

Explanation / Answer

Here are the solutions:

For problem 1:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class gfx1 extends Applet {
   public void paint(Graphics g) {
       g.setColor(Color.red); // this sets the fill color of the next shape to be drawn
       g.fillRect(10,10,100,100); // 10,10 is the start x and start y (top left corner) and 100,100 is the width and height respectively
       g.setColor(Color.green);
       g.fillOval(10,10,100,100);
   }
}

For sub-part 2:

Code should be self-explanatory:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class gfx2 extends Applet {
   public void paint(Graphics g) {
       g.setColor(Color.blue);
       g.fillOval(10,10,120,120);
       g.setColor(Color.yellow);
       g.fillOval(20,20,100,100);
       g.setColor(Color.green);
       g.fillOval(30,30,80,80);
       g.setColor(Color.black);
       g.fillOval(40,40,60,60);
       g.setColor(Color.cyan);
       g.fillOval(50,50,40,40);
       g.setColor(Color.red);
       g.fillOval(60,60,20,20);
   }
}

For subpart 3, we have:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class gfx3 extends Applet {
   public void paint(Graphics g) {
   g.setColor(Color.red);
   g.fillOval(10,130,70,30);
   g.fillOval(30,110,30,70);
   g.setColor(Color.yellow);
   g.fillOval(30,130,30,30);
    }
}

For sub-part 4:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class gfx4 extends Applet {
   public void paint(Graphics g) {
   g.setColor(Color.white);
   g.fillOval(20,20,100,100);
   g.setColor(Color.black);
   g.fillOval(30,20,100,100);
}
}

There you are!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote