Module 2: Simple Drawing


An example with drawing geometric shapes

Consider the following program:

import java.awt.*;
import java.applet.*;

public class CircleSquare extends Applet {

  public void paint (Graphics g)
  {
    // Draw a red, filled circle:
    g.setColor (Color.red);
    g.fillOval (50,50,70,70);

    // Draw an unfilled blue square next to it:
    g.setColor (Color.blue);
    g.drawRect (140,50,80,70);
  }

}
Note:
In-Class Exercise: Draw a square and then a circle around the square so that the four corners of the square are on the circle.


A more interesting drawing example

Consider the following program:

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);           //  Left 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.
  }
  
}
Note:
In-Class Exercise: Fill in some color to make the face more interesting. Add eyebrows.
In-Class Exercise: Can you figure out how to set a background color (say, yellow) for the whole applet?


Browsing the API

Are there more methods for drawing? How does one find out?

In-Class Exercise: Modify the Face example to add an interesting polygonal object to the picture.


Making the HTML more interesting

Applets can be made more interesting by using the surrounding HTML to write stuff, include pictures and anything you can do with HTML.

For example, let's make a tiny modification to both the Face program and the Face HTML file:

  • Modification to the Face program:
    import java.awt.*;
    import java.applet.*;
    
    public class Face2 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);           //  Left 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.
    
        g.drawString ("Burrp!", 180, 165);      //  Draw a string.
      }
      
    }
        

  • Let's create Face2.html as follows
    <HTML>
    <title>Hello</title>
    <BODY>
    
      <applet code="Face2.class" width=300 height=200> 
      </applet>
    
      <br>
      Oh excuse me, I just finished lunch and er .... well, how are
      you today?
    
    </body>
    </html>
    

  • Link to applet
In-Class Exercise: Modify your earlier example with the polygon to add a string both in the applet and in the HTML file.

You can also place multiple applets on the same webpage:

  • For example, let's put both Face and Face2 on the same page:
    <HTML>
    <title>Hello</title>
    <BODY bgcolor="#FFFFFF">
    
      First applet:
    
      <p>
      <applet code="Face.class" width=300 height=200> 
      </applet>
    
      <p>
      Second applet:
    
      <p>
      <applet code="Face2.class" width=300 height=200> 
      </applet>
    
    </body>
    </html>
    

  • Link to applets