Learning to Draw

back

Your First Java Drawing

The Java library provides many default shapes that one can use to draw. Using the provided methods is as simple as writing text to the applet. Observe the following program:
      import java.awt.*;
      import java.applet.*;

      public class CircleSquare extends Applet{
           public void paint (Graphics g){
                //Sets the draw color to cyan
                g.setColor(Color.cyan);
      
                //Draws a square (width == height)
                g.fillRect(50, 50, 75, 75);

                //Sets the draw color to
                g.setColor(Color.red);
		
                //Draws a circle (width == height)
                g.drawOval(50, 50, 75, 75);
	   }
      }
    
Notice:
Exercise 12: Change the above applet so that the circle is drawn touching all four corners of the square. Example.

First Real Drawing

Let's look at a more interesting example.
      import java.awt.*;
      import java.applet.*;

      public class Face extends Applet{
           public void paint(Graphics g){
                g.drawOval (40, 40, 120, 150);          //  Head.
                g.drawOval (57, 75, 30, 20);            //  Left eye.
                g.drawOval (110, 75, 30, 20);           //  Right eye.
                g.fillOval (68, 81, 10, 10);            //  Left pupil.
                g.fillOval (121, 81, 10, 10);           //  Right pupil.
                g.drawOval (85, 100, 30, 30);           //  Nose.
                g.fillArc (60, 125, 80, 40, 180, 180);  //  Mouth.
                g.drawOval (25, 92, 15, 30);            //  Left ear.
                g.drawOval (160, 92, 15, 30);           //  Right ear.
           }
      }
    
Applet
Exercise 13:

Using the Java API

Thus far, we have just scratched the surface of Applet drawing capabilities. How do we find out more?
Exercise 14: