CS111, Wellesley College, Fall 2006

Lab 12

Wedn/Thur, November 29/30, 2006

Graphics

Brief Intro to Java Graphics

Applets

Applets are Java programs that can be invoked in web pages. To run an applet, Java creates an instance of the applet class.

Certain methods run automatically:

The g object

We'll use Java applet code:

   import java.applet.*;
   import java.awt.*; // graphics library in Java Abstract Windows Toolkit

The coordinate system

    

Basic drawing commands

   public void drawLine (int x1, int y1, int x2, int y2)
   
   public void drawRect (int x, int y, int width, int height)
    
   public void fillRect (int x, int y, int width, int height)
   
   public void setColor (Color c) 
   
   public void drawOval (int x, int y, int width, int height)
   
   public void fillOval (int x, int y, int width, int height)
   
   public void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)   
   public void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle)
   

Creating polygons

First, we create a polygon object:

   Polygon splotch = new Polygon();
  

Then, we add the points one by one (you can add as many as you like):

   splotch.addPoint(200,185);
  
   ...
  

And then we can draw the object we created:

   g.drawPolygon(splotch);
  

Creating your self-portrait

Shown below are two self-portraits, drawn by the Scarecrow from the Wizard of Oz and That Girl (Wendy Wellesley), respectively.

Your task today is to create your self-portrait.

You must use (at least) the following six methods:

More about fonts

Changing your font
To liven up your font a bit (otherwise it will appear in the default font), you can set the font for the graphics object just as you set color for drawing, only instead of setColor() you use setFont() (see the Graphics contract). Changing your font requires creating a new Font object. The Font constructor takes 3 parameters: In this lab, the font in the applet was created like this:

Font rainfont =	new Font("Serif", Font.BOLD, 20);
Want to know more?
You can read about the Font class in the Font contract. Experiment with different font names, styles, and sizes!
Want a list of available fonts?
Invoke the auxiliary method printFontList() from your paint() method in the SelfPortrait class. It will print a list of all the available fonts to your console window.

You can change the text color by setting the color of the Graphics object. Click here for more information about available colors and how to make up your own color! Download the lab12_programs folder to get started.

We'll display your results in a cs111 gallery online. Goofy prizes for the best ones!
Click here for previous cs111 student self portraits.

Want to know more? Check out the Java Graphics Contract


If you still have time, feel free to re-visit our last lab of File I/O to complete writing out a new file.